/*
State change detection (edge detection)
Wokwi sim https://wokwi.com/projects/365743943712748545
based on: https://wokwi.com/projects/365724367327685633
Modified from https://www.arduino.cc/en/Tutorial/BuiltInExamples/StateChangeDetection
to use INPUT_PULLUP
to measure and report time-in-state
to add a state machine per
for https://forum.arduino.cc/t/search-and-destroy-bomb-prop-for-paintball/1130728/8?u=davex
Often, you don't need to know the state of a digital input all the time, but
you just need to know when the input changes from one state to another.
For example, you want to know when a button goes from OFF to ON. This is called
state change detection, or edge detection.
This example shows how to detect when a button or button changes from off to on
and on to off.
The circuit:
- pushbutton attached to pin 2 from GND
- LED attached from pin 13 to ground through 220 ohm resistor (or use the
built-in LED on most Arduino boards)
created 27 Sep 2005
modified 30 Aug 2011
by Tom Igoe
This example code is in the public domain.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/StateChangeDetection
*/
// this constant won't change:
const int buttonPin = 2; // the pin that the pushbutton is attached to
const int ledPin = 13; // the pin that the LED is attached to
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
unsigned long lastChangeMs = 0; // time of last state change
unsigned long lastLowMs = 0; // time of last state change
void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT_PULLUP);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}
unsigned long countdownStartMs = 0;
int armed = false;
unsigned long startOfStateMs = 0;
void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
unsigned long now = millis();
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
lastChangeMs = now;
if (buttonState == LOW) {
// if the current state is HIGH then the button went from off to on:
buttonPushCounter++;
lastLowMs = now;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
} else {
// if the current state is LOW then the button went from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
switch (armed) { // transition based on arm-state and button
case 0: // idle
if (buttonState == LOW && now - lastLowMs > 15000UL) {
armed = 1;
countdownStartMs = now;
changeArming(1);
digitalWrite(ledPin, HIGH);
}
break;
case 1: // armed and counting
if (now - startOfStateMs > 60000UL) { // boom
Serial.print("boom");
changeArming(5);
}
if (buttonState == LOW && now - lastLowMs > 15000UL) { // disarmed
changeArming(0);
digitalWrite(ledPin, LOW);
}
break;
case 5: // boom
Serial.print("B");
if (buttonState == LOW && now - lastLowMs > 15000UL) { // disarmed
changeArming(0);
digitalWrite(ledPin, LOW);
}
}
report();
}
void changeArming(int newState) {
unsigned long now = millis();
armed = newState;
startOfStateMs = now; // record transition time
lastChangeMs = now; // reset button time
lastLowMs = now;
}
void report() {
unsigned long interval = 250;
static unsigned long last = 0;
unsigned long now = millis();
if (now - last >= interval) {
last = now;
Serial.print("state:");
Serial.print(buttonState == HIGH ? "HIGH" : "LOW" );
Serial.print(" time:");
Serial.print( now - lastChangeMs );
Serial.print(" armed:");
Serial.print(armed);
if (armed) {
Serial.print(" armedTime:");
Serial.print(now - countdownStartMs);
}
Serial.println();
}
}Arm/Disarm/Reset
hold 15sec