i have a chasis, 2 hc sr 04 sensor and uno rev3 so i thought to start coding
i edited this code for obstacle avoidance
#define trigPin 13
#define echoPin 12
#define TIMEOUT 40000 // 40ms timeout (the HC-sr04 has 38 ms timeout)
void setup()
{
Serial.begin (19200);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
digitalWrite(trigPin, LOW); // Ensure triger is starting with low befre any pulse
delayMicroseconds(20); // Delay before first pulse
}
void loop()
{
long duration, distance;
digitalWrite(trigPin, HIGH); // Start the High Pulse
delayMicroseconds(10); // Delay 10 micros
digitalWrite(trigPin, LOW); // End the Pulse
duration = pulseIn(echoPin, HIGH, TIMEOUT);
distance = (duration/2) / 29.1;
if (distance >= 400 || distance <= 0)
{
Serial.println("Out of range");
}
else
{
Serial.print(distance);
Serial.println(" cm");
}
delay(50);
} "
into this
//my code
#define ta 13 //trig pin of A ultrasonic sensor
#define ea 12 //eco pin of A ultrasonic sensor
#define tb 11 //trig pin of Bultrasonic sensor
#define eb 10 //trig pin of B ultrasonic sensor
#define lp 9 // pin to l293 to control left motor
#define ln 8 //pin to l293 to control left motor
#define rp 7 //pin to l293 to control rightmotor
#define rn 6 //pin to l293 to control right motor
#define TIMEOUT 40000 // 40ms timeout (the HC-sr04 has 38 ms timeout)
void setup()
{
Serial.begin (19200);
pinMode(lp, OUTPUT);
pinMode(ln, OUTPUT);
pinMode(rp, OUTPUT);
pinMode(rn, OUTPUT);
pinMode(ta, OUTPUT);
pinMode(ea, INPUT);
pinMode(tb, OUTPUT);
pinMode(eb, INPUT);
digitalWrite(ta, LOW); // Ensure triger is starting with low before any pulse
delayMicroseconds(20); // Delay before first pulse
digitalWrite(tb, LOW);
delayMicroseconds(20);
}
void loop()
{
long dua, da,dub,db; // dua duration of A sensor// da distance by A sensor// in the same way dub//db
digitalWrite(ta, HIGH); // Start the a High Pulse
delayMicroseconds(10); // Delay 10 micros
digitalWrite(ta, LOW); // End the Pulse
dua = pulseIn(ea, HIGH, TIMEOUT);
da = (dua/2) / 29.1;
digitalWrite(tb, HIGH); // Start the b High Pulse
delayMicroseconds(10); // Delay 10 micros
digitalWrite(tb, LOW); // End the Pulse
dub = pulseIn(eb, HIGH, TIMEOUT);
db = (dub/2) / 29.1;
if ((da >=10) && (db >=10))
{
digitalWrite(lp, HIGH);
digitalWrite(ln, LOW); \\ turning right concidering obstacle in left sensor A with in 10 cm
digitalWrite(rp, HIGH);
digitalWrite(rn, LOW);
}
if ((da <=10) && (db >=10))
{
digitalWrite(lp, HIGH);
digitalWrite(ln, LOW);
digitalWrite(rn, HIGH); //turning left concidering obstacle in left sensor A with in 10 cm
digitalWrite(rp, LOW);
}
if ((db <=10)&& (da >=10))
{
digitalWrite(ln, HIGH);
digitalWrite(lp, LOW);
digitalWrite(rn, HIGH);
digitalWrite(rp, LOW);
}
if ((da <=10) && (db <=10))
{
digitalWrite(lp, HIGH);
digitalWrite(ln, LOW); // turning right concidering obstacle in both A and B sensor with in 10 cm
digitalWrite(rn, HIGH);
digitalWrite(rp, LOW);
delay(7000); // giving some delay thinking that robot will turn enough from the obstacle
digitalWrite(lp, HIGH);
digitalWrite(ln, LOW);// ten left turn to robot to travel stright always
digitalWrite(rp, HIGH);
digitalWrite(rn, LOW);
}
delay(50);
}
instead of testing this with real robot i planned for a pre check using serial monitor to print direction of robot travelling(replaced the motor block by print statment)
code below
#define ta 13 //trig pin of A ultrasonic sensor
#define ea 12 //eco pin of A ultrasonic sensor
#define tb 11 //trig pin of Bultrasonic sensor
#define eb 10 //trig pin of B ultrasonic sensor/trig pin of A ultrasonic sensor
.#define TIMEOUT 40000 // 40ms timeout (the HC-sr04 has 38 ms timeout)
void setup()
{
Serial.begin (19200);
pinMode(ta, OUTPUT);
pinMode(ea, INPUT);
pinMode(tb, OUTPUT);
pinMode(eb, INPUT);
digitalWrite(ta, LOW); // Ensure triger is starting with low before any pulse
delayMicroseconds(20);// Delay before first pulse
digitalWrite(tb, LOW);
delayMicroseconds(20);
}
void loop()
{
long dua, da, dub, db; // dua duration of A sensor// da distance by A sensor// in the same way dub//db
digitalWrite(ta, HIGH); // Start the a High Pulse
delayMicroseconds(10); // Delay 10 micros
digitalWrite(ta, LOW); // End the Pulse
dua = pulseIn(ea, HIGH, TIMEOUT);
da = (dua/2) / 29.1;
digitalWrite(tb, HIGH); // Start the b High Pulse
delayMicroseconds(10); // Delay 10 micros
digitalWrite(tb, LOW); // End the Pulse
dub = pulseIn(eb, HIGH, TIMEOUT);
db = (dub/2) / 29.1;
if ((da >=10)&& (db >=10 ))
{
Serial.println(" go straight");
}
if ((da <=10)&&(db >=10))
{
Serial.println(" right turn");
}
if ((db <=10)&&(da >=10))
{
Serial.println("left turn ");
}
if ((da <=10) && (db <=10))
{
Serial.println(" right turn");
delay(50000);
Serial.println(" left turn ");
}
delay(50);
}
then i tested the above code with only two sensor...... i left both sensor unintreuptted so o/p should be printing only "go stright" statment but the output is mixed with "right turn" and "left turn" statment i dont no why...
help me to fix this and i like to go for pwm speed control(for that i have only l293 not arduino motor sheild ) so direct me, having lots of idea about add on like bluethoot contol using android app and add navigation capability