programming Servo with LDR

Hello,
I have a robot car that detects light on either side a car and will swing a 3d printed arm around on a servo to that side. My only problem is that with my code, when there is an even amount of light on both sides the servo will act crazy and start twitching, so i need a way in my code to set a threshold ( if that makes any sense) to only swing to one side if it has substantially more light. Here is the original code.

Servo myservo;
int val;
byte LDRL = 0;
byte LDRR = 1;

void setup()
{
  myservo.attach(12);
}
void loop()
{
   if(analogRead(LDRL) > analogRead(LDRR) ) {
      myservo.write(0); // move to angel 80 degrees or what ever you want
}
else {
     myservo.write(180); // move to angel 100 degrees or what ever you want
  }
}

I am very new to coding. Is there a way to code something like a percentage, Like "only move the servo if it has 20% more light than the other side"??To me that doesn't seem right.
Or do i have to physically measure the resistance of the LDR in the same type of lighting conditions, read the resistances and then implement that into my code.

Not asking to write the code for me, just trying to see which is the best way to tackle this.
Thanks

To me that doesn't seem right.

Why doesn't it seem right?

It may be tricky or unstable with lower light levels, but isn't it worth a try, just to get you started?

I really dont know why that doesnt seem right to me, i didnt know you can use percentages in that manner.
Here is my best shot ...if LDRL is bigger than 20% and 20% is bigger that LDRR

{
   if(analogRead(LDRL) >20%> analogRead(LDRR) ) {
      myservo.write(0); // move to angel 80 degrees or what ever you want
}
else {
     myservo.write(180); // move to angel 100 degrees or what ever you want
  }

Percentages don't work like that - the % operator is used for modulo arithmetic in C/C++

The compiler probably already told you that, in a roundabout way.

Try putting the readings into variables, and do your arithmetic on the variables.

AWOL:
Percentages don't work like that - the % operator is used for modulo arithmetic in C/C++

The compiler probably already told you that, in a roundabout way.

Try putting the readings into variables, and do your arithmetic on the variables.

Do you mean my resistance readings?
and thanks for response

They're not really resistance readings, they're voltage readings.
Yes, just assign the results of the analogReads to variables.

I'm sorry, but I still do not understand. i know how to take a voltage reading from a voltage divider circuit with the LDR and a 10k resistor. i just measured it, when it is bright i get 4.7 v ( with a 5v supply) between output and ground. Which that is what is read by A0 of my Arduino.
so would I use something like "move servo when LDRL is .4v more that LDRR"??

LDR voltage divider.jpg

{
   if(analogRead(LDRL) > .4v> analogRead(LDRR) ) {
      myservo.write(0); // move to angel 80 degrees or what ever you want
}

LDR voltage divider.jpg

The result of an analogRead(A0) is an integer between 0 and 1023. If you think you've read a voltage then you're imagining things. It's just an integer so trying to compare it with .4V which is very definitely not an integer is never going to work.

You could work out what number you actually read when the voltage is 4.7V and from that you could work out what the number should be for say 10% (1/10th) of that and then use those numbers to do something.

Steve

slipstick:
The result of an analogRead(A0) is an integer between 0 and 1023. If you think you've read a voltage then you're imagining things. It's just an integer so trying to compare it with .4V which is very definitely not an integer is never going to work.

You could work out what number you actually read when the voltage is 4.7V and from that you could work out what the number should be for say 10% (1/10th) of that and then use those numbers to do something.

Steve

slipstick:
The result of an analogRead(A0) is an integer between 0 and 1023. If you think you've read a voltage then you're imagining things. It's just an integer so trying to compare it with .4V which is very definitely not an integer is never going to work.

You could work out what number you actually read when the voltage is 4.7V and from that you could work out what the number should be for say 10% (1/10th) of that and then use those numbers to do something.

Steve

im sorry i still dont uderstand , do you have an example?

Imagine the left LDR is illuminated more brightly than the right, and brighter illumination results in a larger value returned by analogRead

Let's say left LDR analogRead returns 900, and right LDR analogRead returns 750.

Now, can you figure it out?

i understand what you are saying about 750 and 900 but would it work to say,
move servo when LDRL is 200 more than LDRR.( I only want the servo to move from one side to another if there is a significant difference like (200) higher than the other side

{
if(analogRead(LDRL) > 200> analogRead(LDRR) ) {
myservo.write(0); // move to angel 80 degrees or what ever you want
}
Sorry if im not getting it.

 if(analogRead(LDRL) > 200>  analogRead(LDRR) ) {

If you're wondering why this dosn't work, let's analyse why.

The expression is evaluated left-to-right.
So, is analogRead(LDRL) greater than 200.
Let's say it is - result true, aka 1.
Is 1 greater than analogRead (LDRR) ?

Unlikely.

AWOL:

 if(analogRead(LDRL) > 200>  analogRead(LDRR) ) {

If you're wondering why this dosn't work, let's analyse why.

The expression is evaluated left-to-right.
So, is analogRead(LDRL) greater than 200.
Let's say it is - result true, aka 1.
Is 1 greater than analogRead (LDRR) ?

Unlikely.

I see what you are saying, i get what i want to do but dont know how to do it

Your analogReads return values between 0 and 1023.

Read both values into variables (e.g. leftLDR and rightLDR).

If leftLDR reading is greater than (rightLDR reading + 100) then move the arm to the left.

If rightLDR reading is greater than (leftLDR reading + 100) then move the arm to the right.

(100 was chosen as an arbitrary offset as it is roughly 10% of 1023 i.e. full scale. You may want to scale this depending on the maximum light available).

The effect of these three statements is that if the readings from the two analog channels are within 100 of each other no change will be made to the state of the arm.

Ian