Your robot has 3 Infrared Sensors on the bottom, 2 in the front (left, right) and one in the rear (center). These are mainly used by the robot to sense when its near the edge of a battle ring. However, the readings could also be creatively used to sense the IR reflectivity of a surface under the robot…
You will need to activate the serial monitor (“Tools” from the Arduino IDE menu) to see the readout from the IR sensors when this code is installed and running. It’s a good idea to calibrate your robot using this code before a robot battle. This is to accommodate lighting conditions, and the reflectivity of the battle-ring which can vary. Adjust your tournament code IR thresholds to your test results:
// IR Sensor pins
#define leftSensor A1
#define rightSensor A2
#define rearSensor A3
#define IREmitter 6
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
// IR sensors
pinMode(leftSensor, INPUT);
pinMode(rightSensor, INPUT);
pinMode(rearSensor, INPUT);
pinMode(IREmitter, OUTPUT);
// Turn on our IR Emitter
digitalWrite(IREmitter, HIGH);
}
void loop() {
// put your main code here, to run repeatedly:
int leftIR = analogRead(leftSensor);
int rightIR = analogRead(rightSensor);
int rearIR = analogRead(rearSensor);
Serial.print(“Left=”);
Serial.print(leftIR);
Serial.print(“—Right=”);
Serial.print(rightIR);
Serial.print(“—Rear=”);
Serial.println(rearIR);
}