Hi iam newbie here..I have two button,s1 and s2 both are now HIGH STATE . but each one pressed in different time..how to detect which one press first.could you please help me ?
How do you detect which of two lights turns on first?
You look alternating at light A and light B as fast as you can and the moment you see a light turn on you know that's the first. Same in code
..I have two button,s1 and s2 both are now HIGH STATE . but each one pressed in different time..how to detect which one press first.could you please help me ?
I assume they go low when you push the button and I assume you (your software) can read both switches?
The easiest thing would be to read them in a "fast loop" with two [u]if-statements[/u]. Then, if one reads low you "do something" and that one was pressed first.
If the other one reads low, you "do something else" and that one was pressed first.
Depending on what you want to do, you might need to "lock-in" a variable value telling you that the switch was pressed and you might need to skip-over the button reading as the looping continues (another if-statement?).
A more advanced approach would be to use interrupts. (With an interrupt you don't have to wait for the loop to come-around to read the button.)
Use the millis() function to record the time and compare which one was pressed first based on the time difference
int buttonPin1 = 2;
int buttonPin2 = 3;
boolean button1 = false;
boolean button2 = false;
boolean noEnterAgain_bt1 = false;
boolean noEnterAgain_bt2 = false;
void setup() {
Serial.begin (9600);
pinMode (buttonPin1, INPUT_PULLUP);
pinMode (buttonPin2, INPUT_PULLUP);
}
void loop() {
button1 = digitalRead (buttonPin1);
button2 = digitalRead (buttonPin2);
if (button1 == LOW && button2 == HIGH && noEnterAgain_bt1 == false) {
Serial.println ("BUTTON 1 FIRST");
noEnterAgain_bt1 = true; noEnterAgain_bt2 = true;
}
if (button2 == LOW && button1 == HIGH && noEnterAgain_bt2 == false) {
Serial.println ("BUTTON 2 FIRST");
noEnterAgain_bt2 = true; noEnterAgain_bt1 = true;
}
}