// BUTTONS : 1 to 16
// reliable debounce
// buttons info : state, fronts down & up, toggle
// multiple buttons can be pressed at a time
#define DELAY_TIMER_BTN B00000001 // Button on A0
#define PROG_SEL_BTN B00000010 // Button on A1
#define START_BTN B00000100 // Button on A2
unsigned long prevmillis; // for debounce period
unsigned char prevData; // previous reading of buttons
unsigned char prevSta; // Button previous state
unsigned char actSta; // Button state (pressed, no pressed)
unsigned char actTog; // Toggle
unsigned char actOn; // down front
unsigned char actOff; // up front
void setup() { // pullups on pins A0,A1,A2
PORTC |= B00000111; // A3, A4, A5 as output
DDRC |= B00111000;
Serial.begin(115200); //
Serial.println(F("go ! press a button")); //
} //
void isButtonPressed() {
static unsigned int counter;
// Serial.print(counter); Serial.print(" ");
counter++;
unsigned long now = millis(); // or use global "now"
if (now - prevmillis < 25) return; // too soon to even look again
prevmillis = now; // 25 ms debounce delay reached
unsigned char newData = ~PINC & B00000111; // 0 when pressed, internal pullup on pins A0, A1, A2
// this section reacts immediatley to a new different reading
actOn = newData & ~prevData; // calculate PRESSED transitioning bits
actOff = ~newData & prevData;
actTog ^= actOn; // apply TOGGLE
if (actOn & DELAY_TIMER_BTN) {Serial.print(counter); Serial.println(F(" DELAY pressed ")); }
if (actOn & PROG_SEL_BTN) {Serial.print(counter); Serial.println(F(" PROG pressed ")); }
if (actOn & START_BTN) {Serial.print(counter); Serial.println(F(" START pressed ")); }
if (actOff & DELAY_TIMER_BTN){Serial.print(counter); Serial.println(F(" released DELAY")); }
if (actOff & PROG_SEL_BTN) {Serial.print(counter); Serial.println(F(" released PROG")); }
if (actOff & START_BTN) {Serial.print(counter); Serial.println(F(" released START")); }
digitalWrite(A5, actTog & START_BTN); // display red led toggle START/STOP
// this section below develops the idea of a stable press (same for 2 readings)
unsigned char staBits = ~(newData ^ prevData);
actSta &= ~staBits; // knock out the old stable bit readings
actSta |= newData & staBits; // and jam the stable readings in there
digitalWrite(A4, actSta & (PROG_SEL_BTN | DELAY_TIMER_BTN)); // one or both, green led ON
// and could be used with for a fussier actOn / actOff / actTog by basing them on stable state readings
unsigned char fussyActOn = actSta & ~prevSta;
unsigned char fussyActOff = ~actSta & prevSta;
if (fussyActOn & DELAY_TIMER_BTN) {Serial.print(counter); Serial.println(F(" verified fussy DELAY"));}
if (fussyActOff & DELAY_TIMER_BTN) {Serial.print(counter); Serial.println(F(" released fussy DELAY"));}
// always...
prevData = newData; // bump along the history
prevSta = actSta; // bump along the history
}
void loop() { //
isButtonPressed(); //
// delay(250);
} //