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
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:
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
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.
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.