From 1c0aa09030a7c06ab0ae0e56ac437177946575e6 Mon Sep 17 00:00:00 2001 From: James Adam Date: Thu, 26 Jul 2012 09:42:57 +0100 Subject: [PATCH] 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 --- printer.ino | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/printer.ino b/printer.ino index c75544d..a8397e3 100644 --- a/printer.ino +++ b/printer.ino @@ -4,7 +4,6 @@ #include #include -#include // -- 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 {