Hello,
I'm trying to make a ON/OFF switch with a push button, but sometimes it works like a ON/OFF switch, other times like a push button.
here is the code:
const int buttonPin = 2;
const int ledPin = 3;
int buttonState = 0;
bool ledOn = false;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
if (ledOn == false) {
ledOn = true;
} else {
ledOn = false;
}
}
if (ledOn == true) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}
Post a drawing that shows how you wired the button.
if the buttonstate is high
and if the ledOn is already true you are
turning it off.
I am assuming you want the
led to reflect the buttonstate. So you can
just get rid of the else {ledOn = false} part
look through this part of your code
if (buttonState == HIGH) {
if (ledOn == false) {
ledOn = true;
} else {
ledOn = false;
}
}
Your sketch is lacking both State Change Detection and Debounce.
The State Change Detection only toggles the LED when the button is first pressed. The button must be released before a press of the button will toggle the LED. Without it the LED will toggle on and off very quickly for as long as the button is held down.
Mechanical switches, buttons, and relay contacts have a tendency to cycle closed and open for a few milliseconds after being activated. The debounce will filter out any state changes that happen less than a few milliseconds after the last accepted state change. Without it a single button press might register as two or more button presses.
const int ButtonPin = 2;
const int LedPin = 3;
void setup()
{
pinMode(ButtonPin, INPUT);
pinMode(LedPin, OUTPUT);
}
void loop()
{
// These variables are only used in loop() so there is no need for them to be global.
// The 'static' keyword makes them retain their value across function calls, like a global.
static unsigned long lastStateChangeTime = 0;
static boolean previousButtonState = LOW;
static boolean ledOn = false;
// This variable is set fresh each time loop() runs so it doesn't need to be static.
boolean buttonState = digitalRead(ButtonPin);
// State Change Detection and Debounce
if (buttonState != previousButtonState && millis() - lastStateChangeTime > 10)
{
lastStateChangeTime = millis();
previousButtonState = buttonState;
if (buttonState == HIGH) // Just changed to HIGH state
{
ledOn = !ledOn;
digitalWrite(LedPin, ledOn);
}
}
}
johnwasser:
Your sketch is lacking both State Change Detection and Debounce.
The State Change Detection only toggles the LED when the button is first pressed. The button must be released before a press of the button will toggle the LED. Without it the LED will toggle on and off very quickly for as long as the button is held down.
Mechanical switches, buttons, and relay contacts have a tendency to cycle closed and open for a few milliseconds after being activated. The debounce will filter out any state changes that happen less than a few milliseconds after the last accepted state change. Without it a single button press might register as two or more button presses.
const int ButtonPin = 2;
const int LedPin = 3;
void setup()
{
pinMode(ButtonPin, INPUT);
pinMode(LedPin, OUTPUT);
}
void loop()
{
// These variables are only used in loop() so there is no need for them to be global.
// The 'static' keyword makes them retain their value across function calls, like a global.
static unsigned long lastStateChangeTime = 0;
static boolean previousButtonState = LOW;
static boolean ledOn = false;
// This variable is set fresh each time loop() runs so it doesn't need to be static.
boolean buttonState = digitalRead(ButtonPin);
// State Change Detection and Debounce
if (buttonState != previousButtonState && millis() - lastStateChangeTime > 10)
{
lastStateChangeTime = millis();
previousButtonState = buttonState;
if (buttonState == HIGH) // Just changed to HIGH state
{
ledOn = !ledOn;
digitalWrite(LedPin, ledOn);
}
}
}
Thank you, the code works perfectly
OP's picture. I assume you actually have connections to the + and - rails at the top of the breadboard, otherwise the LED would never light.