I wanna do a project. In this project, there are 2 buttons and 1 led. If You press one of buttons, led should not work. If You press one of buttons and press another one in 0.5 seconds, led can work. But If You press only one button or you press second one after 0.5 seconds, led should not work.
But In my codes, I press buttons and remove my hands from buttons back from buttons, led still works. Led should work only during pressing buttons time.
boolean waitingForSecondButton = false;
byte button1=8;
byte button2=9;
int currentButton1;
int previousButton1;
long firstButtonPressTime;
long currentButton2State;
long previousButton2State;
long secondButtonPressTime;
byte led=3;
void setup()
{
Serial.begin(115200);
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
pinMode(3,OUTPUT);
}
void loop()
{
currentButton1 = digitalRead(button1);
if (currentButton1 == LOW && previousButton1 == HIGH)
{
firstButtonPressTime = millis();
waitingForSecondButton = true;
}
previousButton1 = currentButton1;
currentButton2State = digitalRead(button2);
if (currentButton2State == LOW && previousButton2State == HIGH && waitingForSecondButton == true)
{
secondButtonPressTime = millis();
waitingForSecondButton = false;
if (secondButtonPressTime - firstButtonPressTime <= 500 && waitingForSecondButton == false)
{
digitalWrite(3,HIGH);
delay(3000);
digitalWrite(3,LOW);
}
}
previousButton2State = currentButton2State;}
GreyArea:
So... two buttons, one LED.
Press either button on its own, no effect
Press either button 1 then 2 or 2 then one within 500 ms and the LED comes on?
For how long?
Do you need to hold both buttons down, or will a quick press do?
When button 1 or 2 is pressed, the LED will be active as long as the other button is pressed within 500ms.