Program not reacting to PushButton's value change

So I'm trying to write my first "serious" program for Arduino. I'm a complete beginner, but I do have some basic knowledge in programming and electronics.

I'm trying to make 8 LED's change their blinking pattern when the PushButton is pressed.So that would mean: one push, first pattern, another push, next pattern etc.

The problem I'm having though is that the program is reacting as if I'm not even pushing the button at all, but I checked the serial monitor, and the value is changing depending on if I'm pushing the button or not.

The code follows:

int LED1 = 1;
int LED2 = 2;
int LED3 = 3;
int LED4 = 4;
int LED5 = 5;
int LED6 = 6;
int LED7 = 7;
int LED8 = 8;
int Button = 9;
int br = 0;
int buttonstate = 0;

void setup() {
  // put your setup code here, to run once:
   pinMode(LED1, OUTPUT);
   pinMode(LED2, OUTPUT);
   pinMode(LED3, OUTPUT);
   pinMode(LED4, OUTPUT);
   pinMode(LED5, OUTPUT);
   pinMode(LED6, OUTPUT);
   pinMode(LED7, OUTPUT);
   pinMode(LED8, OUTPUT);
   pinMode(Button, INPUT);
}

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

  buttonstate = digitalRead(Button);

    
  
    

    switch(br)
    {
      case 0: 
      while(buttonstate!=HIGH)
      {
        program1();
        }
        br=1;
      break;
      
      case 1: 
      while(br==1){
      if(buttonstate==HIGH)
      {
        br=0;
        }
      else program2();
      }
      break;
    
        
      
  
 
  
    
    
    }

}
  void program1(){

     

    digitalWrite(LED1,HIGH);
    digitalWrite(LED2,HIGH);
    digitalWrite(LED3,HIGH);
    digitalWrite(LED4,HIGH);
    digitalWrite(LED5,HIGH);
    digitalWrite(LED6,HIGH);
    digitalWrite(LED7,HIGH);
    digitalWrite(LED8,HIGH);
    delay(100);
    digitalWrite(LED1,LOW);
    digitalWrite(LED2,LOW);
    digitalWrite(LED3,LOW);
    digitalWrite(LED4,LOW);
    digitalWrite(LED5,LOW);
    digitalWrite(LED6,LOW);
    digitalWrite(LED7,LOW);
    digitalWrite(LED8,LOW);
    delay(100);
   
   
    
  }
  
  void program2(){
     
    
       
    digitalWrite(LED1,HIGH);
    delay(100);
    digitalWrite(LED1,LOW);
    delay(100);
    }

You say you checked the serial monitor. How? The code you posted has no use of Serial and has an LED attached to one of the Serial pins so you couldn't use Serial right if you wanted to.

You seem to expect the button to read backwards, HIGH when pressed. Do you have an external pull-down resistor to keep it from floating when not pressed? It would be much more common to wire the button to read LOW when pressed and then you could use the internal built-in pull-up resistors on the Arduino and not need the extra parts.

Finally look here:

while(buttonstate!=HIGH)
      {
        program1();
        }

If the buttonstate wasn't HIGH to get this loop started, then how will it ever be HIGH again to get out of it? buttonstate never changes inside that loop and program1 function doesn't change it. This is an infinite loop once it enters. I would say you need to read the button inside the while loop, but what you actually need to do is to do this without the while loop entirely. Blocking code is bad.

Hey thanks for the reply, I actually checked the serial monitor after copying the code so that's why it's not in here.

As for the connectivity, I connected the button according to this scheme on the Arduino site

I was thinking that the while loop could get me into an infinite loop, I just completely overlooked the fact that it needs to be checked while inside the loop. Still, as you said, I'm gonna try to do it without the loop at all.

OK so you do have the pull-down resistor. That at least keep you from having floating pins. Still, in the future it would be easier and simpler on you to wire the button the right way round and eliminate the need for the resistor.

