# define CHECK_INTERVAL 50
# define startStopPin 7
# define runningLED 6
# define runningTooLED 5
void setup() {
Serial.begin(115200);
Serial.println("Hello World!\n");
pinMode(startStopPin, INPUT_PULLUP);
pinMode(runningLED, OUTPUT);
pinMode(runningTooLED, OUTPUT);
}
bool running;
bool runningToo;
bool haveSaid;
unsigned long onInsant, onSluggush;
void loop()
{
loopOne();
loopTwo();
if (running && runningToo && !haveSaid) {
Serial.print(" sluggish : ");
Serial.print(onSluggush - onInsant);
Serial.println(" milliseconds later.");
haveSaid = true;
}
if (!running && !runningToo) haveSaid = false;
}
void loopOne() {
static unsigned long tmrCheck;
static bool startStopStatus;
static bool previousState = HIGH;
unsigned long currentMillis = millis();
if (currentMillis - tmrCheck > CHECK_INTERVAL) {
startStopStatus = digitalRead(startStopPin);
if (startStopStatus != previousState) {
if (previousState == HIGH)
running = !running;
if (running) onInsant = currentMillis;
tmrCheck = currentMillis;
previousState = startStopStatus;
}
}
digitalWrite(runningLED, running);
}
void loopTwo() {
static unsigned long tmrCheck;
static bool startStopStatus;
static bool previousState = HIGH;
unsigned long currentMillis = millis();
if (currentMillis - tmrCheck >= CHECK_INTERVAL) {
// It's time to check the Start/Stop button!
startStopStatus = digitalRead(startStopPin);
// Changed? Toggle the "Running" state
if (startStopStatus != previousState && previousState) {
runningToo = !runningToo;
if (runningToo) onSluggush = currentMillis;
}
// Next check
tmrCheck = currentMillis;
previousState = startStopStatus;
}
digitalWrite(runningTooLED, runningToo);
}