// --- Pins ---
const byte IR_START_PIN = 2; // button input
const byte LED_PIN = 3; // LED output
// --- Debounce globals (raw tracking) ---
bool lastStartState = HIGH; // previous raw reading
unsigned long lastStartDebounce = 0; // time of last raw change
const unsigned long DEBOUNCE_DELAY = 50; // ms
// --- Event flag (used by loop) ---
bool startEdgeFlag = false; // defined here, consumed in loop()
// ------------------------------------------------------------
void setup() {
Serial.begin(115200);
pinMode(IR_START_PIN, INPUT_PULLUP); // button to GND when pressed
pinMode(LED_PIN, OUTPUT);
}
void loop() {
checkStartSensorBestFinal();
// --- Event consumption / task handling ---
if (startEdgeFlag) {
startEdgeFlag = false; // consume event
Serial.println("edge -> action");
// Toggle LED
digitalWrite(LED_PIN, digitalRead(LED_PIN) == LOW ? HIGH : LOW);
}
}
// ------------------------------------------------------------
void checkStartSensorBogus()
{
bool currentState = digitalRead(IR_START_PIN);
if (currentState != lastStartState) {
lastStartDebounce = millis();
}
if ((millis() - lastStartDebounce) > DEBOUNCE_DELAY) {
if (lastStartState == HIGH && currentState == LOW) {
// ~100 lines of code for this edge elided here, set the flag we using
startEdgeFlag = true;
}
}
lastStartState = currentState;
}
// ------------------------------------------------------------
void checkStartSensorWorksInterim()
{
bool currentState = digitalRead(IR_START_PIN);
// raw-change detector: reset debounce timer on *any* input transition (bounce included)
if (currentState != lastStartState) {
lastStartDebounce = millis();
}
lastStartState = currentState; // last raw reading (Limor's lastButtonState)
if ((millis() - lastStartDebounce) > DEBOUNCE_DELAY) {
// debounced state (Limor's buttonState) — separate from lastStartState (raw history)
static bool startSwitchState = HIGH; // make global if used elsewhere
if (currentState != startSwitchState) {
bool old = startSwitchState;
startSwitchState = currentState;
// act only on the debounced falling edge
if (old == HIGH && startSwitchState == LOW) {
/* Isn't this just
if (startSwitchState == LOW) {
or
if (currentState == LOW) {
Yes:
*/
// if (startSwitchState == LOW) { // works...
// if (currentState == LOW) { // also works...
startEdgeFlag = true;
// NOTE: handle the ~100 lines elsewhere when startEdgeFlag is seen/consumed
}
}
}
}
// ------------------------------------------------------------
void checkStartSensorBestFinal()
{
bool currentState = digitalRead(IR_START_PIN); // raw reading (may bounce)
// If raw changed, reset debounce timer.
if (currentState != lastStartState) {
lastStartDebounce = millis();
}
// Save raw reading for next pass
lastStartState = currentState;
// If raw has been stable long enough, accept it as the debounced state.
if ((millis() - lastStartDebounce) > DEBOUNCE_DELAY) {
static bool startSwitchState = HIGH; // debounced state (make global if used elsewhere)
// Debounced transition occurred (Limor pattern)
if (currentState != startSwitchState) {
startSwitchState = currentState;
// Select ONE edge: falling (button press)
if (startSwitchState == LOW) {
startEdgeFlag = true; // signal event
}
}
}
}
// ------------------------------------------------------------