case 1:
while(br==1){
if(buttonstate==HIGH)
{
br=0;
}
else program2();
}
break;

You can't use "else" in a "while" which in the "case".

I'm very surprised that compiled

GrOnThOs:
You can't use "else" in a "while" which in the "case".

I'm very surprised that compiled.

I'm not... it actually looks better with proper formatting, not to say that it makes any sense...

case 1:
while (br == 1)
{
  if (buttonstate == HIGH)
  {
    br = 0;
  }
  else program2();
}
break;

But whats wrong with “else” in “while” in “case”? I’ve used that way and they all are working as they should.

surepic:
But whats wrong with “else” in “while” in “case”? I’ve used that way and they all are working as they should.

Nothing. I think someone was confused. if - else and while work perfectly fine inside a case. Only thing I know of that you can't do inside a case is create a new variable unless you use { and } to limit its scope to the case.

Nothing. I think someone was confused.

Indeed, but is it ok use "else" without open close bracket?

Yes as long as it is a single statement you don't need braces.

Thank you so much to everybody that replied, I managed to do the program without the loops,only using a switch command, the problem I was having before is that the patterns wouldn't repeat, the program would only go through a pattern once and nothing would happen again if I didn't press the button, but somehow it loops the patterns now, I have no idea why.

Now I only have to manage to debounce the button that changes the patterns, which I'm kinda stuck on.

Should I implement the debouncing into the main loop or should I make a separate function for debouncing and then call it at the beginning of the main loop?

Now I only have to manage to debounce the button that changes the patterns, which I'm kinda stuck on.

Should I implement the debouncing into the main loop or should I make a separate function for debouncing and then call it at the beginning of the main loop?

Perhaps it will help if you post the new code.

I also added a value for a potentiometer that determines the blinking speed, but that shouldn't interfere with the main program in any way.

int LED1 = 1;
int LED2 = 2;
int LED3 = 3;
int LED4 = 4;
int LED5 = 5;
int LED6 = 6;
int LED7 = 7;
int LED8 = 8;
int Button = 9;
int Pot = 0;
int val = 0;
int br = 0;
int buttonstate = 0;



void setup() {
  // put your setup code here, to run once:
   pinMode(LED1, OUTPUT);
   pinMode(LED2, OUTPUT);
   pinMode(LED3, OUTPUT);
   pinMode(LED4, OUTPUT);
   pinMode(LED5, OUTPUT);
   pinMode(LED6, OUTPUT);
   pinMode(LED7, OUTPUT);
   pinMode(LED8, OUTPUT);
   pinMode(Button, INPUT);
   
}

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

  buttonstate = digitalRead(Button);
  val = analogRead(A0);
  
  
    
  switch(br){
    
    case 0:
    if(buttonstate!=HIGH){
    program1();}
    else br=1;break;

    case 1:
  
    if(buttonstate!=HIGH){
    program2();}
    else br=0;break;
    
    }
  

  
    
        
      
  
 
  
    
    
    

}
  void program1(){

     

    digitalWrite(LED1,HIGH);
    digitalWrite(LED2,HIGH);
    digitalWrite(LED3,HIGH);
    digitalWrite(LED4,HIGH);
    digitalWrite(LED5,HIGH);
    digitalWrite(LED6,HIGH);
    digitalWrite(LED7,HIGH);
    digitalWrite(LED8,HIGH);
    delay(val);
    digitalWrite(LED1,LOW);
    digitalWrite(LED2,LOW);
    digitalWrite(LED3,LOW);
    digitalWrite(LED4,LOW);
    digitalWrite(LED5,LOW);
    digitalWrite(LED6,LOW);
    digitalWrite(LED7,LOW);
    digitalWrite(LED8,LOW);
    delay(val);
   
   
    
  }
  
  void program2(){
     
    
       
    digitalWrite(LED1,HIGH);
    delay(val);
    digitalWrite(LED1,LOW);
    delay(val);
    }