Why does this not loop?  please!!

Can anybody tell me why this sketch does not loop and detect changes in light levels - please!! The delays in the loop to the servo is to slow down its repositioning and works that way if I can get the main loop to work.

// Controlling a servo position using a potentiometer (variable photoresistor) 


#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 
 
int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin 
int valold;  //updates val 

void setup()
{valold=90;
 myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
} 
 
void loop()
{  
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023) 
  val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180)

 if (val > valold);
  for (val=valold; val!=valold; val++)
  {myservo.write(val); delay (150);}
  
 if (val < valold);
  for (val=valold; val!=valold; val--)
  {myservo.write(val); delay (150);}

(valold=val);

delay (3000);

             
}
if (val > valold);

val and valold are compared. If val is greater than valold, nothing happens.

for (val=valold; val!=valold; val++)

The body of this code will be executed once. You move the servo to some position.

Then you have another do nothing statement, followed by another block whose body is executed once, in which you move the servo to the same position it is currently in.

Then, you have this:

(valold=val);

What are the parentheses for? They don't hurt anything, but, they don't do anything, either (except scream newbee).

Look at the example in the servo library. It properly moves a servo based on a potentiometer setting.

To Paul S

Thanks for your reply but what of the following -

for if (val>valold you say “val and valold are compared. If val is greater than valold, nothing happens.”

  • surely if val IS greater than valold then the next “for” statement surely does happen - does it not!!???

The next if statement and body (noe corrected!) covers the opposite case - i.e. for val<valold - so one or other of the if statements must be true

The point of

if (val < valold);
for (val=valold; val!=valold; val–)
{myservo.write(val); delay (150);}

is to move the servo in single steps with 150ms delay between steps and from position valold to position val - Is this wrong?

The published code of course works but the object here is to slow the servo motor down - AND it does slow down with the delays inserted if I put specific numbers in for val and valold - so why doesn’t it for the variables???

remove the ; at the end of the if statements

if (val < valold); //remove this semicolon
for (val=valold; val!=valold; val–)
{myservo.write(val); delay (150);}

There are a lot more problems with the code than just the extra ;. OP sent me a PM that I replied to with an explanation of the remaining problems and suggestions for correcting them.