I guys, it’s the first time i try to make a some simple code…
How can I make the code for an output to work, if the input is OFF and has previously been ON for at least 10 seconds (that is, make sure that the output is only triggered, if the input has been at least 10 seconds on, previously).
// How can I make the code for an output to work, if the
// input is OFF and has previously been ON for at least
// 10 seconds
const byte InputPin = 2;
const byte OutputPin = LED_BUILTIN;
unsigned long TimeLastOff = 0;
void setup()
{
pinMode(InputPin, INPUT_PULLUP);
pinMode(OutputPin, OUTPUT);
}
void loop()
{
unsigned long currentMillis = millis();
if (digitalRead(InputPin) == LOW)
{
// If the input has been HIGH for
// at least 10 seconds before going LOW
if (currentMillis - TimeLastOff >= 10000UL)
digitalWrite(OutputPin, HIGH);
TimeLastOff = currentMillis;
}
}
the code i provided does that. it calls setOutput () which captures the timestamp at the time the output is set (i think john’s approach, which is a good approach, resets the timer when the output is reset (off))
Thanks for the support Jonh , i really aprecciate,
Can you help me with another simple situation please?
I now have another output (a 12v led strip, on which I will use a relay to act) and I want the arduino to be able to control it …
I want this new output to be always on … and only starts to flash when the other output (the first one) is working (that means , INPUT OFF)
Thanks