This is an improved version of my first Light Seeking Robot.
The trouble with the first one, is that it needed to scan to find the lightest spot. This is slow and inefficient. Apart from that, you would need at least two servos to build a moving robot: one for scanning and one for steering.
Version 2 is a little more efficient. It uses two LDRs and measures the difference between them. Based on that difference, the servo will rotate towards the one that measures the brightest light.
The circuit is hardly more complex, it just wires up an extra LDR:
The wiring is similar to my first Light Seeker. The servo is connected to +5V, GND and control pin 9. The two LDRs are connected to pin 0 and 1. Again, a voltage divider is used.
And there it is:
The source code has even become simpler. Just reading the values from the two LDRs and modify the rotation of the motor according to it.
The need for a loop to make the motor scan its full range is gone. I’m using Serial to write the measured values to the serial port. Using the serial monitor in the Arduino IDE, these values can be read for easier debugging.
Two convensient functions help me to easily transform the measured values to a motor rotation. The first one is map, which maps a value in a range to another range. This function is used to translate the inputs to a range of -80 to 80.
Note, though, that map doesn’t constraint the value to the given range.
The second function, constrain, constrains a given value X to a given range. If X is higher than the highest value of the range, constrain returns that highest value. If X is lower than the lowest value, constrain returns the lowest value. Otherwise, the function returns X.
#include <Servo.h> Servo myservo; void setup() { Serial.begin(9600); myservo.attach(9); } int pointer = 90; void loop() { // Read and output the values of both sensors. int valLeft = analogRead(1); int valRight = analogRead(0); Serial.print("Left:"); Serial.println(valLeft); Serial.print("Right:"); Serial.println(valRight); // Calulate the difference int delta = valLeft - valRight; // Change the rotation accordingly delta = map(delta, -1023, 1023, -80, 80); pointer += delta; pointer = constrain(pointer, 0, 179); Serial.println(pointer); myservo.write(pointer); delay(50); }
This light seeking head is more useful than my first attempt, and all it took was an extra LDR. I think this is a good base for an actual moving robot.
Thank you again for reading. I’ll be back! :o)