Simple Start / Stop control with timer ON delay for start latch?

Hi guys!

I am trying to achieve the following:

D0 = Input (start button)
D1 = Input (stop button)
bool = “start_latch”

  • IF you press the start button for >3 seconds, then set “start_latch” = true
  • IF you press the stop button, then set “start_latch” = false

**Essentially, I want the user to have to press the start button for more than 3 seconds before latching a start bool to true.
If at any point the stop button is pressed this will stop the process.

TIA!!!

What’s the question ;)?

This worked for me:

#include "Particle.h"


const int LED_RUNNING = D2;
const int BUTTON_START = D3;
const int BUTTON_STOP = D4;

const unsigned long START_PRESS_MS = 3000;

bool isRunning = false;
unsigned long pressTime = 0;

void setRunning(bool running); // forward declaration

void setup() {
	pinMode(BUTTON_START, INPUT_PULLUP);
	pinMode(BUTTON_STOP, INPUT_PULLUP);
	pinMode(LED_RUNNING, OUTPUT);
}

void loop() {
	if (digitalRead(BUTTON_START) == LOW) {
		// Start button pressed
		if (pressTime == 0) {
			// First time pressed
			pressTime = millis();
		}
		else {
			// Still pressed
			if (millis() - pressTime > START_PRESS_MS) {
				// Pressed long enough to activate
				setRunning(true);
			}
		}
	}
	else {
		// Not pressed
		pressTime = 0;
	}

	if (digitalRead(BUTTON_STOP) == LOW) {
		// Stop button pressed
		setRunning(false);
		pressTime = 0;
	}
}

void setRunning(bool running) {
	if (isRunning != running) {
		isRunning = running;
		digitalWrite(LED_RUNNING, running);
	}
}

Hey Rickas

Thanks for your reply mate.

I have tried your code but not working unfortunately…
Any ideas?

It’d help if you could at least tell us what didn’t work…

What does it do or not do?

The start button connects between D3 and GND.
The stop button connects between D4 and GND.

Perfect :smiley:

I was going between D3 / D4 and 3v3 :confounded:

Thanks so much!
Cheers

1 Like

You need to be careful not to blow the D3 and D4 pins if you connect them direct to a power supply - best to put a 10K ohms resistor in series.

1 Like