Austin, TX USA
Offline
God Member
Karma: 3
Posts: 992
Arduino rocks
|
 |
« Reply #3 on: November 09, 2008, 12:17:25 pm » |
Ex--
The code below tracks the state of the 5 pins and makes a note of which was pressed last. The "button_down" variable stores the most recently pressed button pin number (or -1 if none are pressed). In the case where button 1 is pressed and held then button 2 pressed and released, it does not record that button 1 is still active. If this is a problem, it can be tweaked.
Is this something like what you were hoping for?
Mikal
byte pin_state[5] = {HIGH, HIGH, HIGH, HIGH, HIGH}; byte switch_pins[5] = {2, 3, 4, 5, 6}; unsigned long last_push_time[5] = {0, 0, 0, 0, 0}; int button_down = -1;
void setup() { for (int i=0; i<5; ++i) pinMode(switch_pins, INPUT); }
void loop() { unsigned long now = millis(); for (int i=0; i<5; ++i) { byte newstate = digitalRead(switch_pins); if (newstate != pin_state) // did the pin change state? { if (newstate == HIGH && now - last_push_time > 200) // debouncing { pin_state = HIGH; if (button_down == switch_pins) button_down = -1; } else if (newstate == LOW) { pin_state = LOW; button_down = switch_pins; last_push_time = now; } } } /* At this point the variable "button_down" contains the pin number for the most recently pressed button, or -1 if no buttons are down. This assumes that a button press causes the pin state to drop "LOW". */ }
|