Im building a small robot which moves in two directions depending on the direction of the light falling. Ive used 2 LDR's and one Continuous Rotation Servo(which is separately powered from a 9v battery) to move the robot.
#include <Servo.h>
Servo S;
int val;
int val2;
void setup()
{
S.attach(12);
Serial.begin(9600);
}
void loop()
{
val = analogRead(0);
//Serial.println(val);
val2 = analogRead(1);
//Serial.println(val2);
if (val > val2)
{
val = map(val, 0, 1023, 90, 180);
S.write(val);
}
if (val2 > val)
{
val2 = map(val2, 0, 1023, 90, 0);
S.write(val2);
}
else
S.write(90);
delay(100);
}
The problem I'm facing is that the servo turns in only one direction when I hold a light on one LDR. When I shine a light on the other it doesn't turn. Was hoping someone could help me out.