I am little bit in a dead end with millis(). I need to activate a relay with a button, and it relay should remain open 5000ms. I can do it with a Delay(). But, what if I press that button while not all of those millis run out? Obviously, the MCU will not notice it. I can not use interrupt as the button is tied to a D4 on Uno.
So my guess is to play with states. But here I need your help.
Here is what my code look like for now:
const int buttonPin = 4;
const int rlyPin = 2;
int buttonState = 0;
int rlyState = 1;
unsigned long previousMillis = 0;
;
const long interval = 1000;
void setup()
{
pinMode(rlyPin, OUTPUT);
pinMode(buttonPin, INPUT);
digitalWrite(rlyPin, LOW);
}
void loop()
{
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH)
{
rlyState = LOW;
}
else
{
rlyState = HIGH;
}
digitalWrite(rlyPin, rlyState);
}
Do you want the relay to react (5000ms more) to the second button press during the "relay open" or do you want the relay to close at 5000ms from the first button press?
Well, first of all you should respond to button CHANGES not current state. Second, you should handle button bouncing (either via software using a small delay(50) after a change is detected, or, better, via hardware with a capacitor and a resistor).
Then you need to better clarify your goal because the code you have shown us turns and keeps the relay on while the button is pressed, and off as soon as you leave the button. Based on your description, I assume you just need a single press to activate the relay for 5 seconds. If you press the button again you reset the 5 seconds: just tell me that's correct, and I'll give you a few tips and some code using millis().
PS: please, always enclose the whole code inside a single "code" block, don't use single line formatting. Thanks.
There is a bug with the Discourse forum software that sometimes results in code posted in code tags ending up as a series of single lines of code each in code tags. That is probably what happened here.
I have fixed the code tags and for future reference I suggest that you don't use the Rich Text editor option of the forum post composer
In my experience the easiest way to tidy up the code and add the code tags is as follows
Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.
It is also helpful to post error messages in code tags as it makes it easier to scroll through them and copy them for examination