Purpose:
The following is the sample code for the IR Challlenge.
/*
IR Unknown
This sketch uses the IR proximity sensors to inform the robot about the
presence of a wall and then has a particular response. Your task is to
figure out what it does and how. This will be an example of a poorly
commented code.
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;
int rightMotor = 5; // the PWM pin for the right motor
int leftMotor = 6; // the PWM pin for the left motor
int rightForward = 7; // forward-reverse right motor
int leftForward = 8; // forward-reverse left motor
// settings
int turnSpeed = 20;
int turnTime = 100;
int motorSpeed = 50;
// logic stuff
int keepGoing = 0;
int huntCount = 0;
int movingNow = 0;
int huntTime = 100;
int wallLast = 0;
// This function runs once when you turn your Arduino on
void setup() {
// declare motor pins to be outputs:
pinMode(rightMotor, OUTPUT);
pinMode(leftMotor, OUTPUT);
pinMode(rightForward, OUTPUT);
pinMode(leftForward, OUTPUT);
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() {
if (keepGoing == 0) {
Serial.println("I'm happy moving forward");
if (movingNow == 1) {
Serial.println("....already moving...");
} else {
Serial.println("Turning on motors");
digitalWrite(rightForward, LOW);
digitalWrite(leftForward, LOW);
analogWrite(rightMotor, motorSpeed);
analogWrite(leftMotor, motorSpeed);
movingNow = 1;
}
} else {
// what if I need to stop?
}
sensorReadL = digitalRead(sensorPinL);
sensorReadR = digitalRead(sensorPinR);
// print out sensor status
Serial.print("Left sensor reading: ");
Serial.println(sensorReadL);
Serial.print("Right sensor reading: ");
Serial.println(sensorReadR);
if (sensorReadL == 0) { // if the wall is on the left
analogWrite(rightMotor, 0);
analogWrite(leftMotor, 50);
while (sensorReadL == 0) {
delay(50);
sensorReadL = digitalRead(sensorPinL);
}
analogWrite(leftMotor, 0);
wallLast = 0; // remember last wall was on left
movingNow = 0; // currently stopped
huntCount = 0; // reset counter
}
delay(huntTime);
huntCount = huntCount+1;
if (huntCount >= 10){
analogWrite(rightMotor, motorSpeed+10);
delay(huntTime);
huntCount = 0;
}
}
Copy and paste this into your sketch and then save it with a useful name of your choice.
