Hello everyone!
i am building a mini sumo robot for a school project. i am using 2 IR sensors (1 placed on the front and on the back ), 1 ultrasonic sensor and 2 dc motors which are contacted to a L298N Dual H-Bridge.
the idea is , or my strategy is when both IR sensor sees the black colour which is the arena colour the robot will start rotating or searching none stop , until the ultrasonic sensor will give a reading which is , if the distance to the object is less than 10cm , the robot should go forward.
the issue is . if you check the code down bellow , where it is written
while (IR_back > 650 && IR_front > 650 ) { // > 650 means black color
search();
break;
}
`
it is actually rotating (searching) nonstop which is what I want , but the issue is when I add this conditions which is
while (IR_back > 650 && IR_front > 650 ) {
search();
if (distance < 10 ){
FORWARD ();
}
break;
}
`
the robot won't rotate anymore and it would just go straight forward even when the distance is more than 10cm .
however all the sensors are working fine when tested separately. including the ultrasonic sensor
any help would mean a lot as i have to submit this project tomorrow and i have been trying for the past two day.
thank you
here is the full code :
#include <Ultrasonic.h>
Ultrasonic ultrasonic(3,4);
int distance ;
#define IR_sensor_front A0 // front sensor
#define IR_sensor_back A1 // rear senson
// motor pins
const int IN1=5;
const int IN2=6;
const int IN3=9;
const int IN4=10;
void setup() {
delay (5000); // delay for 5 sec when turned on
}
void loop() {
distance = ultrasonic.read();
int IR_front = analogRead(IR_sensor_front);
int IR_back = analogRead(IR_sensor_back);
while (IR_front< 650 ) {
Stop();
delay (50);
BACKWARD();
delay(500);
break;
}
while (IR_back < 650 ) {
Stop();
delay (50);
FORWARD();
delay(500);
break;
}
while (IR_back > 650 && IR_front > 650 ) {
search();
if (distance < 10 ){
FORWARD ();
}
break;
}
}
void FORWARD (){
analogWrite(IN1,0);
analogWrite(IN2,255);
analogWrite(IN3,0);
analogWrite(IN4,255);
}//--------------------------------------------
void BACKWARD (){
analogWrite(IN1,255);
analogWrite(IN2,0);
analogWrite(IN3,255);
analogWrite(IN4,0);
}//--------------------------------------------
void Stop(){
analogWrite(IN1,0);
analogWrite(IN2,0);
analogWrite(IN3,0);
analogWrite(IN4,0);
}//--------------------------------------------
void search(){
analogWrite(IN1,0);
analogWrite(IN2,255);
analogWrite(IN3,255);
analogWrite(IN4,0);
}
`