using momentary push button as toggle

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;
}

Any help will be greatly appreciated. Thank you.

Use the state change detection example to trigger some code to run when the button changes from 'not pressed' to 'pressed'.

The code will invert a boolean variable.

boolean toggleState = false; // initial state

...

if((newState != prevState) and (newState == LOW))
{
    // button has been pressed
    toggleState = !toggleState;
}

PeterH:
Use the state change detection example to trigger some code to run when the button changes from 'not pressed' to 'pressed'.

The code will invert a boolean variable.

boolean toggleState = false; // initial state

...

if((newState != prevState) and (newState == LOW))
{
    // button has been pressed
    toggleState = !toggleState;
}

Can't seem to get this to work in my code. Not sure where to place these values

const int buttonPin = 1;

...

Serial.begin(9600);

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.

Here is one way to use a momentary push button as a toggle switch.

/*
 *    simple debounce using delay, will block for 5mSec when state changes
 */

const uint32_t debounceTime = 5;  // 5 mSec, enough for most switches
const uint8_t switchPin     = 8;  // with n.o. momentary pb switch to ground
const uint8_t ledPin        = 13; // built-in led

const bool switchOn  = false;     // using INPUT_PULLUP
const bool switchOff = true;

bool lastState   = switchOff;
bool newState    = switchOff;
bool toggleState = false;

void setup()
{
  
  Serial.begin ( 9600 );
  Serial.println ( F ( __FILE__ ) );
  Serial.println ( F ( "Simple state change/debounce using delay." ) );
  Serial.println ( F ( "push on, push off -- with momentary contact switch" ) );
  
  pinMode ( switchPin, INPUT_PULLUP );
  pinMode ( ledPin, OUTPUT );
  
} // setup

void loop ()
{

  newState = digitalRead( switchPin );

  if( lastState != newState ) // state changed
  {
    delay( debounceTime );
    lastState = newState;
    
    // push on, push off
    if( newState == switchOn && toggleState == false )
    {
      toggleState = true;
      digitalWrite( ledPin, HIGH );
      Serial.println( F ( "Switched ON" ) );
    }
    else if( newState == switchOn && toggleState == true )
    {
      toggleState = false;
      digitalWrite( ledPin, LOW );
      Serial.println( F ( "Switched OFF" ) );
    }

  }  // if state changed

} // loop

I hope this helps.

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.

Like so many people, you are confusing debouncing with what you really want, which is edge detection.

I like to use a struct for my buttons:

typdef struct
{
  unsigned char pin;
  unsigned char level;
  unsigned char edge;
} button_t;

That way, when you update the button, you can do this:

void update_button(button *b)
{
  unsigned char new_level = digitalRead( b->pin );

  if( b->level != new_level )
    b->edge = 1;
  else
    b->edge = 0;

  b->level = new_level;
}

I typed this up from memory, so there's no debouncing and there may be errors.

Then, when you want to check for a positive edge, use:

if( button.edge && (button.level==HIGH) )

// 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

void setup() {
pinMode(SwWhite, INPUT_PULLUP);
pinMode(En_LED_R, OUTPUT);
}

void loop() {
if(digitalRead(SwWhite)==0 && set == 0) {
delay(250); //delay of 250 is perfect to stop bouncing
set = !set;
digitalWrite(En_LED_R, LOW);
} else if(digitalRead(SwWhite) == 0 && set == 1) {
delay(250);
set = !set;
digitalWrite(En_LED_R, HIGH);
}
}

@msousa1,

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.