Hi,
For my project I need 4 push buttons, but each button must have two functions:
When you press button 1 shortly (+/- 1 sec) do something, when you push the same button more then 3 sec, do something else..
I also make use of the debounce functions, and I have already this phase of code:
#define DEBOUNCE 10 // button debouncer, how many ms to debounce, 5+ ms is usually plenty
// Here is where we define the buttons that we'll use. button "1" is the first, button "6" is the 6th, etc
byte buttons[] = {6, 7, 8, 9};
// This handy macro lets us determine how big the array up above is, by checking the size
#define NUMBUTTONS sizeof(buttons)
//track if a button is just pressed, just released, or 'currently pressed'
byte pressed[NUMBUTTONS], justpressed[NUMBUTTONS], justreleased[NUMBUTTONS];
byte previous_keystate[NUMBUTTONS], current_keystate[NUMBUTTONS];
byte thisSwitch=thisSwitch_justPressed();
switch(thisSwitch){
case 0:
Serial.print("button 3 just pressed);
doButton1();
break;
case 1:
doButton2();
Serial.println("button 3 just pressed");
Break;
case 2:
Serial.println("button 3 just pressed");
doButton3();
break;
case 3:
Serial.println("button 3 just pressed");
doButton4();
break;
}
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
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)) {
justreleased[index] = 1; // just released
}
pressed[index] = !currentstate[index]; //remember, digital HIGH means NOT pressed
}
previousstate[index] = currentstate[index]; //keep a running tally of the buttons
}
}
byte thisSwitch_justPressed() {
byte thisSwitch = 255;
check_switches(); //check the switches & get the current state
for (byte i = 0; i < NUMBUTTONS; i++) {
current_keystate[i]=justpressed[i];
if (current_keystate[i] != previous_keystate[i]) {
if (current_keystate[i]) thisSwitch=i;
}
previous_keystate[i]=current_keystate[i];
}
return thisSwitch;
}
Can anyone help me to make the two different functions with one button?