The BlinkWithoutDelay Sample Which Functions Depending on a Push Button

Hello everyone, I'm trying to add a button to BlinkWithoutDelay sample. I add a button. Everything was going OK but I wanted while I'm not pressing the button, I want the led turn off. In this case if the led turns on and I take my hand the led still turn on. Then I add a little code for that but now the led always turns off.

const int ledPin =  LED_BUILTIN; 
const int buttonPin = 5; // Emirhan

int ledState = LOW;            
int buttonstate; // Emirhan

unsigned long previousMillis = 0;      

const long interval = 1000;          

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT); // Emirhan
}

void loop() {

  unsigned long currentMillis = millis();
  buttonstate = digitalRead(buttonPin);  // Emirhan
  
    if (buttonstate==HIGH && currentMillis - previousMillis >= interval) { // Emirhan
    
    previousMillis = currentMillis;
    
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }

    digitalWrite(ledPin, ledState);
  }
  
    else
    digitalWrite(ledPin,LOW);  // Emirhan 

  
}

how did you wire your button?

look at your outer if-else

    if (buttonstate==HIGH && currentMillis - previousMillis >= interval) { // Emirhan

--> try to understand when you are in the "else" - what does it mean for the condition

Sorry, but I can't understand the problem. I wired simply.

The demo Several Things at a Time is an extended example of BWoD and illustrates the use of millis() to manage timing without blocking. It may help with understanding the technique.

It includes code for a button switch.

Have a look at Using millis() for timing. A beginners guide if you need more explanation.

...R

When the condition (A and B) is not true - what can you say about A or B ? What can’t you say?

I am pleased for your effort but I still don't understand. I add a one more if-else but now the led always works.

 unsigned long storageMillis;
const byte ledPin = 13;   
const byte buttonPin = 5;
int buttonstate;

void setup()
{

  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);  
}

void loop()
{
 buttonstate = digitalRead(buttonPin);
 
  if(buttonstate==HIGH) 
  {
    
  if(millis() - storageMillis >= 500)  
  
    {
    digitalWrite(ledPin, !digitalRead(ledPin));  
    storageMillis = millis();  
    }
    
  }
  
  else
  digitalWrite(ledPin, LOW);
  
}

J-M-L:
When the conduatuob (A and B) is not true - what can you say about A or B ? What can’t you say?

It can't be there is one situation; button on or off.

Robin2:
The demo Several Things at a Time is an extended example of BWoD and illustrates the use of millis() to manage timing without blocking. It may help with understanding the technique.

It includes code for a button switch.

Have a look at Using millis() for timing. A beginners guide if you need more explanation.

...R

Thank you but isn't it complicated for just a switch?

if (A and B) is true, it means only one thing A is true and B is true
if (A and B) is false, it could be many things (A is false and B is true), or (A is true and B is false) or (A is false and B is false)

This is not an answer

I wired simply.

How is the button wired, really ? full description...

emirhankarahan:
Thank you but isn't it complicated for just a switch?

Take out of the demo the parts that you need.

...R

J-M-L:
How is the button wired, really ? full description...

emirhankarahan:
Sorry, but I can't understand the problem. I wired simply.

@OP

Given below 4 possible (could have more) options for connecting a button with the UNO via DPin-5. Tell us which option (1, 2, 3, 4) you have selected.
led1-sw.png

BTW: To you, what is the meaning of -- BlinkWithoutDelay? The blink is composed of 4 events: ON-timeDelay-OFF-timeDelay. We need certain amount of timeDelay (waiting for sometimes) to see that the LED is ON (OFF). How can a LED blink without Delay?

led1-sw.png

J-M-L:
if (A and B) is true, it means only one thing A is true and B is true
if (A and B) is false, it could be many things (A is false and B is true), or (A is true and B is false) or (A is false and B is false)

This is not an answer How is the button wired, really ? full description...

I already added a picture for connection. One terminal of the button wired Arduino 5. digital pin and the other terminal wired with Arduino 5V pin. And a pull-up resistor. I'm using simulation programs(tinkercad and proteus). I checked the wires. There isn't problem.

And I updated my code for your advice. It isn't working.

    else if(buttonstate==LOW && currentMillis - previousMillis >= interval)
    digitalWrite(ledPin,LOW);  // Emirhan 
    else if(buttonstate==HIGH && currentMillis - previousMillis < interval)
    digitalWrite(ledPin,LOW);
    else if(buttonstate==LOW && currentMillis - previousMillis < interval)
    digitalWrite(ledPin,LOW);

Robin2:
Take out of the demo the parts that you need.

...R

I tried. I wish, I could. Thx.

GolamMostafa:
@OP

Given below 4 possible (could have more) options for connecting a button with the UNO via DPin-5. Tell us which option (1, 2, 3, 4) you have selected.
led1-sw.png

BTW: To you, what is the meaning of -- BlinkWithoutDelay? The blink is composed of 4 events: ON-timeDelay-OFF-timeDelay. We need certain amount of timeDelay (waiting for sometimes) to see that the LED is ON (OFF). How can a LED blink without Delay?

  1. connection and ''BlinkWithoutDelay'' is a Arduino sample. It doesn't mean without delay, It means without ''delay'' code.

I already added a picture for connection.

but you failed at doing so.. picture not visible for the rest of us...

emirhankarahan:
I already added a picture for connection. One terminal of the button wired Arduino 5. digital pin and the other terminal wired with Arduino 5V pin. And a pull-up resistor.

emirhankarahan:
4. connection[...].

Apparently, the above two statements on connection of your button do not match.

As per top statement: one side (say, Terminal-1) of the button with DPin-5; other side (say, Terminal-2) of the button with 5V. You have a pull-up resistor - how is it connected? Is it between DPin-5/Terminal-1 and 5V or between Terminal-2 and 5V?

Option 4. of Post#8 says that Terminal-1 of the button is with DPin-5; Terminal-2 of the button is with 5V; the pull-down resistor is between Terminal-1 and GND.

Please, be sure first about the connection of your button.

emirhankarahan:
I tried. I wish, I could. Thx.

Unless you post the program showing what you tried I can't help.

...R

I've tried one more this code;

unsigned long storageMillis;
const byte ledPin = 13;   
const byte buttonPin = 5;
int buttonstate;

void setup()
{

  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);  
}

void loop()
{
 buttonstate = digitalRead(buttonPin);
 
  if(buttonstate==HIGH) 
  {
    
  if(millis() - storageMillis >= 500)  
  
    {
    digitalWrite(ledPin, !digitalRead(ledPin));  
    storageMillis = millis();  
    }
    
  }
  
  else
  digitalWrite(ledPin, LOW);
  
}

and It works as I wish. Thank you for all.

Note: I used one more if-else for that. I couldn't fix the problem but in the end, the second code is working.