Servo control using Ldr

Hi there,
Im doing a project based on sun tracking solar panel. in this im using two ldr to track the intensity of the sun. and i used this code #include <Servo.h>
Servo myservo;
void setup ()
{
Serial.begin(9600);
pinMode (A0,INPUT);
pinMode (A1,INPUT);
myservo.attach(10);
}
void loop ()
{
myservo.attach(10);
int val1=analogRead(A0);
int val2=analogRead(A1);
Serial.println(val1);
Serial.println(val2);
int ser= myservo.read();
Serial.println(ser);
delay (1000);
if (val1<val2)
{
myservo.writeMicroseconds (1500);
delay (1000);
myservo.detach();
}
else if(val1>val2)
{
myservo.writeMicroseconds (-2400);
myservo.detach();

}
}

for making it happen… the problem is the servo keeps on rotating… wat can i do to make it stop , because it should not keeps on rotating it will damage the solar panel. so i need to stop the angle to certain limit… i read the ldr values all other stuffs are working fine… just need to set a threshold value so that the servo wont rotate more than that in both directions. do help me…

Servo.read() does not return the current position of the servo. Servo.read() returns the last number sent to the servo by software. There is no way to know the actual position of the servo. The servo.writeMicroseconds() function expects a number between around 1000 (0 degrees) and 2000 (180 degrees). Sending a negative number will cause undefined behavior.

I assume you have the LDRs arranged so that they only get the same brightness when the servo points them directly at the sun.

Your code needs to see which LDR is brighter and then move the servo a small amount in the direction of the brighter one.

You should have a variable (let's call it servoPos) which holds the value for the servo position and you code will increase or reduce it a little each time. Then at the bottom of loop() you can have

servo.writeMicroseconds(servoPos); delay(1000);

You should also arrange that servoPos never gets too low or too high so that it tries to move the servo past its physical limits. You will probably need to experiment to see what they are - servos vary quite a lot.

You only need servo.attach() in setup() and you probably never need servo.detach().

As @groundfungus has said, servo.read() is pretty much worthless - it will give the same number as servoPos in this example.

...R

Thanks... i did the coding taking out the servo.detach();

and the problem is the servo is keeping on rotating if an ldr is brighter than the other.. All i need is to stop the rotation of the servo to a certain angle no matter the ldt is brighter than other. cos if the servo keeps on rotating the solar panel will be damaged.. hope u got the idea wat i am saying :)

You haven't posted your updated code so I don't know if you took account of what I said about moving the servo a small distance between readings.

In your original code you have (in different places) myservo.writeMicroseconds (1500); and myservo.writeMicroseconds (-2400);

Biggest problem is that you can't give negative values. A value of 1000 is about 0 degrees, 1500 is approx 90deg and 2000 is approx 180 deg. You need to experiment to get the exact figures for your servo.

Second problem is that you are telling it to go to a specific position (1500) rather than a little more than where it was last time.

Although you did not say so I wonder if you are using a continuous rotation servo. In that case the number of microseconds determines the speed, not the position. The centre position (approx 1500) should be stop and higher numbers give motion in one direction, lower numbers in the other direction. Again you will need to experiment to find the stop value for your servo. Using a continuous rotation servo its probably best to use a fixed speed setting and use a small time interval to get the LDRs to move a little towards the sun.

...R