I'm having a tough time using a momentary push button as a toggle switch.
I'm trying to create system that displays the state of a switch on an LCD display.
something which in the end will display "LED ON", and "LED OFF" on the LCD display.
I've been trying for a while but can't seem to figure out the correct way to approach this. I've tried using the "Debounce" tutorial, but can't seem to change the state of the switch properly.
What I'm trying to accomplish is: push button once, LCD displays "LED ON" for 2 seconds. Push the button again, and LCD displays 'LCD OFF"
I haven't gotten that far into the code, but here's my current bit:
#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);
const int buttonPin = 1;
const int ledPin = 13;
int ledState = HIGH;
int buttonState;
int lastButtonState = LOW;
long lastDebounceTime = 0;
long debounceDelay = 50;
void setup(){
Serial.begin(9600);
lcd.begin(16,2);
lcd.clear();
analogWrite(10, 135);
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, ledState);
}
void loop(){
int reading = digitalRead(buttonPin);
if (reading != lastButtonState){
lastDebounceTime = millis();
}
if((millis() - lastDebounceTime) > debounceDelay) {
if(reading != buttonState){
buttonState = reading;
if (buttonState == HIGH) {
ledState = 1;
}
else{
ledState = 0;
}
}
}
Serial.println(ledState);
digitalWrite(ledPin, ledState);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(ledState);
delay(10);
lastButtonState = reading;
}
Try changing the button pin to some other pin. Pin# 1 is used to transmit data to your computer when you are using serial communication or uploading a sketch. Using the same pin to do two things at once might be causing the problem.
Will try posting the updated code. Meanwhile try changing the pin.
Thank you every one.
tf68, your code worked nicely and I'm going to try and reverse engineer it as much as possible to fit my project. Thank you for the help! I appreciate it.
// 3/17/2020
// LED toggles after momentary button is pressed
// delay of 250ms works great to remove an false triggers from switch bounce
#define SwWhite 40 //Switch input to Arduino Mega D40 #define En_LED_R 24 //Red LED from RGB-LED on Encoder connected to Arduino Mega pin D24
int set = 0; //variable to toggle
You have posted code without using code tags. The code tags make the code look
like this
when posting source code files. It makes it easier to read, and can be copied with a single mouse click. Also, if you don't do it, some of the character sequences in the code can be misinterpred by the forum code as italics or funny emoticons. The "Code: [Select]" feature allows someone to select the entire sketch so it can be easily copied and pasted into the IDE for testing.
If you have already posted without using code tags, open your message and select "modify" from the pull down menu labelled, "More", at the lower left corner of the message. Highlight your code by selecting it (it turns blue), and then click on the "</>" icon at the upper left hand corner. Click on the "Save" button. Code tags can also be inserted manually in the forum text using the code and /code metatags.