const byte startPin = 2; // first button
const byte stopPin = 3; // second button
bool isRunning = false;
void setup() {
pinMode(startPin, INPUT_PULLUP);
pinMode(stopPin, INPUT_PULLUP);
Serial.begin(115200); Serial.println();
Serial.println(F("Ready. Press the start button (green)"));
Serial.println(F("- Press the green button to start running"));
Serial.println(F("- Press the red button to stop once running"));
}
void loop() {
if (isRunning) { // we are running test if the secondary button is pressed
if (digitalRead(stopPin) == LOW) {
isRunning = false;
Serial.println(F("Secondary button pressed, stop."));
} else {
// do what you need to do in a non blocking way when running
}
} else {
if (digitalRead(startPin) == LOW) {
isRunning = true;
Serial.println(F("Primary button pressed, Running."));
} else {
// do what you need to do in a non blocking way when notrunning
}
}
}