-3
\$\begingroup\$

I cannot figure it out how to write the code for the next situation: I have a variable stayON = 5000; (5 seconds) that is used to turn on an LED for 5 sec after a button is pressed then turn it over after that time expired. This part I've done it but now I want to break this delay if the button is pressed again. I want to turn OFF the LED if the button is pressed again withing those 5 seconds when the LED is on. It doesn't work with delay() :(

\$\endgroup\$

closed as unclear what you're asking by Phil Frost, Anindo Ghosh, Matt Young, Nick Alexeev, PeterJ Jul 17 '13 at 2:04

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    \$\begingroup\$ What kind of hardware are you using? What compiler and tools are you using? What programming language are you using? How can we possibly answer this question without at least some clue about these things? \$\endgroup\$ – The Photon Jul 16 '13 at 16:21
  • 1
    \$\begingroup\$ it is tagged Arduino, don't you see it? \$\endgroup\$ – Popescu Marian Jul 16 '13 at 16:22
  • 3
    \$\begingroup\$ @PopescuMarian sometimes people just put an arbitrary tag of arduino. It doesn't mean anything unless the body of the question specifically says 'Arduino'. \$\endgroup\$ – efox29 Jul 16 '13 at 16:33
  • \$\begingroup\$ I didn't see it either. In general you can use interrupt service routine for event happening in unknown moment, like user input at arbitrary time. In the code, when you turn your led on, you can initialize a variable and when interrupt occur you can check that variable to confirm that led is on, if it is then you can switch it off. If this is not the case, if you want you can ignore user input or turn the led on. \$\endgroup\$ – zzz Jul 16 '13 at 16:34
  • 3
    \$\begingroup\$ Electrical Engineering Question Checklist \$\endgroup\$ – Rev1.0 Jul 16 '13 at 17:40
3
\$\begingroup\$

Assumptions: You are using Arduino.

Look for the blink_without_delay example. It uses the millis value and compares it to the last time the loop was run through to determine if enough time has passed. Translate the following pseudo code to code for your application, and merge it in with the example to do what you need.

Pseudo code:

if button pushed and light on {
   Turn light off
}

else if button pushed and light off  {
   Turn light on
}
\$\endgroup\$
2
\$\begingroup\$

One way is to use interrupts. That would probably be the best way because in that time, you can do other things if you wanted too. But a simple way, especially if all you are doing is just controlling this led, is the following

for(int x =0;x<500;x++)
{
  delay(10);
  if(digitalRead(button)==ON || x == 499)
    {
       digitalWrite(led)=OFF;
       break;
    }   
}

You delay for 10ms, and then check if the button is pressed or if we are at the end of our count, and then turn it off.

You may need to change the "ON" and "OFF" to whatever they should be for your setup up.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.