Hi guys,
I am a beginner and have 2 problems with the code below. I hope that you help me solving them:
1- When button1 or 2 is pressed and released, LED1 or 2 shouldn't remain HIGH. I need the LED to turn LOW after a delay of certain time, e.g. 30sce. It also should return LOW if the button is pushed again before the delay time is up.
2- When button1 is pressed and released, LED1 goes HIGH after the delay time(3000). The problem is that button2 does not work to turn LED2 HIGH or LOW until after that delay time is up. I need to control each LED separately.
Any help would be much appreciated!
Many thanks, ![]()
#define LED1 8
#define LED2 13
#define BUTTON1 7
#define BUTTON2 6
int val1 = 0;
int val2 = 0;
int old_val1 = 0;
int old_val2 = 0;
int state1 = 0;
int state2 = 0;
void setup() {
pinMode(LED1, OUTPUT);
pinMode(BUTTON1, INPUT);
pinMode(LED2, OUTPUT);
pinMode(BUTTON2, INPUT);
}
void loop(){
val1 = digitalRead(BUTTON1);
val2 = digitalRead(BUTTON2);
if ((val1 == HIGH) && (old_val1 == LOW)){
state1 = 1 - state1;
delay(3000);
}
if ((val2 == HIGH) && (old_val2 == LOW)){
state2 = 1 - state2;
delay(300);
}
old_val1 = val1;
if(state1 == 1) {
digitalWrite(LED1, HIGH);
} else {
digitalWrite(LED1, LOW);
}
old_val2 = val2;
if(state2 == 1) {
digitalWrite(LED2, HIGH);
} else {
digitalWrite(LED2, LOW);
}
}