While Statement

Hello i have the following problem with the While statement after editing the “Blink” program.

void setup() {                
  // initialize the digital pin as an output.
  // Pin 13 has an LED connected on most Arduino boards:
  pinMode(13, OUTPUT);     
}

void loop() {  

byte b = 0;
  
  while(b < 2){
  digitalWrite(13, HIGH);   // set the LED on
  delay(1000);              // wait for a second
  digitalWrite(13, LOW);    // set the LED off
  delay(1000);              // wait for a second
  b = b + 1;
  }
 
  
}

The problem in this code is that:

  1. while(b < 2) , turns out to always blink the LED
  2. while(b > 2) , The LED will not be controlled.

So the problem is in the incrediment statement if I look at this structure.
I’ve tried code like “b = b + 1;” “++b;” “b = ++b;”.
It FAILS at all ways, is it that hard for arduino to let a LED blink for just 2 times?

No, it isn't hard, but just remember that "loop", loops! (Think what happens to your while loop when b == 2)

(do you have something against "for" loops?)

How should i have the code set then? I cant but the Byte b at the setup.

You could make byte b global by declaring it before the setup() function, or you could just add a "while (1);" after your while loop (to stop loop() from looping and re-running your code).

Or you could put all your code in “setup”, which doesn’t loop.