Unsure which "for" loop the Arduino will follow - Need Guidance

Hi All,
We are using servo motors in class (can rotate 180º) and I am trying to make a program that makes the servo rotate on its own and increase/decrease how fast it rotates based on the value from a potentiometer.

I am unsure if the program once it goes under 180º, whether it will switch back to the first “for” loop or continue with the second loop until it reaches 0.
Thanks.

for(x = 0; x < 179; x = x + potVal){
  servo1.write(x); // Sends the value of "x" to the servo
  delay(0); // Waits for 0 milliseconds
if (x >= 179) /* Checks if "x" is greater than or equal to 179, and 
if it is, it does the below commands */
for(x = 179; x >= 0; x -= potVal){
servo1.write(x); // Sends the value of "x" to the servo
delay(0); // Waits for 0 milliseconds

Delay(0) seems a bit odd. Don't you have an Arduino and servomotor ready to try your program out?

Unfortunately that is at school, and this program is due tonight. I just realised that I might be able to do it with counters, but at school, it wasn't working, so I have been troubleshooting.

Do I need a delay then?

Delay(0) is the same as not writing a line of code.

But what you're basically trying to program is a servo sweep with variable speed? Arduino has a builtin example called sweep, does the same as you're trying to do. Just change the variable that controls speed to the value of the potentiometer.

Thanks. This really helped.

  • SurvivingJ

BTW the program you made wasn't finished, it was missing some ending brackets!

Thanks for pointing that out. I have fixed it now. Thanks again for the help.

  • SurvivingJ

SurvivingJ:
Unfortunately that is at school, and this program is due tonight. I just realised that I might be able to do it with counters, but at school, it wasn't working, so I have been troubleshooting.

Not with that code.