Blinking LED in IF state

Hi guys. I'm a beginner and I've been experimenting with arduino for the past months.

I found a code on google that i was trying to adapt for an experiment but i can't seem to succeed no matter what.

This is the code:

int ledpin = 5;
 int pushbutton1 = 9;
 int pushbutton2 = 8;
 boolean lastbuttonstate1 = LOW;
 boolean lastbuttonstate2 = LOW;

 void setup() 
 { 
   pinMode(ledpin,OUTPUT);
   pinMode(pushbutton1,INPUT);
   pinMode(pushbutton2,INPUT);
 }

 void loop() {
    static bool ledState = false; // LED is off by default
    if ((digitalRead(pushbutton1) == HIGH) && !ledState) { // 1 pressed & led is off
        digitalWrite(ledpin, HIGH) ;
        ledState = true;
    }
    if ((digitalRead(pushbutton2) == HIGH) && ledState) { // 2 pressed & led is on
        digitalWrite(ledpin, LOW);
        ledState = false;
    }
}

Now the code the way it's written allows me to turn on the LED when i push the button 1 and keep it on, then by pushing the button 2 the LED turns off.

What i want to do instead is making the led blink in a loop when button 1 is pressed instead of having it always lit, then turn it off with button 2.

Also it would be interesting to know if and how i can add more functions/actions while the ledState is true.

Thanks

Imagine you have a variable called shouldBeBlinkin

boolean shouldBeBlinkin = false;

Then in your if statements instead of turning a led on and off, you could set shouldBeBlinkin to true or false.

if ((digitalRead(pushbutton1) == HIGH) && !shouldBeBlinkin) {
        shouldBeBlinkin = true;
    }

Then after those if statements in loop(), you could check that variable and blink:

if(shouldBeBlinkin) {
    // write blink without delay style code here
}

Delta_G: Then after those if statements in loop(), you could check that variable and blink:

if(shouldBeBlinkin) {
    // write blink without delay style code here
}

Thank you for your answer.

Everything clear as day. Except, shame on me, about the blink without delay. I've never done that and the examples found around seem not working for me.

Would you please be so kind to put me in the good direction?

I really appreciate it.

Thank you

threepwood85: Thank you for your answer.

Everything clear as day. Except, shame on me, about the blink without delay. I've never done that and the examples found around seem not working for me.

Would you please be so kind to put me in the good direction?

I really appreciate it.

Thank you

You must have done something wrong. I can point you in the right direction to get it right. Show it to someone who knows what they're doing and see if they can point out for you what you did wrong.

Or tell us what part you don't understand.

There are literally thousands of pages and examples trying to explain that concept. I know you can find some.

What i did was to find some examples of no delay blinking.

The last version of the code modified accordingly is

 const byte ledpin = 5;
 int pushbutton1 = 9;
 int pushbutton2 = 8;
 boolean shouldBeBlinkin = false;
 boolean lastbuttonstate1 = LOW;
 boolean lastbuttonstate2 = LOW;

 void setup() 
 { 
   pinMode(ledpin,OUTPUT);
   pinMode(pushbutton1,INPUT);
   pinMode(pushbutton2,INPUT);
 }

 void loop() {
    static bool ledState = false; // LED is off by default
    if ((digitalRead(pushbutton1) == HIGH) && !shouldBeBlinkin) { // button 1
        shouldBeBlinkin = true;
    }
    if ((digitalRead(pushbutton2) == HIGH) && shouldBeBlinkin) { // button 2
        shouldBeBlinkin = false;

        if(shouldBeBlinkin) {
digitalWrite(ledpin, millis()>>9 &1); // for ca. 2 Hz blinkin
      }
    }
}

No idea where the mistake is tbh.

A few things.

Recheck all your { } for the if() statements.

As it is, the code will leave the LED in its last state when shouldBeBlinkin goes false. If you want the LED off you’ll need to explicitly tell the processor that.

Depending how your switches are wired you may want to change to INPUT_PULLUP for the pin mode.

I presume that you have read Using millis() for timing. A beginners guide

Let us know what you do not understand

threepwood85:
No idea where the mistake is tbh.

Hey at least you know what happened when you tried to run it. Imagine how hard it is for us to debug when you refuse to share that sort of information.

The should-be-blinking code should NOT be embedded in the

if ((digitalRead(pushbutton2) == HIGH) && shouldBeBlinkin)

body.

It should be completely independent of the (current or previous) state of either switch.