/* Tiny Light Seeker (TLS) entry for the The MicRObot Challenge @ http://letsmakerobots.com/node/38890 by Lumi December 2013 Body: ABS sheet Brain: Beetle Motors: 2x DC motor + controller from gear broken 3.5g micro servos Battery. LiPo 3.7V/550mAh Sensors: 2x LDR Holding together by Blu Tack and cable ties Simple standard programming for beginners!!! All commands are standard Arduino IDE commands. Variables for pins, values and arrays as well servo objects are named as their respective usage/function. */ #include // Pins int rightEye = A0; int leftEye = A1; int ledPin = 13; // Values int sensorValueLeft = 0; int sensorValueRight = 0; int left; int right; int stopp = 90; int fw = 100; int rev = 80; // Arrays for sensor reading average (checksum/cross sum) calculation int leftReadings[5]; int rightReadings[5]; // Creating servo objects Servo rightServo; Servo leftServo; void setup() { // declare the ledPin as an OUTPUT: pinMode(ledPin, OUTPUT); // Attach servos to their respective pin rightServo.attach (10); leftServo.attach (11); // Set servos to stop leftServo.write(stopp); rightServo.write(stopp); // Next line is for debugging -> uncomment it when needed //Serial.begin(9600); } // checking the light on each sensor void scan() { int i; // Take 5 readings on each sensor for (i = 0; i < 5; i = i + 1) { // read the value from the left and right sensor: sensorValueLeft = analogRead(leftEye); sensorValueRight = analogRead(rightEye); // add sensor readings of both sides to their respective array leftReadings[i] = sensorValueLeft; rightReadings[i] = sensorValueRight; } // calculate an average value (checksum or cross sum) for left and right sensor readings left = (leftReadings[0]+leftReadings[1]+leftReadings[2]+leftReadings[3]+leftReadings[4])/5; right = (rightReadings[0]+rightReadings[1]+rightReadings[2]+rightReadings[3]+rightReadings[4])/5; delay(20); } // move forward motor function void forward(){ leftServo.write(fw-20); rightServo.write(fw); } // turn left motor function void leftTurn(){ leftServo.write(stopp); rightServo.write(fw); } // turn right motor function void rightTurn(){ leftServo.write(fw-20); rightServo.write(stopp); } // reverse motor function -> not used yet void reverse(){ leftServo.write(rev+20); rightServo.write(rev); } // here happens all the important stuff in a loop void loop() { // call the scan function to get the sensor readings for left and right scan(); // Looks more complicated as it is :-) // Check if the left sensor sees more light than ther right sensor if (left > right){ // subtract right from left value to get a value to work with left = left-right; // check if that previous calculated value is greater than 50 if (left > 50) // I will call this "Inside IF 1" in further comments. { /* If the value is greater than 50 then turn left. Why? the 50? Due the readings it's nearly impossible to get the exact same readings from both sensors in at the same time. So if both sensors always have slightly different readings the robot would just wiggle left and right without really going forward. By eliminating slight differences and set the accepted difference to a value of 50 it's more likely that the robot actually goes to the function forward. */ leftTurn(); // Next 3 lines are for debugging -> uncomment it when needed //Serial.print ("Left: "); //Serial.println (left); //Serial.println (" "); delay (50); // Wait 50 mSec } // That happens when the left value in "Inside IF 1" is lower than 50 else{ /* Is the calculated value for left lower than 50 then go forward. Why? Is that value lower than 50 then the difference between the light on the left and right sensor are not that great. In this case it should be save to go forward. */ forward(); // Next 2 lines are for debugging -> uncomment it when needed //Serial.print ("Forward: "); //Serial.println (" "); delay (50); // Wait 50 mSec } } // That happens when left is lower than right else if (left < right){ // subtract left from right value to get a value to work with right = right-left; // check if that previous calculated value is greater than 50 if (right > 50){ // I will call this "Inside IF 2" in further comments. /* If the value is greater than 50 then turn right. Why? the 50? Due the readings it's nearly impossible to get the exact same readings from both sensors in at the same time. So if both sensors always have slightly different readings the robot would just wiggle left and right without really going forward. By eliminating slight differences and set the accepted difference to a value of 50 it's more likely that the robot actually goes to the function forward. */ rightTurn(); // Next 3 lines are for debugging -> uncomment it when needed //Serial.print ("Right: "); //Serial.println (right); //Serial.println (" "); delay (50); // Wait 50 mSec } else{ /* Is the calculated value for right lower than 50 then go forward. Why? Is that value lower than 50 then the difference between the light on the left and right sensor are not that great. In this case it should be save to go forward. */ forward(); // Next 2 lines are for debugging -> uncomment it when needed //Serial.print ("Forward: "); //Serial.println (" "); delay (50); // Wait 50 mSec } } /* That "else" happens when none of the above conditions occur and the left and right sensor shows the same readings. This condition will probably never occur but we need it anyway. */ else{ // Go forward without questions forward(); // Next 2 lines are for debugging -> uncomment it when needed //Serial.print ("Forward: "); //Serial.println (" "); delay (50); // Wait 50 mSec } /* The next 3 lines are just for fun. They let the Onboard LED on Pin 13 blink in an interval of 100mSec off and 50mSec on. The 100mSec off time is set by the delay in the second line and the 50mSec is set in either one of the conditions above. Changing that values will change the total delay in the loop and your robots behavior e.g. jerking movement when the value is getting to big */ digitalWrite (ledPin, LOW); delay (100); // Wait 100 mSec digitalWrite (ledPin, HIGH); }