/**************************************************************************************************************** * Program for a simple light Seeking Robot. * created by: Anuj * (c)copyright * * Robotic base should include differential drive. * * Physical setup: * Two LDRs connected to ground and Analog pin 1(Right) and Analog pin 2(left) * base of NPN Transistors on pins 11(right) and 12(left). * * ******************************************************************************************************************/ // Using Arduino Duemilinove // Pin definitions - attaches a variable to a pin. const int RightMotor = 2; // This pin is used to enable or disable the Right motor. Connected to the base of an NPN transistor. const int LeftMotor = 3; // This pin is used to enable or disable the Left motor. Connected to the base of an NPN transistor. const int RightSensor = 1; // This pin is used to read the value of the Right Sensor. const int LeftSensor = 2; // This pin is used to read the value of the Left Sensor. // Variable definitions int SensorLeft; // This stores the value of the Left Sensor pin to use later on in the sketch int SensorRight; // This stores the value of the Right Sensor pin to use later on in the sketch int SensorDifference; // This value is used to determine the difference between the Left and Right // the setup() method runs once when the program is run. When the // Arduino is reset, the setup() will be executed once again. void setup() { pinMode(LeftMotor, OUTPUT); // Defines this pin as an output. The Arduino will write values to this pin. pinMode(RightMotor, OUTPUT); // Defines this pin as an output. The Arduino will write values to this pin. pinMode(LeftSensor, INPUT); // Defines this pin as an input. The Arduino will read values from this pin. pinMode(RightSensor, INPUT); // Defines this pin as an input. The Arduino will read values from this pin. digitalWrite(A1, HIGH); // Enables an internal pullup resistor digitalWrite(A2, HIGH); // Enables an internal pullup resistor Serial.begin(9600); // Enables a serial connection through the Arduino to either USB or UART (pins 0&1). Note that the baud rate is set to 9600 Serial.println(" \nBeginning Light Seeking Behavior"); // Placed at the very end of void Setup() so that it is runs once, right before the void Loop() } // the loop() method runs over and over again, // as long as the Arduino has power void loop() { SensorLeft = 1023 - analogRead(LeftSensor); // This reads the value of the sensor, then saves it to the corresponding integer. delay(1); SensorRight = 1023 - analogRead(RightSensor); // This reads the value of the sensor, then saves it to the corresponding integer. delay(1); SensorDifference = abs(SensorLeft - SensorRight); // This calculates the difference between the two sensors and then saves it to an integer. // This section of the sketch is used to print the values of the // sensors through Serial to the computer. Useful for determining // if the sensors are working and if the code is also functioning properly. Serial.print("Left Sensor = "); // Prints the text inside the quotes. Serial.print(SensorLeft); // Prints the value of the Left Sensor. Serial.print("\t"); // Prints a tab (space). Serial.print("Right Sensor = "); // Prints the text inside the quotes. Serial.print(SensorRight); // Prints the value of the Right Sensor. Serial.print("\t"); // Prints a tab (space). // This section of the sketch is what actually interperets the data and then runs the motors accordingly. if (SensorLeft > SensorRight && SensorDifference > 75) { // This is interpreted as if the Left sensor reads more light than the Right Sensor, Do this: digitalWrite(RightMotor, HIGH); // This is used to turn Left. Notice the digitalWrite(LeftMotor, LOW); // opposite motor runs to turn Left. Serial.println("Left"); // This prints Left when the robot would actually turn Left. } if (SensorLeft < SensorRight && SensorDifference > 75) { // This is interpreted as if the Left sensor reads less light than the Right Sensor, Do this: digitalWrite(RightMotor, LOW); // This is used to turn Right. Notice the digitalWrite(LeftMotor, HIGH); // opposite motor runs to turn Right. Serial.println("Right"); // This prints Right when the robot would actually turn Right. } else if (SensorDifference < 75) { // This is interpreted as if the difference between the two sensors is under 125 (Experiment to suit our sensors), Do this: digitalWrite(RightMotor, HIGH); // This is used to go straight. Notice digitalWrite(LeftMotor, HIGH); // both motors are enabled to go forward. Serial.println("Forward"); // This prints Forward when the robot would actually go forward. } Serial.print("\n"); }