This chapter focused on using a pair of infrared LED emitters and receivers to broadcast and detect reflections of 38.5 kHz modulated infrared light. Using this sensor system for robot navigation used these concepts and skills:
int irLeft = 1; // Declare IR variables and int irRight = 1; // initialize both to 1 while((irLeft == 1) && (irRight == 1)) // Wait for detection { irLeft = irDetect(9, 10, 38000); // Check for object on left irRight = irDetect(2, 3, 38000); // Check for object on right }
int irLeft = 1; // Declare IR variables and int irRight = 1; // initialize both to 1 while((irLeft == 1) && (irRight == 1)) // Wait for detection { irLeft = irDetect(9, 10, 38000); // Check for object on left irRight = irDetect(2, 3, 38000); // Check for object on right }
Now, to chase after an object, the if…else if…else statement needs to be updated so that it goes forward when it sees an object with both sensors. It should also turn toward an object instead of away from it. Here is a modified loop function.
void loop() // Main loop auto-repeats { int irLeft = irDetect(9, 10, 38000); // Check for object on left int irRight = irDetect(2, 3, 38000); // Check for object on right if((irLeft == 0) && (irRight == 0)) // If both sides detect { maneuver(200, 200, 20); // Forward 20 ms, chase object } else if(irLeft == 0) // If only left side detects { maneuver(-200, 200, 20); // Left toward object 20 ms } else if(irRight == 0) // If only right side detects { maneuver(200, -200, 20); // Right toward object 20 ms } else // Otherwise, no IR detects { maneuver(200, 200, 20); // Forward 20 ms } }
The example above goes forward for 20 ms if it does not see the object. Optionally, it could spin in place until it sees an object and then chase after it again. For this, you could place the code that spins and waits for a detection in a function. Then, your code could call that function from the end of the setup function to wait until it sees something, and also from the else statement in the loop function for when it can’t see the object any more.
void setup() // Built-in initialization block { pinMode(10, INPUT); pinMode(9, OUTPUT); // Left IR LED & Receiver pinMode(3, INPUT); pinMode(2, OUTPUT); // Right IR LED & Receiver pinMode(8, OUTPUT); pinMode(7, OUTPUT); // LED indicators -> output tone(4, 3000, 1000); // Play tone for 1 second delay(1000); // Delay to finish tone servoLeft.attach(13); // Attach left signal to pin 13 servoRight.attach(12); // Attach right signal to pin 12 } void loop() // Main loop auto-repeats { // Sniff int irLeft = digitalRead(10); // Check for IR on left int irRight = digitalRead(3); // Check for IR on right if((irLeft == 0) || (irRight == 0)) { for(int i = 0; i < 5; i++) // Repeat 5 times { digitalWrite(7, HIGH); // Turn indicator LEDs on digitalWrite(8, HIGH); tone(4, 4000, 10); // Sound alarm tone delay(20); // 10 ms tone, 10 between tones digitalWrite(7, LOW); // Turn indicator LEDs off digitalWrite(8, LOW); } } // Roam irLeft = irDetect(9, 10, 38000); // Check for object on left irRight = irDetect(2, 3, 38000); // Check for object on right if((irLeft == 0) && (irRight == 0)) // If both sides detect { maneuver(-200, -200, 20); // Backward 20 milliseconds } else if(irLeft == 0) // If only left side detects { maneuver(200, -200, 20); // Right for 20 ms } else if(irRight == 0) // If only right side detects { maneuver(-200, 200, 20); // Left for 20 ms } else // Otherwise, no IR detects { maneuver(200, 200, 20); // Forward 20 ms } }