Hi,
I’m currently trying to use the multi-button checker sketch from Adafruit (http://www.adafruit.com/blog/2009/10/20/example-code-for-multi-button-checker-with-debouncing/) as the foundation for a sketch I’m creating.
I’d like to use a “while” function, since I’d like to have multiple actions triggered depending on how and when a button is pressed. So, for the purposes of demonstrating what I’m trying to do, I’ve pared the sketch to be essentially the Adafruit example, with the only difference from the Adafruit is in my “loop” section. See below:
int val1 = 0;
int val2 = 0;
int val3 = 0;
int val4 = 0;
#define DEBOUNCE 10 // button debouncer// here is where we define the buttons that we’ll use. button “1” is the first, button “6” is the 6th, etc
byte buttons = {2, 3, 4};
// This handy macro lets us determine how big the array up above is, by checking the size
#define NUMBUTTONS sizeof(buttons)
// we will track if a button is just pressed, just released, or ‘pressed’ (the current state
volatile byte pressed[NUMBUTTONS], justpressed[NUMBUTTONS], justreleased[NUMBUTTONS];void setup()
{
byte i;
Serial.begin(9600);// Make input & enable pull-up resistors on switch pins
for (i=0; i< NUMBUTTONS; i++) {
pinMode(buttons*, INPUT);*
_ digitalWrite(buttons*, HIGH);_
_ }}*_void check_switches()
{
* static byte previousstate[NUMBUTTONS];*
* static byte currentstate[NUMBUTTONS];*
* static long lasttime;*
* byte index;*
* if (millis() < lasttime) {*
* // we wrapped around, lets just try again*
* lasttime = millis();*
* }*
* if ((lasttime + DEBOUNCE) > millis()) {*
* // not enough time has passed to debounce*
* return;*
* }*
* // ok we have waited DEBOUNCE milliseconds, lets reset the timer*
* lasttime = millis();*
* for (index = 0; index < NUMBUTTONS; index++) {*
* justpressed[index] = 0; // when we start, we clear out the “just” indicators*
* justreleased[index] = 0;*
* currentstate[index] = digitalRead(buttons[index]); // read the button*
_ /*
* Serial.print(index, DEC);*
* Serial.print(": cstate=");*
* Serial.print(currentstate[index], DEC);*
* Serial.print(", pstate=");*
* Serial.print(previousstate[index], DEC);*
* Serial.print(", press=");*
/_
_ if (currentstate[index] == previousstate[index]) {_
_ if ((pressed[index] == LOW) && (currentstate[index] == LOW)) {_
_ // just pressed*_
* justpressed[index] = 1;*
* }*
* else if ((pressed[index] == HIGH) && (currentstate[index] == HIGH)) {*
* // just released*
* justreleased[index] = 1;*
* }*
* pressed[index] = !currentstate[index]; // remember, digital HIGH means NOT pressed*
* }*
* //Serial.println(pressed[index], DEC);*
* previousstate[index] = currentstate[index]; // keep a running tally of the buttons*
* }*
}
void loop() {
*check_switches(); ** while (val4 == 0){*
* if (justreleased[0]) {val1 = 1;}*
* if (justreleased[1]) {val2 = 10;}*
* if (justreleased[2]) {val3 = 100;}*
val4 = val1 + val2 + val3;}
check_switches();
val1 = 0;
val2 = 0;
val3 = 0;
val4 = 0;
Serial.println(“Welcome!”);
}
[/quote]
I’m certain there’s a simpler way to do what I’m attempting, but I’d really like to stick with the “while” functionality, if possible.
Many, many thanks in advance!