hi guys,
so i just made obstacle avoiding robot using one ultrasonic sensor and two H bridges(l289n), and two dc motors and the arduino uno.But the program i wrote wouldn't do what i want.I want to turn when it sees a object ten cm way, however, what it does do is that only the right dc motor turn and when there is something infront of it both turn. help please!
#define echoPin 6
#define trigPin 7
int maximumRange = 200;
long duration, distance;
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration/58.2;
Serial.println(distance);
if(distance>=10)
{
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
}
else
{
digitalWrite(9, HIGH);
digitalWrite(8, LOW);
digitalWrite(11, HIGH);
digitalWrite(12, LOW);
}
}
this is my code. help !!