For statement, decrement not working

#include <Servo.h>

int pos1 = 0;
Servo myservo;

void setup() {
// put your setup code here, to run once:
myservo.attach(3);
myservo.write(180);
Serial.begin(9600);
delay(200);
Serial.print("Hello\n");
}

void loop() {
//put your main code here, to run repeatedly:

for(int pos=50; pos <= 0; pos -= 10){
//myservo.write(pos);
delay(200);
Serial.println(pos);
}
////////// => not Working. nothing is printed

for(int pos=0; pos <= 50; pos += 10){
//myservo.write(pos);
delay(200);
Serial.println(pos);
///////// => working good! output is 0,10,20,30,40,50. repeat repeatedly.
}

Why decrement do not working in above code?

Easy, here:

  for(int pos=50; pos <= 0; pos -= 10){

you start with "pos=50" but the loop condition is "pos<=0" so because 50 isn't less than zero the for won't execute anything (neither decrementing).
Change it to:

  for(int pos=50; pos >= 0; pos -= 10){
1 Like

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

Please post your full sketch, using code tags when you do

Posting your code using code tags prevents parts of it being interpreted as HTML coding and makes it easier to copy for examination

In my experience the easiest way to tidy up the code and add the code tags is as follows

Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

It is also helpful to post error messages in code tags as it makes it easier to scroll through them and copy them for examination

Thank you very much. It works. I spent 3 days for solving this.

And the next time that you make the same mistake it will only take one day. And the time after that only a few minutes :smiley:

1 Like

Learn to read the for loop to yourself or out loud if it helps. When I see

for(int pos=50; pos <= 0; pos -= 10){

to me it reads "set pos to 50 and while pos is smaller than or equal to zero execute the code in the for loop then subtract 10 from pos

The important word in there is while

2 Likes

Always remember that the for() statement:

 for(int pos=50; pos >= 0; pos -= 10) {
    // body
  }

is equivalent to:

  // Initialization
  int pos=50;
  // loop condition
  while (pos >= 0) {
    // body
    // post loop
    pos -= 10;
  }
2 Likes

that a for loop can be written as a while loop, yes.

But... if the // body has a continue statement, you will have to account for the fact that the for loop will run the iteration expression whilst in the case of while you must be sure to do it "manually".

A subtlety, and possibly an error that might be hard to find. Don't ask me how I know.

a7

2 Likes
for (int pos = 50; pos >= 0; pos -= 10)

pos	 pos–10
 50	   40
 40	   30
 30	   20
 20	   10
 10	    0
  0   -10 // stop
1 Like

loop condition might be pos > 0.
To avoid pos < 0 stop at pos <= 9 since 9 - 10 < 0.

1 Like

The condition from @docdoc stops at the right time, when pos is not >= 0

1 Like

The obverse of >= 0 is < 0, requires signed values.
The loop should end when signed integer pos <= 0 or in the case of unsigned values, == 0. Start at 50, end at 0... or less (<= 0) depending on how you count.

If those values are for servos, aren't servos 0-255? PWM?

Is there such a thing as servo pos == -10? Exit condition < 0!
There's no need if you stop at zero. Need pos > ( decrement - 1 ) to continue looping if unsigned values are used and pos -= decrement per step just to leave open the possibility for pos to be other than multiples of ten/decrement.

1 Like

It exits after testing pos < 0 so, before using pos = -10 (but it uses pos == 0)

1 Like

It does use pos == zero. It's worth using an int. I conceed.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.