I’ve been trying to call the irLoop within the loop. I have also tried using if else statements instead but I still can’t get my code to work.
#include <Servo.h> //Including Servo library
Servo servoLeft; // Declaring left servo
Servo servoRight; // Declaring right servo
void setup() // built-in initialization
{
pinMode(7, INPUT); // set right whisker pin
pinMode(5, INPUT); // set left whisker pin
pinMode(10, INPUT); // Left IR receiver
pinMode(9, OUTPUT); // Left IR LED
pinMode(3, INPUT); // Right IR LED receiver
pinMode(2, OUTPUT); // Right IR
pinMode(11, OUTPUT); // set left LED indicator
pinMode(1, OUTPUT); // set right LED indicator
//tone(4, 3000, 1000); // Play tone for 1 second
delay(1000); // Delay to finish tone for 1 second
servoLeft. attach(13); // Attach left signal to pin 13
servoRight.attach(12); // Attach tight signal to pin 12
}
void loop()
{
byte wLeft = digitalRead(5); // Copy left result to wLeft
byte wRight = digitalRead(7); // Copy right resutl to wRight
int irLeft = irDetect(9, 10, 42000); // check for the object on the left
int irRight = irDetect(2, 3, 42000); // check for the object on the right
if ((wLeft == 0)&& (wRight ==0) ) // If both whiskers get in contact
{
//tone(4, 3000, 2000); // Play tone for 2 seconds
delay(1000); //Delay to finish tone
irLoop; // go through the IR check
}
else if (wLeft == 0) // If only the left whisker gets in contact
{
//tone(4, 3000, 2000); // Play tone for 2 seconds
delay(1000); //Delay to finish tone
irLoop; // go through the IR check
}
else if (wRight == 0)
{
//tone(4, 3000, 2000); // Play tone for 2 seconds
delay(1000); //Delay to finish tone
irLoop; // go through the IR check
}
else
{
digitalWrite(1, LOW); // Right LED off
digitalWrite(11, LOW); // Left LED off
forward(20);
}
}
void irLoop()
{
int irLeft = irDetect(9, 10, 42000); // check for the object on the left
int irRight = irDetect(2, 3, 42000); // check for the object on the right
if ((irLeft == 0 ) && (irRight == 0)) // both sides detect an object
{
digitalWrite(1, HIGH); // Right LED on
digitalWrite(11, HIGH); // Left LED on
backward(1500); // move backwards for 1.5 seconds
}
else if(irLeft == 0) // left side detects an object
{
digitalWrite(11, HIGH); // Left LED on
turnRight( 400); // turn to the right about 60 degrees
}
else if(irRight == 0)// right side detects an objects
{
digitalWrite(1, HIGH); // Right LED on
turnLeft(400); // turn to the left about 60 degrees
}
else // no object detected
{
forward(10); // move forward for 0.01 seconds
}
}
int irDetect(int irLedPin, int irReceiverPin, long frequency)
{
tone(irLedPin, frequency, 11); // IRLED 38kHz for at least 1ms
delay(1.5); // wait for 1ms
int ir = digitalRead(irReceiverPin); // IR receiver to the ir pin
delay(1.5) ; // wait for 0.0015 seconds
return ir; // return a 0 if ir detects and a 1 if ir does not detect
}
void forward(int time) // Forward function
{
servoLeft.writeMicroseconds(1700); // Left wheel counterclockwise
servoRight.writeMicroseconds(1300); // Right wheel clockwise
delay(time); // Maneuver for time ms
}
void turnLeft(int time) // Left turn function
{
servoLeft.writeMicroseconds(1300); // Left wheel clockwise
servoRight.writeMicroseconds(1300); // Right wheel clockwise
delay(time); // Maneuver for time ms
}
void turnRight(int time) // Right turn function
{
servoLeft.writeMicroseconds(1700); // Left wheel counterclockwise
servoRight.writeMicroseconds(1700); // Right wheel counterclockwise
delay(time); // Maneuver for time ms
}
void backward(int time) // Backward function
{
servoLeft.writeMicroseconds(1300); // Left wheel clockwise
servoRight.writeMicroseconds(1700); // Right wheel counterclockwise
delay(time); // Maneuver for time ms
}
#include <Servo.h> //Including Servo library
Servo servoLeft; // Declaring left servo
Servo servoRight; // Declaring right servo
void setup() // built-in initialization
{
pinMode(7, INPUT); // set right whisker pin
pinMode(5, INPUT); // set left whisker pin
pinMode(10, INPUT); // Left IR receiver
pinMode(9, OUTPUT); // Left IR LED
pinMode(3, INPUT); // Right IR LED receiver
pinMode(2, OUTPUT); // Right IR
pinMode(11, OUTPUT); // set left LED indicator
pinMode(1, OUTPUT); // set right LED indicator
//tone(4, 3000, 1000); // Play tone for 1 second
delay(1000); // Delay to finish tone for 1 second
servoLeft. attach(13); // Attach left signal to pin 13
servoRight.attach(12); // Attach tight signal to pin 12
}
void loop()
{
byte wLeft = digitalRead(5); // Copy left result to wLeft
byte wRight = digitalRead(7); // Copy right resutl to wRight
int irLeft = irDetect(9, 10, 42000); // check for the object on the left
int irRight = irDetect(2, 3, 42000); // check for the object on the right
if ((wLeft == 0)&& (wRight ==0) ) // If both whiskers get in contact
{
//tone(4, 3000, 2000); // Play tone for 2 seconds
delay(1000); //Delay to finish tone
if ((irLeft == 0 ) && (irRight == 0)) // both sides detect an object
{
digitalWrite(1, HIGH); // Right LED on
digitalWrite(11, HIGH); // Left LED on
backward(1500); // move backwards for 1.5 seconds
}
else // no object detected
{
forward(10); // move forward for 0.01 seconds
}
}
else if (wLeft == 0) // If only the left whisker gets in contact
{
//tone(4, 3000, 2000); // Play tone for 2 seconds
delay(1000); //Delay to finish tone
if(irLeft == 0) // left side detects an object
{
digitalWrite(11, HIGH); // Left LED on
turnRight( 400); // turn to the right about 60 degrees
}
else // no object detected
{
forward(10); // move forward for 0.01 seconds
}
}
else if (wRight == 0)
{
//tone(4, 3000, 2000); // Play tone for 2 seconds
delay(1000); //Delay to finish tone
if(irRight == 0)// right side detects an objects
{
digitalWrite(1, HIGH); // Right LED on
turnLeft(400); // turn to the left about 60 degrees
}
else // no object detected
{
forward(10); // move forward for 0.01 seconds
}
}
else
{
digitalWrite(1, LOW); // Right LED off
digitalWrite(11, LOW); // Left LED off
forward(20);
}
}
int irDetect(int irLedPin, int irReceiverPin, long frequency)
{
tone(irLedPin, frequency, 11); // IRLED 38kHz for at least 1ms
delay(1.5); // wait for 1ms
int ir = digitalRead(irReceiverPin); // IR receiver to the ir pin
delay(1.5) ; // wait for 0.0015 seconds
return ir; // return a 0 if ir detects and a 1 if ir does not detect
}
void forward(int time) // Forward function
{
servoLeft.writeMicroseconds(1700); // Left wheel counterclockwise
servoRight.writeMicroseconds(1300); // Right wheel clockwise
delay(time); // Maneuver for time ms
}
void turnLeft(int time) // Left turn function
{
servoLeft.writeMicroseconds(1300); // Left wheel clockwise
servoRight.writeMicroseconds(1300); // Right wheel clockwise
delay(time); // Maneuver for time ms
}
void turnRight(int time) // Right turn function
{
servoLeft.writeMicroseconds(1700); // Left wheel counterclockwise
servoRight.writeMicroseconds(1700); // Right wheel counterclockwise
delay(time); // Maneuver for time ms
}
void backward(int time) // Backward function
{
servoLeft.writeMicroseconds(1300); // Left wheel clockwise
servoRight.writeMicroseconds(1700); // Right wheel counterclockwise
delay(time); // Maneuver for time ms
}