Code Doc

Copy the codes provided below and paste it in the coding console and click Run button


  • Copy the code

  • Paste as plain text in the coding console

  • Click RUN button


1] LED Blink Code


void setup() {
pinMode(6, OUTPUT);
}
void loop(){
digitalWrite(6, HIGH);
delay(100);
digitalWrite(6, LOW);
delay(100);
}

2] Ultrasonic Sensor Code

(For your information, there is no simulation in this lesson since this lesson is about basic connection only. So'

  • Copy the below code

  • Long press in the coding console

  • Click "Paste as Plain Text"

  • Click RUN button and CONTINUE button for this lesson)


const int trigPin = 5;
const int echoPin = 6;

float duration, distance;

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  Serial.begin(9600);
}

void loop() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  distance = (duration*.0343)/2;
  Serial.print("Distance: ");
  Serial.println(distance);
  delay(100);
}

3] Obstacle Avoidance Robot


#define echo 5
#define trig 6
#define led 13
void setup()
{
pinMode(trig, OUTPUT);
pinMode(echo,INPUT);
pinMode(1,OUTPUT);
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
Serial.begin(9600);
}
void loop()
{
long time,dist;
digitalWrite(trig,LOW);
delayMicroseconds(2);
digitalWrite(trig,HIGH);
delayMicroseconds(10);
digitalWrite(trig,LOW);
time = pulseIn(echo,HIGH);
dist = time/2/29.1;
Serial.println(dist);
delay(1000);
if (dist<100)
{
digitalWrite(1,LOW);
digitalWrite(2,LOW);
digitalWrite(3,LOW);
digitalWrite(4,LOW);
}
else
{
digitalWrite(1,HIGH);
digitalWrite(2,LOW);
digitalWrite(3,LOW);
digitalWrite(4,HIGH);
}
}

4] Line Following Robot Code


const int r1=1, r2=2, l1=3, l2=4; 
const int LS = 6;
const int RS = 5; 
int L,R;

void setup()

{

 pinMode(LS, INPUT); 
 pinMode(RS, INPUT);
 pinMode(r1, OUTPUT); 
 pinMode(r2, OUTPUT);
 pinMode(l1, OUTPUT);
 pinMode(l2, OUTPUT);
 digitalWrite(r2,LOW); 
 digitalWrite(l2,LOW);
}

void loop()

{

 L = digitalRead(LS);
 R = digitalRead(RS);
 digitalWrite(r1, !R);
 digitalWrite(l1, !L);
}


5] Bluetooth Joystick Robot

(select and copy from below if coping the code from top doesn't work)


char t;
const int r1=2,r2=3,l1=4,l2=5;
 
void setup() {
Serial.begin(9600);
pinMode(r1,OUTPUT); 
pinMode(r2,OUTPUT);
pinMode(l1,OUTPUT);
pinMode(l2,OUTPUT);
}
 
void loop() {
if(Serial.available()){
  t = Serial.read();
}
 
if(t == 'F'){
  digitalWrite(r1,HIGH);
  digitalWrite(r2,LOW);
  digitalWrite(l1,HIGH);
  digitalWrite(l2,LOW);
}
 
else if(t == 'B'){
  digitalWrite(r1,LOW);
  digitalWrite(r2,HIGH);
  digitalWrite(l1,LOW);
  digitalWrite(l2,HIGH);
}
 
else if(t == 'L'){ 
  digitalWrite(r1,HIGH);
  digitalWrite(r2,LOW);
  digitalWrite(l1,LOW);
  digitalWrite(l2,HIGH);
  delay(100);
  t = 'S';
}
 
else if(t == 'R'){
  digitalWrite(r1,LOW);
  digitalWrite(r2,HIGH);
  digitalWrite(l1,HIGH);
  digitalWrite(l2,LOW);
  delay(100);
  t = 'S';
}
}


Thank You for choosing Kodacy. We wish you all the best and a bright future