// Hide the mess of millis!
#include <mechButton.h>
#include <timeObj.h>
#include <blinker.h>
#define LED_PIN 2
#define BTN_PIN 3
#define BLINK_PER_MS 500
// ************************************************************
// Whip up a quick subclass of blinker to
// do set number of blinks.
// ************************************************************
// The class setup..
class numberedBlinks : public blinker {
public :
numberedBlinks(int inPin);
virtual ~numberedBlinks(void);
void setNumBlinks(int inNum);
virtual void pulseOff(void);
int numBlinks;
};
// The actual code..
// Constructor. Pass in pin number to base blinker is all it does.
numberedBlinks::numberedBlinks(int inPin)
:blinker(inPin) { }
// Destructor has nothing to do.
numberedBlinks::~numberedBlinks(void) { }
// New function sets the number of blinks and turns on the blinking.
void numberedBlinks::setNumBlinks(int inNum) {
if (inNum>0) { // If its a valid value.
numBlinks = inNum; // Save it off.
setOnOff(true); // Flip the ON switch.
}
}
// Inherited pulseOff. Turns us off when completed.
void numberedBlinks::pulseOff(void) {
numBlinks--; // Bump down the count.
if (numBlinks==0) { // If we hit zero..
setOnOff(false); // Flip OFF the On/Off switch.
} //
blinker::pulseOff(); // Let the function we overrode for this do it's thing.
}
// ************************************************************
// That's it for the subclass of blinker
// We return to the main program.
// ************************************************************
mechButton ourBtn(BTN_PIN); // Button manager. Debounces and does callbacks.
numberedBlinks ourBlinker(LED_PIN); // LED manager. Lets you set a # of blinks.
timeObj our3SecTimer(3000); // A 3 second timer.
int count; // A place to save the current number of button pushes.
void setup() {
Serial.begin(115200); // Fire up Serial.
ourBtn.setCallback(btnClk); // Set callback for button manager.
ourBlinker.setPeriod(BLINK_PER_MS); // Set the period for the LED manager.
ourBlinker.setPercent(50); // Set the %on for the LED manager.
count = 0; // Clear count for.. counting.
Serial.println("Button hasn't been pressed yet."); // Give the user a hint.
}
void btnClk() {
if (ourBtn.getState()) { // If the button has been released..
our3SecTimer.start(); // Start the 3 sec timer.
count++; // Bump up the count.
Serial.print("Button has been pressed "); // Amuse the user.
Serial.print(count); //
Serial.println(" times."); //
}
}
void loop() {
idle(); // Run background stuff.
if (our3SecTimer.ding()) { // Timer ran out? **ding!**
ourBlinker.setNumBlinks(count); // Tell the blinker to do it this many times.
our3SecTimer.reset(); // Reset the timer. Turn it off.
count = 0; // Reset the count for the next set.
}
}