Light-Seeking Robot
Above circuit shows two photocells connected to inputs A4 and A5, to create a light-seeking robot
Shining a flashlight on one photocell will make the opposite motor turn faster, turning the robot towards the light
To determine optimal resistor size for voltage divider, measure photocell resistance in dark(Rdark) and in the light(Rlight).
Optimal Resistor= squareroot of (Rdark * Rlight).
Hints:
Use AnalogRead Serial example sketch to read dark voltages and light voltages.
Note: analogRead gives value of 0 to 1023; while analogWrite uses value of 0 to 255; so
Use Map function to appropriately scale difference value( or divide by 4).
Shining a flashlight on one photocell will make the opposite motor turn faster, turning the robot towards the light
To determine optimal resistor size for voltage divider, measure photocell resistance in dark(Rdark) and in the light(Rlight).
Optimal Resistor= squareroot of (Rdark * Rlight).
Hints:
Use AnalogRead Serial example sketch to read dark voltages and light voltages.
Note: analogRead gives value of 0 to 1023; while analogWrite uses value of 0 to 255; so
Use Map function to appropriately scale difference value( or divide by 4).
// partial code
void loop() {
int photocellleft= analogRead(A4)/4;
int photocellright= analogRead(A5)/4;
int difference=(photocellleft- photocellright);
leftspeed= 200- difference; //may have to play with these
rightspeed=200 + difference;
forward();
}
void forward() { //user-defined function
analogWrite(ENA, leftspeed); //set speed
digitalWrite(IN1, HIGH);//left motor forward
digitalWrite(IN2, LOW);
analogWrite(ENB, rightspeed); //set speed
digitalWrite(IN3, HIGH); //right motor forward
digitalWrite(IN4, LOW); //
}