
Some days you really gotta wonder what the hell were you thinking and where did you leave that? Before I had decided to design the board the micro is on, I had pretty much written the code and had it all working, come time to upload the code and finishing things off, i cant for the life of me find the code anywhere on my computer.
Screws me where the hell it is and the best I could find was an early backup that would have been more work to fix than actually starting from scratch. So, starting from scratch it was going to be. Start with getting the 2 rotary encoders working followed from the 6 push buttons, then the SI5351A and finally the LCD screen. All seems simple enough, well it was going swimmingly till I started on the buttons.
|
void Do_Rotary1Button() { if (!digitalRead(Rotary1Button)) { delay(20); if (!digitalRead(Rotary1Button)) { while (!digitalRead(Rotary1Button)); Serial.println("rotary button 1 pressed"); } } return; } |
So this is my cheats way of doing key debounce, it works well and does a great job of debouncing the button so you only register one key press. But for whatever reason having 3 or more buttons functions called like that from the main loop, causes them all to fail and I have no idea why, other than for some bizarre reason it was getting locking in the while statement of the 3rd function.
I honestly have no idea why its doing that, or why it should be entering the while loop at all on the 3rd function. So I needed to find a fix, and fix it I did by writing an actual debounce library to use on buttons.
|
void Do_ModeButton() { if (Mode.read() == HIGH) { Serial.println ("Mode Button Pressed"); } return; } |
Now the button code looks a little cleaner, the read class is essentially a modification of digitalRead to use the millis timer to debounce, which is much nicer as its non blocking, unlike the delay call i was using. The difference is now I still get the odd double key, rather than always getting one per push.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
int DebounceButton::read() { if (millis()-_debounce < _debounceTime) { return LOW; } if (digitalRead(_b_pin) == HIGH) { if (millis()-_debounce > _debounceTime) { _debounce=millis(); return HIGH; } else { return LOW; } } |
So now the doing my head in is over, I can actually move on to writing the code to make each function work and do its thing, other than being a placeholder than does nothing than print to the serial monitor.
Code huh, it can be a pain in the arse.


