Introduction: Arduino Light Seeker
Here is one of my first little project I did with my Arduino Uno.
It's is just a little light seeker. The servo turns towards the light.
I made a video to show you what it does :
Step 1: The Wiring
Here is the exact wiring I used.
The only difference is that I fixed the photoresistors on the servo (but I couldn't do it with the program)
Also I used a 9V battery in the video, it was plugged in Vin and GND.
The resistors are 200 Ohm for the LED's and 10 KOhm for the photoresistors.
Step 2: The Code
This is my first code. I tried to translate the comments in english.
The Arduino calculate the difference between the left and right photoresistor and turns the servo if the difference is greater than 30.
When the servo is turning clockwise, the right LED lights up. When it turns counter clockwise, the left LED lights up
If you see any errors or weird thing, please tell me :)
#include
Servo servo; //define servo
//Variables
int photoD = 0; //define pin 0 for right photoresistor
int photoG = 1; //define pin 1 for left photoresistor
int ledD = 3; //define pin 3 for right LED
int ledG = 5; //define pin 5 for left LED
int incr = 5; //value to add to the angle
int valS; //angle
long temps; //Time
long temps2; //Time 2
long temps3; //Time 3
long temps4; //Time 4
//----------------------------------------------------------------------------
//Setup
void setup()
{
pinMode(3, OUTPUT); //define output pin 3
pinMode(5, OUTPUT); //define output pint 5
servo.attach(8); //servo on pin 8
Serial.begin(9600); //Start serial communcation
temps = millis();
servo.write(75); //Initial position for the servo
}
//----------------------------------------------------------------------------
//Loop
void loop()
{
int valG; //photoG value
int valD; //photoD value
valD = analogRead(photoD);
valG = analogRead(photoG);
//Displays the values on the monitor every second
if (temps + 1000 < millis()){
Serial.print("right photoresistor value :");
Serial.println(valD);
Serial.print("left photoresistor value :");
Serial.println(valG);
Serial.print("Angle :");
Serial.println(valS);
Serial.println("----------------------");
temps = millis();
}
if (temps3 + 1000 < millis() && (valD - valG > 20) && (valS < 180)){
Serial.println("Turning counter clockwise");
temps3 = millis();
}
if (temps4 + 1000 < millis() && (valG - valD > 20)&& (valS > 0)){
Serial.println("Turning clockwise");
temps4 = millis();
}
if (valD - valG > 30) {
valS = valS + incr;
digitalWrite(ledG,HIGH);
}
if (valG - valD > 30) {
valS = valS - incr;
digitalWrite(ledD,HIGH);
}
if (valS > 0 || valS < 180) {
servo.write(valS); //turns the servo
}
if (valS <= 0) {
valS = 0;
}
if (valS >= 180) {
valS = 180;
}
if (temps2 + 500 < millis() && valD - valG < 30 && valG - valD < 30 ){
digitalWrite(ledD,LOW);
digitalWrite(ledG,LOW);
temps2 = millis();
}
delay(30);
}
Step 3: The End
And... that's all !
I hope you enjoyed this project !
If you have any questions, feel free to ask.
2 Comments
10 years ago on Introduction
Hi, I like your project because your project is cool but simple at the same time! It would be fun to work on this project! Could you give me your contact details so that I could ask more question on it?
I would like to know what kind of photoresistors did you use and how did you hook up the photoresistor to the servo so that it'll spin acccording to the light detected by the it.
Reply 10 years ago on Introduction
my email is nisarahimi@hotmail.com