Hello, Im new in coding and I have a problem with one thing. I need to make a code that will monitor an input pin and if a input is at HIGH state for 5 seconds to turn on a relay. I hope that you understand me and I would be very thankful with any help.
Please read the first topic, "How to use this Forum".
Your posting lacks code, it lacks wiring.
Look at the State Change Detection example in the IDE (File->examples->02.Digital->StateChangeDetection). It will show you how to know when a pin goes high or low. You then capture the time of that event [millis()] and then monitor how long it has been in that state. If it is greater than 5 seconds [5000 milliseconds] then turn on your relay.
Give it a shot and if you get stuck, post your code, your errors and a description and people will help.
Some tutorials on how to use millis() for timing.
Several things at a time.
Beginner's guide to millis().
Blink without delay().
In your loop, check the button each time through the loop. If it changes state (say HIGH to LOW), jump immediately to a function which delays for say 50 ms. (debounce) saves the time ( the millis() function) and then do a tight loop that just reads the button, each time through noting the time. If you make it to 5000 ms. elapsed time with the button still LOW, you're there. If at ant time before that, the button changes state exit the function with no action.
Hello
At least you need two time keeper, one time keeper is doing the debouncing of the button and the second is working as a retriggerable timer.
As basis for o.m. timers you can use the IDE example BLINKWITHOUTDELY.
p.s. Under what conditions is the LED switched off again?
electronics996:
if a input is at HIGH state for 5 seconds to turn on a relay
This should get you there - timer counter state change toggle FSM tutorial
electronics996:
if a input is at HIGH state for 5 seconds to turn on a relay.
const byte InputPin = 4;
const byte RelayPin = 7;
void setup()
{
pinMode(InputPin, INPUT);
digitalWrite(RelayPin, LOW);
pinMode(RelayPin, OUTPUT);
}
void loop()
{
static unsigned long timeLastLOW;
if (digitalRead(InputPin) == LOW)
timeLastLOW = millis();
if (millis() - timeLastLOW > 5000)
{
// InputPin has not been LOW for at least 5 seconds,
// therefore it has been HIGH for at least 5 seconds.
digitalWrite(RelayPin, HIGH);
}
}
WARNING: Your design never turns the relay off.
WARNING: Most relay modules sold for Arduno are Active LOW. Set the output LOW to turn on the relay coils and HIGH to turn off the relay coil.