Light Seeking Robot

light seeker@youtube
Another simple project. I used an Arduino Nano to build a light seeker robot.

The project consists of two ldr´s resistors and a servo. Ldr are light dependent resistors, the change their resistivity depending on how much light they are exposed to.

Just connect your servo at vcc an gnd, and at any pwm digital input ( I’ve choosen 9 ).
light-seeker-robot

The ldrs are connected on Vcc , and a 1k resistor connected to ground. Connect the point between the Ldr and resistor to any Analogic input, I choose a0 and a1.

The program is very small and the result is a light seeker robot. Try it!

/*
AnalogReadSerial
Reads an analog input on pin 0, prints the result to the serial monitor.
Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.

This example code is in the public domain.
*/

#include

Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created

int pos;
// the setup routine runs once when you press reset:
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
// inicial position, at the middle.
pos=90;
myservo.write(pos); // tell servo to go to position in variable ‘pos’
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}

// Light seeker robot – Ricardo Pecanha – eletrofun.com.br
// the loop routine runs over and over again forever:
void loop() {
int diferenca,posmap=pos;
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// read the input on analog pin 1:
int sensorValue1 = analogRead(A1);
// print out the value you read:
// I added 40, after test my ldr´s on same light.
diferenca=sensorValue-(sensorValue1+40);
//int mapd=map(diferenca,-1023,1024,-255,256);
int mapd=diferenca;

// To avoid constant changes I only move the servo after some difference is observed. Like an
hystheresis.

if ( abs ( mapd ) > 3 )
{
if ( mapd > 0 )
pos–;
else
pos++;
}

posmap=constrain(pos,1,180);
myservo.write(posmap); // tell servo to go to position in variable ‘pos’
delay(100); // delay in between reads for stability
}