-3

So I am trying to have an LED react to a button. The problem I seem to be having is how to add a delay in between the button click and the led reacting. Essentially I want to, for example, click the button 3 times and then 2 seconds later have the led flash 3 times or if I hold the button for a 3 seconds, for the last second of the hold the led will turn on for 3 seconds. What I have so far is:

    //Global Vars
    // Global Variables
    int BUTTON = 2;
    int LED = 12;
    unsigned long DELAY = 2000;


    void setup() {
        pinMode(BUTTON, INPUT);
        pinMode(LED, OUTPUT);
        digitalWrite(LED, LOW);
    }

    void loop() {
        //Code that delays the button press
    }

The circuit, just so you can see what I have. Not 100% sure this is right either

I don't know if I should try to store the times that the button is pressed in an array or do something else. Looking for suggestions/explantions and maybe even just some code and an explanation. Its just for fun/learning so I am more interested in knowing how to do it, not just having the code that will do it. Thanks!

BeamerEA
  • 103
  • 1
  • 7
  • 1
    If you read what I wrote, you would know that it isn't homework. Also, if I knew how to implement the logic I described, I would have. As I stated earlier, I am stuck at even starting the scenario described... – BeamerEA Jun 12 '17 at 15:21
  • 1
    Agree with @Olaf. There isn't even the necessary pseudo-code to prove that you even though about this at all. You would have to start a timer after the first button press and stop the timer after the button was let go. Try to make it light up the LED if you hold for 1sec as a start. – Billy Ferguson Jun 12 '17 at 15:46
  • Just use a countdown timer and set an interruption that powers the led when the countdown reaches 0. –  Jun 12 '17 at 15:49
  • You haven't given a better try on for your question.I couldn't find any work, as per your question, in your code portion. – Mathews Sunny Jun 13 '17 at 03:47

1 Answers1

-1

This can be solved with a simple if statement.

if (digitalRead(BUTTON) = //however you have it set){
delay(DELAY);
digitalWrite(LED, HIGH);
}

This will read the state of the button line, when it matches the state you want it will execute the delay, then change the state of the LED pin.

CRoberts
  • 101
  • 1
  • delay() pauses the loop does it not? I still want to be able to read more button clicks in between the time that I click the button and the time that the led turns on. – BeamerEA Jun 12 '17 at 15:24
  • 1
    Then use interruptions. –  Jun 12 '17 at 15:44