I've seen some recent discussions about pushbuttons management here
Here's how I do it for my projects : a function placed at the bottom of the programs, a regular call and the buttons immediately processed, it's easy
// Simplest code for arduino R3 or equiv. 1..8 to 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 (0 != Clicked) {Serial.print("button clicked: "); Serial.println(Clicked);}
}
Vertical counters: Debouncing switches with vertical counters
Essentially, they let you do debounce on several (M) switches by using N variables of M bits to implement M N-bit counters.
I always thought that they were more likely to be useful on "weird" architectures like PIC8 that let you do (some) math directly on "memory", and I'm not sure that they'll be very helpful on RISC-y CPUs like an AVR...
I always thought that they were more likely to be useful on "weird" architectures like PIC8
This remark is relevant, because indeed my code above is directly derived from the programming of the 8748 microcontrolers that I did in the last century; 1K of program memory, 64 bytes of RAM (the PIC8 is a Rolls-Royce, in comparison). With this, we did many, many things; if I remember correctly, my keyboards took 10 or 12 bytes of EPROM, and 4 or 5 bytes of RAM; and with reliable operation over time, it was necessary. If you're smart, the reference of this controller will give you an idea of the years mentioned.
and I'm not sure that they'll be very helpful on RISC-y CPUs like an AVR...
Why? my code is very simple, doesn't take up space, runs quickly, doesn't need a library, and by adding a few lines, I have the release detection, a toggle function, double-speed auto-repeat, press time counting, and multiple click detection; why deprive myself of these features? to take a code that does the same thing with more space? (-; and then the Boolean logic is very interesting to use, and allows many tricks.
Funny! In the link you give me, the author gives an example of code that looks like mine; so we always approach the same result, starting from different places!
(I have seen in an other discussion your method for non-blocking delays : I have already adopted it and will use it, fine)
I was worried that the added loads and stores that you need to do math on your persisent variables (some of which may need to be "volatile", depending on ... stuff) would negate the efficiencies added by using the vertical counting method.
Moving your code to a separte function, I count up to about 50 instructions, compared to 14 instructions for Scott Dattalo's PIC code (which is also in ASM, so it's not a particularly fair comparison)...
If you start with a single button on A0, do this (with the code at the very top of this discussion): at the top of your program, write:
const boolean MyButton = A0;
The Boolean (bit) variable MyButton is now associated with pin A0
Change the name MyButton to your liking.
Now that this association is made, we can read the electrical state of the A0 pin, and put the result in a variable of our choice, in this case InPort1
Replace the line under delay(6) with:
InPort1 = digitalRead(MyButton);
And there you have it!
Warning : InPort1 is an 8-bit variable, Button_A represents only one bit. Here it works, but if you want to read 2 or more buttons, you will have to make a small adaptation. I will do it to you, if you wish
It would have helped considerably if you had posted a complete tested and working example sketch in your first post
When you correct your mistakes I suggest that you post a new reply to this topic rather than changing code and/or advice already given. By all means edit your first post and add a note that mistakes had been corrected in a later post, giving its number
I was worried that the added loads and stores that you need to do math on your persisent variables would negate the efficiencies added by using the vertical counting method.
I'm not looking for efficiency (in terms of loads and store) since I'm on an AVR, and not on a controler limited in size from the 90s, but
the beauty of using the vertical technique, and
the challenge of writing the most compact and readable code-source possible. In addition, I had fun lining up the instructions (that's why there is no define in this code).
In 7 lines of source code, I debounce 8 keys and determine the 2 states "pressed" and "clicked" (descending edge). With one more line, I detect the "release" and make a "toggle" state of the 8 keys. I like it, and the use is easy to understand:
if (Button1 == Clicked) ....
"volatile" ? But all my variables are in 8 bits ;-)
I have an idea : make a project that shows the vertical counter in action, by means of LEDs. I start
In wokwi there is the animated vertical counter, which is presented as a 4-level horizontal counter.
Start the simulation (to show the terminal) and adjust the image size and the height of the terminal window to align the LEDs with the columns like this :
just tried out scott's code on my pbp3 pic12f1822 project ,
called every 5mS in main loop it works very nicely
thanks for the info
_dbounce ;vertical counter debounce
banksel PORTA
MOVLW 2
ANDWF PORTA,W
MOVWF csa
;Increment the vertical counter
MOVF count_B,W
XORWF count_A,F
COMF count_B,F
;See if any changes occurred
MOVF csa,W
XORWF cva,W
;Reset the counter if no change has occurred
ANDWF count_B,F
ANDWF count_A,F
;Determine the counter's state
MOVF count_B,W
IORWF count_A,W
;Clear all bits that are filtered-or more accurately, save
;the state of those that are being filtered
ANDWF cva,F
XORLW 0xff
;Re-write the bits that haven't changed.
ANDWF csa,W
IORWF cva,F
RETURN
endasm
GOSUB GET_NOW
IF (NOW - keytime > 5) THEN
keytime = now
call dbounce
IF CVA == 0 then
if key1state == kidle then
key1t = now
key1state = kheld
endif
endif
endif