I have started building my first robot. For proximity sensing, I have built up simple IR sensors[. The hardware works corrctly on it's own. However when I try to implement obstacle detection in my code,The front sensor always indicates that there is an obstacle in it's path(even when there isn't), and the right and left sensors constantly change between having an obstacle in it's path or not. I cannot seem to figure out what is incorrect. My robot consists of 3 sensors, a front left and right sensor, and by using a truth table, I have come up with 8 possible actions that the robot should take.
//Pin definitions
// Motor A-Right
int EnableA = 9;
int In1 = 2;
int In2 = 3;
// Motor B-Left
int EnableB = 10;
int In3 = 4;
int In4 = 5;
//Right Sensor
int IRpin = A0;
int IRemitter = 8;
//Left Sensor
int IRpin1 = A1;
int IRemitter1 = 0;
//Front sensor
int IRpin2 = A2;
int IRemitter2 = 13;
int ambientIR; // variable to store the IR coming from the ambient
int obstacleIR; // variable to store the IR coming from the obstacle
int value[10]; // variable to store the IR values
int distance; // variable that will tell if there is an obstacle or not in the right sensor
int distancea; // variable that will tell if there is an obstacle or not in the left sensor
int distanceb; // variable that will tell if there is an obstacle or not in the front sensor
void setup(){
//Configure pin modes
//Motor A all outputs
pinMode (EnableA, OUTPUT);
pinMode (In1, OUTPUT);
pinMode (In2, OUTPUT);
//Motor B all outputs
pinMode (EnableB, OUTPUT);
pinMode (In3, OUTPUT);
pinMode (In4, OUTPUT);
//LED pins for testing purposes
pinMode(7,OUTPUT); //right sensor LED
pinMode(1,OUTPUT); //left sensor LED
pinMode(11,OUTPUT); //front sensor LED
pinMode(IRemitter,OUTPUT); //Right sensor
digitalWrite(IRemitter,LOW);
pinMode(IRemitter1,OUTPUT); //Left Sensor
digitalWrite(IRemitter1,LOW);
pinMode(IRemitter2,OUTPUT); //Front Sensor
digitalWrite(IRemitter2,LOW);
}
//Define functions for each motion
void ForwardA(){
analogWrite (9, 128); //50% duty cycle PWM
digitalWrite (2, HIGH);
digitalWrite (3, LOW);
}
void ForwardB(){
analogWrite (10, 128); //50% duty cycle PWM
digitalWrite (4, HIGH);
digitalWrite (5, LOW);
}
void Forward(){
ForwardA();
ForwardB();
}
void BackwardA(){
analogWrite (9, 128);
digitalWrite (2, LOW);
digitalWrite (3, HIGH);
}
void BackwardB(){
analogWrite (10, 128);
digitalWrite (4, LOW);
digitalWrite (5, HIGH);
}
void Backward(){
BackwardA();
BackwardB();
}
void RightTurnA(){
analogWrite (9, 0);
digitalWrite (2, LOW);
digitalWrite (3, LOW);
}
void RightTurnB(){
analogWrite (10, 128);
digitalWrite (4, HIGH);
digitalWrite (5, LOW);
}
void RightTurn(){
RightTurnA();
RightTurnB();
}
void LeftTurnA(){
analogWrite (9, 128);
digitalWrite (2, HIGH);
digitalWrite (3, LOW);
}
void LeftTurnB(){
analogWrite (10, 0);
digitalWrite (4, LOW);
digitalWrite (5, LOW);
}
void LeftTurn(){
LeftTurnA();
LeftTurnB();
//delay(duration);
}
void Stop(){
analogWrite(9,0);
analogWrite(10,0);
}
void Start(){
analogWrite(9,128);
analogWrite(10,128);
}
//for right sensor
int readIR(int times){
for(int x=0;x<times;x++){
digitalWrite(IRemitter,LOW); //turning the IR LEDs off to read the IR coming from the ambient
delay(1); // minimum delay necessary to read values
ambientIR = analogRead(IRpin); // storing IR coming from the ambient
digitalWrite(IRemitter,HIGH); //turning the IR LEDs on to read the IR coming from the obstacle
delay(1); // minimum delay necessary to read values
obstacleIR = analogRead(IRpin); // storing IR coming from the obstacle
value[x] = ambientIR-obstacleIR; // calculating changes in IR values and storing it for future average
}
for(int x=0;x<times;x++){ // calculating the average based on the "accuracy"
distance+=value[x];
}
return(distance/times); // return the final value
}
//for left sensor
int readIR1(int times){
for(int x=0;x<times;x++){
digitalWrite(IRemitter1,LOW); //turning the IR LEDs off to read the IR coming from the ambient
delay(1); // minimum delay necessary to read values
ambientIR = analogRead(IRpin1); // storing IR coming from the ambient
digitalWrite(IRemitter1,HIGH); //turning the IR LEDs on to read the IR coming from the obstacle
delay(1); // minimum delay necessary to read values
obstacleIR = analogRead(IRpin1); // storing IR coming from the obstacle
value[x] = ambientIR-obstacleIR; // calculating changes in IR values and storing it for future average
}
for(int x=0;x<times;x++){ // calculating the average based on the "accuracy"
distancea+=value[x];
}
return(distancea/times); // return the final value
}
//for front sensor
int readIR2(int times){
for(int x=0;x<times;x++){
digitalWrite(IRemitter2,LOW); //turning the IR LEDs off to read the IR coming from the ambient
delay(1); // minimum delay necessary to read values
ambientIR = analogRead(IRpin2); // storing IR coming from the ambient
digitalWrite(IRemitter2,HIGH); //turning the IR LEDs on to read the IR coming from the obstacle
delay(1); // minimum delay necessary to read values
obstacleIR = analogRead(IRpin2); // storing IR coming from the obstacle
value[x] = ambientIR-obstacleIR; // calculating changes in IR values and storing it for future average
}
for(int x=0;x<times;x++){ // calculating the average based on the "accuracy"
distanceb+=value[x];
}
return(distanceb/times); // return the final value
}
void loop(){
distance = readIR(5);
distancea=readIR1(5);
distanceb=readIR2(5);
if (distance<1 && distancea<1 && distanceb<1){
//if there are no bstacle sin front of right,left or front sensors, go front, led will be low
digitalWrite(7,LOW);
digitalWrite(1,LOW);
digitalWrite(11,LOW);
Forward();
}
if (distance>1 && distancea<1 && distanceb<1){
//if there is an obstacle in front of right sensor, and no obstacle in front and left sensors, go front,
digitalWrite(7,HIGH);
digitalWrite(1,LOW);
digitalWrite(11,LOW);
Forward();
}
if(distance<1 && distancea>1 && distanceb<1){
//if there is na obstacle in front of left sensor, no obstacle in front of front and right sensors, go front
digitalWrite(1,HIGH);
digitalWrite(7,LOW);
digitalWrite(11,LOW);
Forward();
}
if (distance>1 && distancea>1 && distanceb<1){
//if there is an obsyacle in front of left and right sensors, no obstacle in front front sensor, go front
digitalWrite(7,HIGH);
digitalWrite(1,HIGH);
digitalWrite(11,LOW);
Forward();
}
if(distance<1 && distancea<1 && distanceb>1){
//if there is na obstacle in front of front sensor, no obstacle in front of left and right sensors, go right
digitalWrite(11,HIGH);
digitalWrite(7,LOW);
digitalWrite(1,LOW);
RightTurn();
}
if(distance>1 && distancea<1 && distanceb>1){
//if there is na obstacle in front of front and right sensors, no obstacle in left, go left
digitalWrite(7,HIGH);
digitalWrite(11,HIGH);
digitalWrite(1,LOW);
LeftTurn();
}
if(distance<1 && distancea>1 && distanceb>1){
//if there is na obstacle in front of front and left sensors, no obstacle in right, go right
digitalWrite(1,HIGH);
digitalWrite(11,HIGH);
digitalWrite(7,LOW);
RightTurn();
}
if(distance>1 && distancea>1 && distanceb>1){
//if there is na obstacle in front of all sensors, go back
digitalWrite(1,HIGH);
digitalWrite(7,HIGH);
digitalWrite(11,HIGH);
Backward();
}
}
](http://www.instructables.com/id/Simple-IR-proximity-sensor-with-Arduino/all/)