// Simplest code for arduino R3 or equiv. 1..8 or 16 buttons strongly debounced
// with use of vertical counter technique 3 status to confirm '1' or '0' inputs
byte InPort1= 0, InPort2= 0, InPort3= 0; // 2 precedents polling of input PORT
byte PortAnd= 0, PortOr = 0; // status of 3 inputs PORT anded,ored
byte Clicked= 0, Pressed= 0, PresSav= 0; // buttons clicked, pressed and saved
void setup(void) { // [PORTC 0..3] with internal pullups
PORTC |= 0B0001111; Serial.begin(9600); // chi va piano va sano, e va lontano
} // A0,A1,A2,A3 --> Buttons n° 1,2,4,8
void loop(void) { // 3 polling PORTC & delay(6) = 18 ms
delay(6); // use of delay:THIS IS A DEMO,THANKS
InPort1 = ~PINC & 0B00001111; // inverted & masked raw PORTC A0..A3
PortAnd = InPort1 & InPort2 & InPort3; // 3 last status of input PORTC anded
PortOr = InPort1 | InPort2 | InPort3; // 3 last status of input PORTC ored
Pressed = PortOr & (PortAnd | Pressed); // 8 (16) buttons pressed & debounced
Clicked =(Pressed ^ PresSav) & Pressed; // 8 (16) buttons just clicked (down)
PresSav = Pressed ; Pressed = PresSav; // save status of pressed buttons ;-)
InPort3 = InPort2 ; InPort2 = InPort1; // shift/memorise status of raw PORTC
if (Clicked != 0) {Serial.print("button clicked: "); Serial.println(Clicked);}
}
1
2
4
8