Skip to content

Commit

Permalink
Removing dependency on Bounce library saves 190 bytes.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
lazyatom committed Jul 26, 2012
1 parent ea9849f commit 1c0aa09
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions printer.ino
Expand Up @@ -4,7 +4,6 @@
#include <EEPROM.h>

#include <SoftwareSerial.h>
#include <Bounce.h>

// -- Settings for YOU to change if you want

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 1c0aa09

Please sign in to comment.