I finally threw this into setup to make it stop. I am on Attiny so I can't debug my vars. noob
int buz = 3;
int j = 0;
void setup() {
pinMode(buz, OUTPUT);
delay(500); // no action for several seconds
do {
digitalWrite(buz, HIGH);
delay(10);
digitalWrite(buz, LOW);
delay(500);
j = j + 1;
} while (j < 3);
}
void loop() {
}
What gives
delay(500); // no action for several seconds
is that an observation, or what you hoped would happen?
I finally threw this into setup to make it stop.
It stops in loop() as well, it is just that it starts up again immediately in loop(), the clue as to why is in the name of the function.
When it is in void loop()
Why does the global variable j reset itself to 0?
If I meet the conditional why does it sart over? Is j not incrementing?
if j incremented to 3 and the loop started over isn't j still 3 and the while statement null. I want this thing to fire 4 times and stop.
Arrch
July 6, 2012, 9:36pm
9
rheine:
if j incremented to 3 and the loop started over isn't j still 3 and the while statement null. I want this thing to fire 4 times and stop.
Yes, but when you put
do {
...
}
it tells it to do it at least once, and then check for that condition.
system
July 6, 2012, 9:38pm
10
OP: I think you're confusing a while loop with a do...while loop.
Look where the test and the increment are.
rheine
July 6, 2012, 9:42pm
11
oooooh, OK thanks. I am used to do until
system
July 6, 2012, 9:46pm
12
Do..while will always execute the body of the loop at least once because the test comes after the body.
A while loop tests before the body is executed.