Go Down

Topic: [HELP] 4 button control 4 led delay with different frequency (Read 150 times) previous topic - next topic

Solas93

Dear everyone,
My name's Vu, I come from Viet Nam and I'm a newbie. I'm studying the tutorials and trying to make a program with 4 buttons and 4 leds.
My teacher's requests are:
- When pressing button 1(not hold), led 1 turns on in 1s then turn off in 1s, after 6 times repeated, it turns on in 3s then turns off in 3s. The loop continues until I press another button
- When pressing button 2(not hold), led2 turns on in 2s then turns off in 2s, 5 times repeated, then turn on in 5s and turn off in 5s. The loop continues until I pressing another button
- Press button 3, led3 turn on; press it again, led3 turns off
- Press and hold button 4, 4 leds turn on, release it, 4 leds turn off
I can write program with simple commands I get from Tutorials but I have troubles with loops. When I press button 1, I write a loop for flashing. But I cannot stop this loop when I press button2 or button3, button4. The same happens to other buttons. Here is my program; please take a look and help me to solve it:  :)

Code: [Select]
int button1 = 1;
int button2 = 2;
const int button3 = 3;
int button4 = 4;
int led1 = 6;
int led2 = 7;
const int led3 = 8;
int led4 = 9;
int button3PushCounter = 0;
int button3State = 0;
int lastButton3State = 0;
int flag = 0;

void setup() {
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(button4, INPUT);
pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);
pinMode(led3,OUTPUT);
pinMode(led4,OUTPUT);
}

void loop() {
int i;
int button1Status = digitalRead(button1);
int button2Status = digitalRead(button2);
int button4Status = digitalRead(button4);

if (button1Status == HIGH && (flag != 0 ? flag == 1 : true)) { // Nếu button1 bị nhấn
 flag = 1;
 LOOP1:
   for (i = 1; i<=6; i=i+1){
 digitalWrite(led1,HIGH);
 delay(1000);
 digitalWrite(led1,LOW);
 delay(1000);
   }
 digitalWrite(led1,HIGH);
 delay(3000);
 digitalWrite(led1,LOW);
 delay(3000);
 goto LOOP1;  
}

if (button2Status == HIGH && (flag != 0 ? flag == 2 : true)) { // Nếu button2 bị nhấn
   flag = 2;
   LOOP2:
   for (i = 1; i<=7; i=i+1){
 digitalWrite(led2,HIGH);
 delay(2000);
 digitalWrite(led2,LOW);
 delay(2000);
 }
 digitalWrite(led2,HIGH);
 delay(5000);
 digitalWrite(led2,LOW);
 delay(5000);
 goto LOOP2;
 }

 BUTTON3:
  button3State = digitalRead(button3);
if (button3State != lastButton3State) {
if (button3State == HIGH) {
 button3PushCounter++;
}
lastButton3State = button3State;
if (button3PushCounter % 2 == 1) {
digitalWrite(led3, HIGH);
} else {
 digitalWrite(led3, LOW);
}
}
}


I'm looking forward to hearing from you

Grumpy_Mike

Read the how to use this forum stick post it will tell you how to post code.
NEVER use a goto statement, that way madness lies.

Break each action into it's own function. Have the main loop just look at the condition of the push buttons and call the appropriate function.

Once some led is blinking all the buttons will be ignored until that blinking is over. If you do not want that sort of action then you have to program it as a state machine, that is much more complex.

Again remove those goto statements.

Go Up