I understand push buttons are a worn out subject but I need help. I've taken on a project that has me just a little confused on how to combine all the different functions I need to happen. I can make them all happen separately but combining them has thrown me for a loop. I'll give a brief description of what I am trying to accomplish.
I have a push button connected into my arduino on pin 3 and a relay shield being controlled via pin 13.
I need to be able to activate the relay with a simple push of the button and turn it off with another push.
I need to be able to push and hold the button for 5-10 seconds which will turn the relay on for 2 min. And then turn off automatically.
But, at anytime during those 2min. I need to be able push the button again to cancel the 2min. Delay.
Like I said, I am able to make these two things happen separately, but I am having trouble combing the two functions into one script.
Any help or point in the right direction would be greatly appreciated.
Sounds to me like you're using the function "delay" to do your timing? As you've found out, it doesn't 'work and play well with others' as the timer doing the delay ties up the processor. The script is 'blind' to things during the delay period. The key is to not use delay to get the timing.
One of the examples addresses this point, you might want to start there?
Another kind of fun idea is to read the voltage off a pot from +5 to ground with one of the analog inputs and use that value for the delay time (so you get a knob to adjust it with). You can scale the steps from reading the pin to convert them to time using the 'map' function.
Kevincol54:
Like I said, I am able to make these two things happen separately, but I am having trouble combing the two functions into one script.
Well, how about posting your current code - using the "code" tagging button [ # ] above the submission window - for each (complete) and we can have a look at it. I have recently posted a debounce algorithm which I hope is accurate; this is a fairly straightforward extension of it.
Study "State Machine" and "Functions".
Having understood those principles it will be easy.
You need to continuously read the button and change the previous button state(s), when it was pressed.
Your code should distinguish between "short press" and "long press" (you might find some libraries doing that for you).
Then depending on the press, you need to have 3 functions:
function 1 = switching the relay on
function 2 = switching the relay off
function 3 = switching the relay on for a period of time and automatically switch it off after the time has expired
If you don't need to scan buttons or do something else while function3 was activated, this could be done with a simple delay. If not - use millis and check in every loop sequence, if the "millis have run out" - see the "blink without delay example".
I have 3 buttons 2 LED's, (kind of a up down stop function)
button1=3
button2=4
button3=5
redled=9
grnled=10
first button turns on red led
if second button is pressed red led is off green turns on and vice versa
if third button is pressed and either red or green led is on it will turn them off
now this is where i am stuck, i would like the third button once held for 3-5s turn on both led's red solid and grn flashes whilst held then when released return to normal state both led's off
im using a state / case at the moment for my first part but when i try to integrate any further code my original function gets completely lost or makes no sense