0% found this document useful (0 votes)
3 views

Code

Uploaded by

robadesta72
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Code

Uploaded by

robadesta72
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

int L1 = 6;

int L2 = 7;
int R1 = 5;
int R2 = 4;

int trigPin= 2;
int echoPin= 3;
long duration, dist, average;
long aver[3]; //array for averagh(servoPin);

void setup()
{
pinMode(L1, OUTPUT);// see the program here
pinMode(L2, OUTPUT);// it works welll
pinMode(R1, OUTPUT);// did u make this code
pinMode(R2, OUTPUT); // put your setup code here, to run once:

digitalWrite(L1, LOW);
digitalWrite(L2, LOW);
digitalWrite(R1, LOW);
digitalWrite(R2, LOW);

pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

Serial.begin(9600);
Serial.write("Arduino has successfully started");

void front(){
digitalWrite(L1, HIGH);
digitalWrite(L2, LOW);
digitalWrite(R1, LOW);
digitalWrite(R2, HIGH);
}

void back(){
digitalWrite(L1, LOW);
digitalWrite(L2, HIGH);
digitalWrite(R1, HIGH);
digitalWrite(R2, LOW);
}

void right(){
digitalWrite(L1, HIGH);
digitalWrite(L2, LOW);
digitalWrite(R1, HIGH);
digitalWrite(R2, LOW);
}
void measure() {
digitalWrite(10,HIGH);
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(15);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
dist = (duration/2) / 29.1; //obtain distance
}

void loop() {

for (int i=0;i<=2;i++) { //average distance


measure();
aver[i]=dist;
delay(10); //delay between measurements
}
dist=(aver[0]+aver[1]+aver[2])/3;

Serial.print(dist);

if (dist < 20 ){

front();
}

if (dist > 20){

right();
}
if ( dist >

You might also like