Pages: [1]   Go Down
Author Topic: Button State  (Read 529 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, currently I am working on coding which requires a remote switching a light on. I have worked it all into the code by the use of a relay switch on the remote. When a push button in 'HIGH' a signal is sent to turn the light on and when i released the button (low) the sign is send to turn the light off.

I want the light to stay on for as long as I'm holding down the button but i don't want the remote to constantly be sending a signal. What do I need to add to the code so when i push and hold the button down the signal is send for 3 seconds then stops sending it to the light as its already on?

Thanks
agau
Logged

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1283
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

One way to do this is to record when then button is first pressed by saving the value returned from the millis() function. Each time through loop() check if the difference between current value of millis() and your saved value is bigger than 3000 (3 seconds). Once you have reached that you can then decide what to do.
Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

would that allow the signal to be switched off automatically after the 3 seconds?
Logged

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1283
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Depends on what you mean by automatically. Remember you are writing the software that makes things happen so if you don't tell the controller to do (or not do) something, it won't happen. To the outside it looks automatic, but in your code you need to spell it out.

Without writing the code, the flow should look something like this:

loop()
button_current_state = digitalRead();
If (button_previous_state = OFF) and (button_current_state = ON)
  last_press_time = millis()

if (button_current_state = ON) and (millis()-last_press_time <= 3000)
  send your signal
else
  turn your signal off, stop sending, or whatever

button_previous_state = button current state  // this remembers the current state of the button for the detect transition test

You see that the code will set the time when the button changes state (not pressed to pressed), so you need to remember the last state of the button in a global variable (one outside of the loop() function). When you detect the change of state, remember the time, and the rest is 'automatic' :-).

Hope this helps.
« Last Edit: April 22, 2012, 07:00:56 pm by marco_c » Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Got it to work  smiley , thanks for your help!
Logged

Pages: [1]   Go Up
Jump to: