Loop inside IF Statement + escaping loop ?

Hello to everyone,
im a newbie in programming.
Now the problem im having is the following.
I need to get a loop inside the if statement when pressing the button 3 times to run.
Now how do i get a loop inside an if statement, i found the while and do while methods but i am not able to get them running. One other problem is how do i escape the loop when i press the button again and the count changes to == 1 so i can switch between the count1 ,2 ,3 statements .

I hope someone can help me out because im realy stuck at the moment.
happy Greetings

Daniel :slight_smile:

const byte LED = 13;
const byte Button = A0;

byte ButtonState;
byte lastState = LOW;
byte count = 0;

void setup() {
  Serial.begin(9600);
  pinMode(LED, OUTPUT);
  pinMode(Button, INPUT);
}

void loop() {
  ButtonState = digitalRead(Button);

  if(ButtonState && ButtonState != lastState)  // button latch, no debounce needed.
  {
    if(count < 3) // This will check to see if the count is within a range of 0 - 255, and anything over that, it will reset count back to 0. Of course, this will happen anyways because count is a BYTE, and not an int or any other type.
      count += 1; // same as count = count + 5;
    else
      count = 0;
      
    
    Serial.println(count);
  } 
  lastState = ButtonState;
  

 
if(count == 1)
  { 
   analogWrite(LED, 0);

}
  
  
  
 
else if(count == 2)
{ 
   analogWrite(LED, 255);

}


else if(count ==3)

{
   analogWrite(LED, 0);
   delay(500);
   analogWrite(LED, 255);
  
}


}

You're thinking too small. You already have the loop function running over and over again. Instead of thinking about trapping you program inside a loop, think about setting up the state so that it ends up going to the same place each time the loop function runs.

Have a look at state machines. I believe there are some examples either on the IDE itself or here on the site. But a google search of state machine should get you an infinite amount of inforamtion.

suvawa:
Hello to everyone,
im a newbie in programming.
Now the problem im having is the following.
I need to get a loop inside the if statement when pressing the button 3 times to run.
Now how do i get a loop inside an if statement, i found the while and do while methods but i am not able to get them running. One other problem is how do i escape the loop when i press the button again and the count changes to == 1 so i can switch between the count1 ,2 ,3 statements .

After looking at your code I can't make sense of your comments because it looks like the code should already do what you describe - do a different thing after each button press.

You can put any code you like after an IF statement.

Perhaps you only want the code to run once when (for example) count == 3. In that case you need another variable that records the fact that the code has run. That variable could then be reset when count changes to a different value.

...R

Thanks a lot for your answers that gave me the hint what i was doing wrong.
Now i got it working, but a new problem popped up once the counter hit 3 and the LED starts blinking the counter aint responding to Button pushes.So i cant get the counter back to 1 or 2. Any ideas on where i made a mistake this time ?

many greetings
Daniel

const byte LED = 13;
const byte Button = 2;

byte ButtonState;
byte lastState = LOW;
byte count = 0;

void setup() {
  Serial.begin(9600);
  pinMode(LED, OUTPUT);
  pinMode(Button, INPUT);
}

void loop() {
  ButtonState = digitalRead(Button);

  if(ButtonState && ButtonState != lastState)  // button latch, no debounce needed.
  {
    if(count < 3) // This will check to see if the count is within a range of 0 - 255, and anything over that, it will reset count back to 0. Of course, this will happen anyways because count is a BYTE, and not an int or any other type.
      count += 1; // same as count = count + 5;
    else
      count = 0;
      
    
    Serial.println(count);

  } 
  lastState = ButtonState;
  

 
if(count == 1)
  { 
   analogWrite(LED, 0);

}
  
  
  
 
else if(count == 2)
{ 
   analogWrite(LED, 255);

}


if(count ==3)

{
   digitalWrite(LED, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second
  digitalWrite(LED, LOW);    // turn the LED off by making the voltage LOW
  delay(1000); 
    
}


}
if(count ==3)

{
   digitalWrite(LED, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second
  digitalWrite(LED, LOW);    // turn the LED off by making the voltage LOW
  delay(1000); 
    
}

Well, you probably have to push the button for 2 seconds at least, since you are delaying here.

      count += 1; // same as count = count + 5;

Not really.

Look at the demo several things at a time to see how to manage time using millis() rather than delay(). The delay() function prevents the Arduino from doing anything else and is really only suitable for quick-and-dirty test code.

...R