// 3 stages vertical counter visualized by leds and list of bits (buttons status)
// See the vertical counter, who is an horizontal counter due to print limitations
// the 2 buttons (0 and 1) simulate an unique input pin receiving a 0 or 1 status
//
// 1) START the WOKWI simulation (green button) to make the terminal apparent
// 2) Adjust the image (move left/right) for leds to fit with the terminal columns
// by playing on the wheel, and enlarge/shrink the image.
// 3) Move the image up (place the mouse in a gray area, left click and move)
// 4) Expand the terminal window upwards (with the mouse and left click)
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
byte Released=0, Toggled= 0; // released and toggled ("flip-flop")
byte timeBase = millis();
bool PressedBlink = 0;
void setup(void) { //
DDRD |= 0B11111100; // [PORTD 7..2] set as output
DDRB |= 0B00011111; // [PORTB 4..0] set as output
PORTC |= 0B00000011; // [PORTC 1..0] with internal pullups
Serial.begin(115200); //
while (!Serial); // when USB onboard
simul(0); // make the window of terminal visible
}
void loop(void) { // 3 polling PORTC & delay(6) = 18 ms
if ((millis() & 0xFF)-timeBase > 6) { // 6 ms*3 loops = 18 ms debounce time
timeBase = millis(); // ATTN !!! loop time limited to 65 s
InPort1 = ~PINC & 0B00000011; // inverted & masked raw PORTC A1..A0
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)
Released = (Pressed ^ PresSav) & PresSav; // 8 (16) buttons just released (off)
Toggled ^= Clicked ; PresSav = Pressed; // 8 (16) flip-flop (toggle function)
InPort3 = InPort2 ; InPort2 = InPort1; // shift/memorise status of raw PORTC
if (0 != (Clicked & 1)) simul(0); // A0 (bin value=1) has been clicked
if (0 != (Clicked & 2)) simul(1); // A1 (bin value=2) has been clicked
} //
// Goodies
if (0 != PressedBlink) digitalWrite (5, ((millis() & 128) == 128)) ; // if activated, mauve led (key Pressed) blinks
else digitalWrite (5, 0);
if (0 != Clicked) {
if (false == PressedBlink) tone(12, 440, 8); // debounce not complete, booooop
else tone(12, 880, 12); // debounce succesfull, Biiiiiiiip
}
}
//------------------- END OF THE REAL PROGRAM ------------------------------------
//------------------ SIMULATION : SAME PROGRAM AS ABOVE --------------------------
// the real program loops permanently ; the simulation is activated once by clic
// which allows displaying the state of variables when a simulated key is pressed
byte XnPort1= 0, XnPort2= 0, XnPort3= 0, XnPort4= 0; // 3 stages vertical counter
byte XortAnd= 0, XortOr = 0; //
byte Xlicked= 0, Xressed= 0, XresSav= 0; //
byte Xeleased=0, Xoggled= 0; //
void simul (byte simulInput) { // same code, but variables renamed
XnPort1 = simulInput; //
XortAnd = XnPort1 & XnPort2 & XnPort3 & XnPort4; // 3 stages vertical counter for demo
XortOr = XnPort1 | XnPort2 | XnPort3 | XnPort4; // 3 stages vertical counter for demo
Xressed = XortOr & (XortAnd | Xressed); // 8 (16) buttons pressed & debounced
Xlicked = (Xressed ^ XresSav) & Xressed; // 8 (16) buttons just clicked (down)
Xeleased = (Xressed ^ XresSav) & XresSav; // 8 (16) buttons just released (off)
Xoggled ^= Xlicked ; XresSav = Xressed; // 8 (16) flip-flop (toggle function)
//XnPort4=XnPort3; XnPort3=XnPort2; XnPort2=XnPort1; // must be executed after the prints
Serial.print (" "); Serial.print ((XnPort1 & 0x1),BIN);
Serial.print (" "); Serial.print ((XnPort2 & 0x1),BIN);
Serial.print (" "); Serial.print ((XnPort3 & 0x1),BIN);
Serial.print (" "); Serial.print ((XnPort4 & 0x1),BIN);
Serial.print (" "); Serial.print ((XortAnd & 0x1),BIN);
Serial.print (" "); Serial.print ((XortOr & 0x1),BIN);
Serial.print (" "); Serial.print ((Xressed & 0x1),BIN);
Serial.print (" "); Serial.print ((Xlicked & 0x1),BIN);
Serial.print (" "); Serial.print ((Xeleased & 0x1),BIN);
Serial.print (" "); Serial.println((Xoggled & 0x1),BIN);
if (0 != (XnPort1 & 0x1)) PORTB |= 0x08; else PORTB &= ~0x08; // jour nuit
if (0 != (XnPort2 & 0x1)) PORTB |= 0x04; else PORTB &= ~0x04; // jour nuit
if (0 != (XnPort3 & 0x1)) PORTB |= 0x02; else PORTB &= ~0x02; // jour nuit
if (0 != (XnPort4 & 0x1)) PORTB |= 0x01; else PORTB &= ~0x01; // jour nuit
if (0 != (XortAnd & 0x1)) PORTD |= 0x80; else PORTD &= ~0x80; // jour nuit
if (0 != (XortOr & 0x1)) PORTD |= 0x40; else PORTD &= ~0x40; // jour nuit
// if (0 != (Xressed & 0x1)) PORTD |= 0x20; else PORTD &= ~0x20; // jour nuit
if (0 != (Xressed & 0x1)) PressedBlink = 1; else PressedBlink = 0; // jour nuit rapidos
if (0 != (Xlicked & 0x1)) PORTD |= 0x10; else PORTD &= ~0x10; // jour nuit
if (0 != (Xeleased & 0x1)) PORTD |= 0x08; else PORTD &= ~0x08; // jour nuit
if (0 != (Xoggled & 0x1)) PORTD |= 0x04; else PORTD &= ~0x04; // jour nuit Jacqouille, ça suffit
XnPort4 = XnPort3; XnPort3 = XnPort2; XnPort2 = XnPort1; // must be executed after the prints
}
// https://wokwi.com/projects/480704385877544961
1
0
InPort2
InPort3
InPort4
1234 anded
1234 ored
Pressed
Clicked
Released
Toggled
(debounced)
limitation resist. on leds and
protection on buttons not drawn
Rule :
4 succesive clics
InPort1