Datasheet
Navigating with Infrared Headlights • Chapter 7
Robotics with the BOE Shield-Bot • 223
/*
* Robotics with the BOE Shield - TestLeftIR
* Display 1 if the left IR detector does not detect an object,
* or 0 if it does.
*/
void setup() // Built-in initialization block
{
tone(4, 3000, 1000); // Play tone for 1 second
delay(1000); // Delay to finish tone
pinMode(10, INPUT); pinMode(9, OUTPUT); // Left IR LED & Receiver
Serial.begin(9600); // Set data rate to 9600 bps
}
void loop() // Main loop auto-repeats
{
int irLeft = irDetect(9, 10, 38000); // Check for object
Serial.println(irLeft); // Display 1/0 no detect/detect
delay(100); // 0.1 second delay
}
// IR Object Detection Function
int irDetect(int irLedPin, int irReceiverPin, long frequency)
{
tone(irLedPin, frequency, 8); // IRLED 38 kHz at least 1 ms
delay(1); // Wait 1 ms
int ir = digitalRead(irReceiverPin); // IR receiver -> ir variable
delay(1); // Down time before recheck
return ir; // Return 1 no detect, 0 detect
}
Leave the BOE Shield-Bot connected to its programming cable, and open the
Serial Monitor when the sketch is done loading.
1’s → no object
detected
0’s → object
detected