i want to run a code on my arduino nano with to buttons which activates a relay one button is going to be pushed in and then send a signal to the raly that it must ''run'' for 10 seconds but the 2nd button i want to press it and hold it and then send a signal that the relay will run during the time that the button is held down. i dont relay how the code for the 2nd buttons must be writen can you guys help me out
this is the code i have so far
You're getting there.
Once you introduce millis() timing - it's as good a time as any to remove all the delay() calls.
You have a delay(100) - probably to debounce the button. That's where I was hinting at detecting the button state change - rather than testing the button is simply LOW or HIGH
You're testing the button unnecessarily frequently.
When the button's 'state' changes TRUE - start a counter.
When that millis counter reaches the short-press, or long-press interval - do your thing.
Keep track so the button action is only performed once per state - or yoyu can add recurring at a 'repeat' interval if you need it.
Give it a try, then we can help.
There are about a hundred exact examples of your problem on the forum.
You’ll need to search and try things - then you’ll learn for next time.
Let’s start with...
unsigned long presstime;
bool buttonstate:
bool prevbuttonstate;
buttonstate = digitalread(buttonpin)// determine if button has changed state...
if (buttonstate != prevbuttonstate) {
presstime = millis();
prevbuttonstate = buttonstate;
// you can do things here to debounce if needed
}
// test the time since the last button state change (long, short, released etc...