I am trying to write a code for a project and my objective is to compare two values and if they are equal for more than a second than an led turns on. I've been programing Arduinos for years and somehow have never learned how to use timers. any help would be much appreciated.
Do you mean the hardware timers that are available on most microcontroller chips in the Arduino ecosystem? There is no need to use those when your timescale is on the order of seconds.
You don't need a timer, just the millis() function
Take a reading and save the value of millis()
Keep taking readings and if the current one differs from the previous one save the value of millis() again. If the two readings are the same check whether 1 second has elapsed since you last saved millis(). If so then the values were the same for 1 second
static unsigned long lastTimeDifferent;
if (ValueA != ValueB)
lastTimeDifferent = millis();
// If the same for more than a second, turn on the LED
if (millis() - lastTimeDifferent >= 1000)
digitlWrite(LED_BUILTIN, HIGH);
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.