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);
}