// Mixing mechButton with timeObj. All of this is
// totally NON BLOCKING.
//
// Hide the mess of millis.
//
// You will need to install LC_baseTools to compile
// this on your machine.
#include <timeObj.h> // Timer object.
#include <mechButton.h> // pin manager, debouncing, calling functions..
#define MAX_COUNT 122
timeObj timerOne(1000,false);
timeObj timerTwo(1000,false);
mechButton triggerOne(2);
mechButton triggerTwo(3);
int count1;
int count2;
void setup() {
Serial.begin(115200);
Serial.println("Green button triggers timer 1, Blue does timer 2.");
triggerOne.setCallback(trigOneClk); // When channged, call this function.
triggerTwo.setCallback(trigTwoClk); // When channged, call this function.
count1 = 0;
count2 = 0;
}
// A changed occorred with triggerOne's pin. Check it out.
void trigOneClk(void) {
if (!triggerOne.getState()) {
count1 = 0;
timerOne.start();
}
}
// A changed occorred with triggerTwo's pin. Check it out.
void trigTwoClk(void) {
if (!triggerTwo.getState()) {
count2 = 0;
timerTwo.start();
}
}
// Mr loop(). Make sure idle() is called for the triggers.
void loop() {
idle(); // Run the mnagic.
if (timerOne.ding()) { // If timerOne has expired..
count1++; // Bump up count1.
Serial.print("Count 1 : "); // Tell the user about it.
Serial.println(MAX_COUNT-count1); //
if (count1==MAX_COUNT) { // If this was the last count..
timerOne.reset(); // Turn off the timer.
} else { // Else, not thelast..
timerOne.stepTime(); // Set the timer for the next count.
} //
} //
if (timerTwo.ding()) { // If timerTwo has expired..
count2++; // Bump up count2.
Serial.print("\t\tCount 2 : "); // Tell the user about it.
Serial.println(MAX_COUNT-count2); //
if (count2==MAX_COUNT) { // If this was the last count..
timerTwo.reset(); // Turn off the timer.
} else { // Else, not thelast..
timerTwo.stepTime(); // Set the timer for the next count.
}
}
}