// limitations:
// no debounce, but this should not be a problem
// in case of very short press, risk of confusion on the buttons (maybe it will be almost never seen)
// only one button pressed at a time
#define DELAY_TIMER_BTN B00000001 // PCF pin 0 (write B00000001 or 1)
#define PROG_SEL_BTN B00000010 // PCF pin 1 (write B00000010 or 2)
#define START_BTN B00000100 // PCF pin 2 (write B00000100 or 4)
unsigned long prevmillis; // tempo
byte dataFromPCF; // PCF result
void setup() {
PORTC |= B00000111;
Serial.begin(115200);
Serial.println(F("go ! press a button"));
}
byte readDataFromPCF() {
byte lowerByte = ~PINC & B00000111;
return lowerByte; // PCF return positive logic (1 when pressed, pulldown on pin)
}
void function1(){ // here your function when DELAY_TIMER_BTN is pressed
Serial.println(F("DELAY_TIMER_BTN just pressed")); //
} //
void function2(){ // here your function when PROG_SEL_BTN is pressed
Serial.println(F("PROG_SEL_BTN just pressed")); //
} //
void function3(){ // here your function when START_BTN is pressed
Serial.println(F("START_BTN just pressed")); //
} //
void isButtonPressed() {
if (millis() - prevmillis > 1) { // wait for 50 ms period
prevmillis = millis(); // 50ms reached
dataFromPCF <<= 1; // left shift
dataFromPCF += readDataFromPCF(); // add button status
if (dataFromPCF == DELAY_TIMER_BTN) function1(); //
if (dataFromPCF == PROG_SEL_BTN) function2(); //
if (dataFromPCF == START_BTN) function3(); //
}
}
void loop() {
isButtonPressed();
//...
}