Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Removing dependency on Bounce library saves 190 bytes. #34

Open
wants to merge 1 commit into from

2 participants

@lazyatom
Owner

I've copied the implementation of a software debouncer from the
Arduino website1. Using Arduino 1.0.1, before this commit the
sketch size is 29,906 bytes, and after it is 29,716 bytes.

However, the main benefit of this commit is that no non-standard
libraries are required to compile the sketch.

@lazyatom lazyatom Removing dependency on Bounce library saves 190 bytes.
I've copied the implementation of a software debouncer from the
Arduino website[1]. Using Arduino 1.0.1, before this commit the
sketch size is 29,906 bytes, and after it is 29,716 bytes.

However, the main benefit of this commit is that no non-standard
libraries are required to compile the sketch.

[1]: http://arduino.cc/playground/Learning/SoftwareDebounce
1c0aa09
@lazyatom
Owner

If we merge this, we should also update the instructions on the wiki (which mention, I believe, installing the Bounce library).

@chrisroos

Do we need to worry about debouncing at all? From my very limited understanding of debouncing I'm not sure it really applies to our situation. If we have a download waiting and see a button press then we'll start printing. If there are multiple other button presses almost immediately after the initial one then they'll essentially be ignored by our sketch, won't they?

@chrisroos

I just did a very scientific experiment* where I removed the debouncing code (i.e. I used digitalRead(buttonPin) to check for a button press) and sent a test print to my Printer. It downloaded successfully and I left the printer sat there with its green LED showing me that something was waiting (this was to see if we ever get false positives). Having satisfied myself that we weren't going to see any false positives, I pressed the button which sent the file to print. So, how would you feel about removing it entirely?

  • No science was involved
@lazyatom
Owner

Without wanting to question your scientific veracity*, I think I've seen false positives in the past - particularly if you somehow manage to ground a part of the board by touching it. Can you repeat your experiment and handle the board a bit while the green LED is lit?

Also, how many bytes does removing the debouncing save?

  • if you weren't wearing a white coat, it ain't science.
@chrisroos

It takes us down to 28,348 bytes (29,602 with debug). I just left the Printer with a pending print (green LED) for 30 minutes, during which time I would occasionally pick it up and generally rub my grubby hands all over it. It didn't print until I pressed the button. This was with the soldered shield. I'm now going to try with the breadboard set-up.

@chrisroos

And now I've had it sitting on the green light for 30 minutes with the breadboard. Again I've had my grubby mitts all over it and still nothing. As soon as I press the button it prints...

@lazyatom
Owner

After a bit more discussion and a bit more "science", we've come to the conclusion that debouncing is more about avoiding multiple button presses rather than preventing phantom button presses. Because subsequent button presses are ignored while the printer is printing, we don't really need to manage multiple presses, and therefore we can lose the debouncing code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 26, 2012
  1. @lazyatom

    Removing dependency on Bounce library saves 190 bytes.

    lazyatom authored
    I've copied the implementation of a software debouncer from the
    Arduino website[1]. Using Arduino 1.0.1, before this commit the
    sketch size is 29,906 bytes, and after it is 29,716 bytes.
    
    However, the main benefit of this commit is that no non-standard
    libraries are required to compile the sketch.
    
    [1]: http://arduino.cc/playground/Learning/SoftwareDebounce
This page is out of date. Refresh to see the latest.
Showing with 25 additions and 5 deletions.
  1. +25 −5 printer.ino
View
30 printer.ino
@@ -4,7 +4,6 @@
#include <EEPROM.h>
#include <SoftwareSerial.h>
-#include <Bounce.h>
// -- Settings for YOU to change if you want
@@ -278,14 +277,35 @@ inline void printFromDownload() {
}
-// -- Check for new data, print if the button is pressed.
+byte counter = 0; // how many times we have seen new value
+byte reading; // the current value read from the input pin
+byte current_state = LOW; // the debounced input value
+long timeOfLastSample = 0;
+
+bool buttonPressed() {
+ if (millis() != timeOfLastSample) {
+ reading = digitalRead(buttonPin);
+ if (reading == current_state && counter > 0) {
+ counter--;
+ }
+ if (reading != current_state) {
+ counter++;
+ }
+ if (counter >= 5) {
+ counter = 0;
+ current_state = reading;
+ return true;
+ }
+ timeOfLastSample = millis();
+ }
+ return false;
+}
-Bounce bouncer = Bounce(buttonPin, 5); // 5 millisecond debounce
+// -- Check for new data, print if the button is pressed.
void loop() {
if (downloadWaiting) {
- bouncer.update();
- if (bouncer.read() == HIGH) {
+ if (buttonPressed()) {
printFromDownload();
}
} else {
Something went wrong with that request. Please try again.