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
}
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.
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.
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...
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.
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?
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.
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.
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?
connection and ''BlinkWithoutDelay'' is a Arduino sample. It doesn't mean without delay, It means without ''delay'' code.
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.