Purpose:
The following is the sample code for reading the IR sensors on the robot
/* IR Proximity Test This is initial test code for the two proximity sensors on the top level of the robot. There is an IR send/receive pair on the front left and right quadrant. The IR sensors are connected to the following analog pins (a bit of a surprise) A2 Left IR proximity sensor A3 Right IR proximity sensor The actual behavior of the IR sensors is digital in that they are either on or off. HIGH (5V) is when the IR sensor sees nothing. LOW (0V) is when the IR sensor sees enough reflection from something to trigger. This sketch is a modification of other sketchs I have written that originally depend on sketchs in the public domain from Adafruit and others. Many thanks as always to the broad community of enthusiasts in this space. modified 7/19/20 Bruce Emerson */ // Define the sensor pins int sensorPinL = A2; int sensorPinR = A3; int sensorReadL = 0; // establish variable to hold the sensor readings int sensorReadR = 0; // This function runs once when you turn your Arduino on void setup() { Serial.begin(9600); //Start the serial connection with the computer //to view the result open the serial monitor while (!Serial) { ; // wait for serial port to connect. Needed for native USB } Serial.println(); Serial.println("The Serial Monitor is ready for business!"); Serial.println(); delay(2000); // Identify the sensor pins as inputs. pinMode(sensorPinL, INPUT); pinMode(sensorPinR, INPUT); } void loop() // run over and over again { // getting a reading from each IR sensor // the reading is a value between 0 and 1024. // 1024 represents HIGH on the pin which is usually 5V on an Uno sensorReadL = digitalRead(sensorPinL); sensorReadR = digitalRead(sensorPinR); // print out the voltage read by Uno Serial.print("Left sensor reading: "); Serial.println(sensorReadL); Serial.print("Right sensor reading: "); Serial.println(sensorReadR); delay(2000); //waiting 2s before checking again }
Copy and paste this into your sketch and then save it with a useful name of your choice. For me I called it QIRTest.