/*
Solution For:
Topics: Programming a push button to restart loop
Category: Programming Questions
Link: https://forum.arduino.cc/t/programming-a-push-button-to-restart-loop/1112525
Sketch: AuCP_Pushbutton.ino
Created: 09-Apr-23
MicroBeaut (μB)
*/
#include "Debounce.h"
#define millisec(t) (t * 1000UL)
#define pbPin 5
#define ledPin 2
bool start;
Debounce debounceInput;
unsigned long startTime;
unsigned long delayTime;
void StartCondition();
void StopConfition();
void DoSomething(bool value);
void setup() {
pinMode(pbPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
delayTime = millisec(2000);
debounceInput.setDebounceTime (20);
}
void loop() {
StartCondition();
StopCondition();
DoSomething(start);
}
void StartCondition() {
static bool prevInput;
bool pbValue = !digitalRead(pbPin);
bool input = debounceInput.debounce(pbValue); // Input Debounce
// Rising Edge
if (!start & input & !prevInput) {
start = true;
startTime = micros();
}
prevInput = input;
}
void StopCondition() {
if (!start) return;
if ( micros() - startTime < delayTime) return;
start = false;
}
void DoSomething (bool value) {
digitalWrite(ledPin, value);
}