Beginner Question Buttons & Display

I have this Code and it sort of Works but not really what i want to do. Please help me with a piece / snipplet of code correction?

If i press button one i want LED1 start blinking (like in the standard blink code) and if i press the button again i want to LED1 to stop blinking. same for LED2

Can i accomplish this by set the button state into the memory? and then let the LED blink as long as the state is 1 and stop when the state is 0? if yes, how do i do that? kindly ask for help

btw...i used "real" pullup resistors instead of the interna pul up as it did not work on button 2. button 1 was fine but button 2 was flickering. so i used real resistors and it works perfect.

here is my code:

/*
  ** DemoGerman **
  Print German text (UTF-8, NFC normalizaed) to
  LCD1602 (European ROM type) via I2C (PCF8574).

  Author: Loc P. Le <phuocloc@gmail.com>

  ** Class **
  - LCDI2C_Latin: for printing European languages (French, Spanish,
    German, Finnish, ...) to LCDs with Latin ROM (European ROM).

  ** Help **
  https://github.com/locple/LCDI2C_Multilingual
*/

#include <LCDI2C_Multilingual.h>

LCDI2C_Latin lcd(0x27, 16, 2);        // I2C address: 0x27; Display size: 16x2
const int ledPin1 = 14;  
const int ledPin2 = 12; 
int buttonState = 0; 

void setup() {
  lcd.init();          // LCD initialisieren
  lcd.backlight();     // Hintergrundbeleuchtung einschalten
  lcd.setCursor(0, 0); // Cursor auf Zeile 0, Spalte 0 setzen
  lcd.print("Test");
  delay(1000);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Initialize!");
  lcd.setCursor(0,1);
  lcd.print("Please Wait....");
  delay(1000);
  lcd.print("Ready");
  delay(1000);
  lcd.setCursor(0,1);
  lcd.print("LED 1 Ready");
  pinMode(12, INPUT_PULLUP);
  pinMode(15, INPUT_PULLUP);
  pinMode(14, OUTPUT);
  pinMode(12, OUTPUT);
  

  //lcd.clear();
}

void loop() {
  int sensorVal1 = digitalRead(15);
  int sensorVal2 = digitalRead(16);
  Serial.println(sensorVal1);
  Serial.println(sensorVal2);
  
   

  
  if ((sensorVal1 == LOW) && (sensorVal2 == LOW)) {
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("LED 1 on");
  lcd.setCursor(0,1);
  lcd.print("LED 2 on");
  delay(100);
    }

    if ((sensorVal1 == HIGH) && (sensorVal2 == LOW)) {
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("LED 1 off");
  lcd.setCursor(0,1);
  lcd.print("LED 2 on");
  delay(100);
    }

    if ((sensorVal1 == HIGH) && (sensorVal2 == HIGH)) {
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("LED 1 off");
  lcd.setCursor(0,1);
  lcd.print("LED 2 off");
  delay(100);
    }
   
if ((sensorVal1 == LOW) && (sensorVal2 == HIGH)) {
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("LED 1 on");
  lcd.setCursor(0,1);
  lcd.print("LED 2 off");
  delay(100);
    }



   if (sensorVal1 == HIGH) {
    digitalWrite(ledPin1, LOW);
   
  } else {
    digitalWrite(ledPin1, HIGH);
    
  }

   if (sensorVal2 == HIGH) {
    digitalWrite(ledPin2, LOW);
   
  } else {
    digitalWrite(ledPin2, HIGH);
   
  }



}

  • Please confirm your switches are wired as S2 below.

  • Are you using an UNO .





  pinMode(12, INPUT_PULLUP);
  pinMode(15, INPUT_PULLUP);
  pinMode(14, OUTPUT);
  pinMode(12, OUTPUT

  • Please draw us a schematic.

That was a wiring problem, not an internal pullup resistor problem.

At each button press, change the state of the LED, and preserve the LED state in a variable.

i solved the wiring problem and i fount the appropriate code in the examples but.... oh my...haha. the two LED's work perfect. i can turn them on and off with the press of the buttons but the display does not what i want to do.
it states:
LED 1 Off
LED 2 Off

while i press the button the display says LED 1 On but when i release the button it jumps back to LED 1 OFF
the LED 1 however stays on or off exatcly how i want it... i am a beginner so i am a bit confused.
Any Help is very much aprechiated. thank you

here is my code:

#include <LCDI2C_Multilingual.h>

LCDI2C_Latin lcd(0x27, 16, 2);    



// this constant won't change:
const int ledPin1 = 12; 
const int ledPin2 = 14;
const int buttonPin1 = 15; 
const int buttonPin2 = 16;
    // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter1 = 0;  // counter for the number of button presses
int buttonState1 = 0;        // current state of the button
int lastButtonState1 = 0;    // previous state of the button

int buttonPushCounter2 = 0;  // counter for the number of button presses
int buttonState2 = 0;        // current state of the button
int lastButtonState2 = 0;    // previous state of the button


void setup() {

  lcd.init();          // LCD initialisieren
  lcd.backlight();  

  // initialize the button pin as a input:
  pinMode(buttonPin1, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin1, OUTPUT);
  // initialize serial communication:

   // initialize the button pin as a input:
  pinMode(buttonPin2, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin2, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {

  int sensorVal1 = digitalRead(15);
  int sensorVal2 = digitalRead(16);
  Serial.println(sensorVal1);
  Serial.println(sensorVal2);

  

  // read the pushbutton input pin:
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);
  // compare the buttonState to its previous state
  if (buttonState1 != lastButtonState1) {
    // if the state has changed, increment the counter
    if (buttonState1 == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter1++;
      
    } else {
      // if the current state is LOW then the button went from on to off:
     
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }

  if (buttonState2 != lastButtonState2) {
    // if the state has changed, increment the counter
    if (buttonState2 == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter2++;
      
    } else {
      // if the current state is LOW then the button went from on to off:
     
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState1 = buttonState1;
  lastButtonState2 = buttonState2;

  // turns on the LED every four button pushes by checking the modulo of the
  // button push counter. the modulo function gives you the remainder of the
  // division of two numbers:
  if (buttonPushCounter1 % 2== 0)  {
    digitalWrite(ledPin1, HIGH);
    } else {
    digitalWrite(ledPin1, LOW);
    
  }

  if (buttonPushCounter2 % 2== 0) {
    digitalWrite(ledPin2, HIGH);
    
  } else {
    digitalWrite(ledPin2, LOW);
    }

   

    if ((sensorVal1 == LOW) && (sensorVal2 == LOW)) {
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("LED 1 on");
  lcd.setCursor(0,1);
  lcd.print("LED 2 on");
  delay(100);
    }

    if ((sensorVal1 == HIGH) && (sensorVal2 == LOW)) {
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("LED 1 off");
  lcd.setCursor(0,1);
  lcd.print("LED 2 on");
  delay(100);
    }

    if ((sensorVal1 == HIGH) && (sensorVal2 == HIGH)) {
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("LED 1 off");
  lcd.setCursor(0,1);
  lcd.print("LED 2 off");
  delay(100);
    }
   
if ((sensorVal1 == LOW) && (sensorVal2 == HIGH)) {
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("LED 1 on");
  lcd.setCursor(0,1);
  lcd.print("LED 2 off");
  delay(100);
    }

}

Your code is displaying the states of the buttons, not the LEDs. The text that appears on the display says that the LEDs are on/off but the criteria used is based on the button states. You need to learn to read your own code in an objective way, putting aside what you know you want it to do. Imagine a friend gave you the code without telling you what it does and you working out for yourself what it will do by reading the code line by line.

1 Like

If the LEDs display the correct state, but the display does not display the correct state, make the display show the state of the LED.

Your code has many empty conditions, looking something like this... Could they be of use?

    } else {
      // if the current state is X then the button went from Y to Z
    }

When you copy the programs, you should type them by hand. This will let you see the flow of the language. The state shown on the display uses the same language as the state of the LED, but with a few specific words (LED, display, button). They follow a logical order. Learn the order as you progress. You will learn the more you try.

I can confirm i used Cinfiguration #2

ok, so i tried this and nothing works... display tells me on line 2: LED 2 on
LED's stay off and buttons have no effect... i start to be desperate and am about to give up, i have tried many possible ways...

#include <LCDI2C_Multilingual.h>

LCDI2C_Latin lcd(0x27, 16, 2);    



// this constant won't change:
const int ledPin1 = 12; 
const int ledPin2 = 14;
const int buttonPin1 = 15; 
const int buttonPin2 = 16;
    // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter1 = 0;  // counter for the number of button presses
int buttonState1 = 0;        // current state of the button
int lastButtonState1 = 0;    // previous state of the button

int buttonPushCounter2 = 0;  // counter for the number of button presses
int buttonState2 = 0;        // current state of the button
int lastButtonState2 = 0;    // previous state of the button


void setup() {

  lcd.init();          // LCD initialisieren
  lcd.backlight();  

  // initialize the button pin as a input:
  pinMode(1, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin1, OUTPUT);
  // initialize serial communication:

   // initialize the button pin as a input:
  pinMode(2, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin2, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {

  int sensorVal1 = digitalRead(15);
  int sensorVal2 = digitalRead(16);
  Serial.println(sensorVal1);
  Serial.println(sensorVal2);

  

  // read the pushbutton input pin:
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);
  // compare the buttonState to its previous state
  if ((buttonState1 != lastButtonState1) && (buttonState2 != lastButtonState2)){
    // if the state has changed, increment the counter
    if ((buttonState1 == HIGH) && (buttonState2 == HIGH))
    {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter1++;
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("LED 1 on");
      } else {
      buttonPushCounter2++;
      lcd.clear();
      lcd.setCursor(0,1);
      lcd.print("LED 2 on");
    }
    // Delay a little bit to avoid bouncing
    delay(50);

  if ((buttonState1 == HIGH) && (buttonState2 == LOW))
    {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter1++;
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("LED 1 on");
      } else {
      buttonPushCounter2++;
      lcd.clear();
      lcd.setCursor(0,1);
      lcd.print("LED 2 off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);

  if ((buttonState1 == LOW) && (buttonState2 == LOW))
    {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter1++;
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("LED 1 off");
      } else {
      buttonPushCounter2++;
      lcd.clear();
      lcd.setCursor(0,1);
      lcd.print("LED 2 off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);

  if ((buttonState1 == LOW) && (buttonState2 == HIGH))
    {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter1++;
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("LED 1 off");
      } else {
      buttonPushCounter2++;
      lcd.clear();
      lcd.setCursor(0,1);
      lcd.print("LED 2 on");
    }
    // Delay a little bit to avoid bouncing
    delay(50);




  }

 
  // save the current state as the last state, for next time through the loop
  lastButtonState1 = buttonState1;
  lastButtonState2 = buttonState2;

  // turns on the LED every four button pushes by checking the modulo of the
  // button push counter. the modulo function gives you the remainder of the
  // division of two numbers:
  if (buttonPushCounter1 % 2== 0)  {
    digitalWrite(ledPin1, HIGH);
    } else {
    digitalWrite(ledPin1, LOW);
    
  }

  if (buttonPushCounter2 % 2== 0) {
    digitalWrite(ledPin2, HIGH);
    
  } else {
    digitalWrite(ledPin2, LOW);
    }

   


}

this is the code from scratch with working buttons and led's but no display. can anyone write me the display code? i can then try and analyse what i did wrong. please?

#include <LCDI2C_Multilingual.h>

LCDI2C_Latin lcd(0x27, 16, 2);    



// this constant won't change:
const int ledPin1 = 12; 
const int ledPin2 = 14;
const int buttonPin1 = 15; 
const int buttonPin2 = 16;
    // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter1 = 0;  // counter for the number of button presses
int buttonState1 = 0;        // current state of the button
int lastButtonState1 = 0;    // previous state of the button

int buttonPushCounter2 = 0;  // counter for the number of button presses
int buttonState2 = 0;        // current state of the button
int lastButtonState2 = 0;    // previous state of the button


void setup() {

  lcd.init();          // LCD initialisieren
  lcd.backlight();  

  // initialize the button pin as a input:
  pinMode(1, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin1, OUTPUT);
  // initialize serial communication:

   // initialize the button pin as a input:
  pinMode(2, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin2, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {

  int sensorVal1 = digitalRead(15);
  int sensorVal2 = digitalRead(16);
  Serial.println(sensorVal1);
  Serial.println(sensorVal2);

  

  // read the pushbutton input pin:
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);
  // compare the buttonState to its previous state
  if (buttonState1 != lastButtonState1) {
    // if the state has changed, increment the counter
    if (buttonState1 == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter1++;
    
      } else {
    
     
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }

  if (buttonState2 != lastButtonState2) {
    // if the state has changed, increment the counter
    if (buttonState2 == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter2++;
      
    } else {
      // if the current state is LOW then the button went from on to off:
     
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState1 = buttonState1;
  lastButtonState2 = buttonState2;

  // turns on the LED every four button pushes by checking the modulo of the
  // button push counter. the modulo function gives you the remainder of the
  // division of two numbers:
  if (buttonPushCounter1 % 2== 0)  {
    digitalWrite(ledPin1, HIGH);
    } else {
    digitalWrite(ledPin1, LOW);
    
  }

  if (buttonPushCounter2 % 2== 0) {
    digitalWrite(ledPin2, HIGH);
    
  } else {
    digitalWrite(ledPin2, LOW);
    }

   


}

We all feel that... to this day... then we read again... to save the day

Sure... then why should anyone help you? I think you should remove this thought from your mind, lips and fingertips. Never, ever, give up.

We can help... you will do the writing.

Take a moment to do some reading, writing and thinking thinking and writing...

Did you see this?

Avoid "magic numbers"... try...

  int sensorVal1 = digitalRead(buttonPin1);
  int sensorVal2 = digitalRead(buttonPin2);

Your buttons are read correctly.

Your LEDs show (correctly) that the buttons WERE pressed, because you stored the state with buttonPushCounter1 and buttonPushCounter2. The LEDs change state only when the button is pressed.

Your LCD shows when the buttons ARE BEING pressed, but do not show that the buttons WERE pressed (as you did with the LEDs). To fix your LCD data, use a similar "buttons WERE pressed" method with the LCD data. Here is the LED (good) state storing mechanism...

  if (buttonPushCounter1 % 2== 0)  {
    digitalWrite(ledPin1, HIGH);
    } else {
    digitalWrite(ledPin1, LOW); 
  }

These two are the same thing... only need one... but also need to "flag" that a button was pressed...

  int sensorVal1 = digitalRead(buttonPin1);
  int sensorVal2 = digitalRead(buttonPin2);

  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);

Hi Randy, I have just written a tutorial proposal that is close to my heart - it relates to button inputs but is primarily about creating maintainable code. If you extract as much as you can from loop() into functions, you will be able to individually test each function by commenting out the call to the other functions in your loop().

Rather than writing code that reads pins directly, use libraries (at a minimum Bounce2) so you can focus on your requirements rather than the low level stuff.

HTH: Tutorial Proposal: Using Physical Inputs (Buttons, Encoders, Joysticks, Switches, Potentiometers)

PS there's an example in the library that toggles an LED :slight_smile:

It is not code dump time, but since hearts have been broken...

@RandyTomlinson - Your code is over-complicated. I suspect it started as a one-button, one-LED set/reset, state-change, button debounce sketch that grew into two-and-two with an LCD... but then lost track of what-goes-where. I am a proponent of "low level stuff" because it, unforgivably, shows me when I make mistakes that might be absorbed by a library or libraries. If you are still with me, try this sketch (be sure to use your own pin assignments). This sketch keeps track of which button was pressed, states of LEDs and displays the data. Through this "low level stuff" you will see the path of reading buttons, storing states, lighting LEDs according to states and displaying data. Never give up.

byte buttonpin[] = {2, 3}; // button pin array
byte ledpin[] = {4, 5}; // led pin array
const byte pins = sizeof(buttonpin) / sizeof(buttonpin[0]);
bool state[pins]; // states
byte i; // index

void setup() {
  Serial.begin(115200); // start serial comm
  for (int i = 0; i < 2; i++) { // index
    pinMode(buttonpin[i], INPUT_PULLUP); // configure button pins
    pinMode(ledpin[i], OUTPUT); // configure led pins
  }
  display(i); // show state of pins
}

void loop() {
  if (!digitalRead(buttonpin[i])) { // read button pin
    delay(150); // cheap button debounce
    state[i] = !state[i]; // change state of button pressed
    display(i); // show current states
  }
  i = !i; // change index
}

void display(byte i) { // format and display states
  Serial.print("button ");
  Serial.print(i); // button pin
  Serial.print(" LED");
  Serial.print(i); // led pin
  Serial.print(" state ");
  digitalWrite(ledpin[i], state[i]); // set led state
  Serial.println(state[i]); // show led state
}

Try it in a simulation: buttonLEDstate - Wokwi ESP32, STM32, Arduino Simulator

Ask any question.

1 Like

@RandyTomlinson @stutchbury - I made some changes to your original code AND I removed most of the comments. Read though this and ADD COMMENTS BACK and compare your sketch in this post against my sketch in Post #13... this will help you see the flow of your program. While you are reading, commenting and comparing, keep "seeing" where your code fits in:

INPUT - read a button
PROCESS - flag when button was pressed
OUTPUT - write to display, light the LED

#include <LCDI2C_Multilingual.h>
LCDI2C_Latin lcd(0x27, 16, 2);    

const int ledPin1 = 12; 
const int ledPin2 = 14;
const int buttonPin1 = 15; 
const int buttonPin2 = 16;

int buttonPushCounter1 = 0;  // counter for the number of button presses
int buttonState1 = 0;        // current state of the button
int lastButtonState1 = 0;    // previous state of the button
int buttonPushCounter2 = 0;  // counter for the number of button presses
int buttonState2 = 0;        // current state of the button
int lastButtonState2 = 0;    // previous state of the button

void setup() {
  lcd.init();          // LCD initialisieren
  lcd.backlight();
  pinMode(buttonPin1, INPUT_PULLUP);
  pinMode(ledPin1, OUTPUT);

  pinMode(buttonPin2, INPUT_PULLUP);
  pinMode(ledPin2, OUTPUT);

  Serial.begin(9600);
}
void loop() {
  int sensorVal1 = digitalRead(buttonPin1);
  int sensorVal2 = digitalRead(buttonPin2);

  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);

  if (buttonState1 != lastButtonState1) {
    if (buttonState1 == HIGH) {
      buttonPushCounter1++;
    }
    delay(50);
  }

  if (buttonState2 != lastButtonState2) {
    if (buttonState2 == HIGH) {
      buttonPushCounter2++;
    }
    delay(50);
  }

  lastButtonState1 = buttonState1;
  lastButtonState2 = buttonState2;

  if (buttonPushCounter1 % 2 == 0)  {
    digitalWrite(ledPin1, HIGH);
    sensorVal1 = LOW;
  } else {
    digitalWrite(ledPin1, LOW);
    sensorVal1 = HIGH;
  }

  if (buttonPushCounter2 % 2 == 0) {
    digitalWrite(ledPin2, HIGH);
    sensorVal2 = LOW;
  } else {
    digitalWrite(ledPin2, LOW);
    sensorVal2 = HIGH;
  }

  lcd.clear();
  if ((sensorVal1 == LOW) && (sensorVal2 == LOW)) {
    lcd.setCursor(0, 0);
    lcd.print("LED 1 on");
    lcd.setCursor(0, 1);
    lcd.print("LED 2 on");
    delay(100);
  }
  if ((sensorVal1 == HIGH) && (sensorVal2 == LOW)) {
    lcd.setCursor(0, 0);
    lcd.print("LED 1 off");
    lcd.setCursor(0, 1);
    lcd.print("LED 2 on");
    delay(100);
  }
  if ((sensorVal1 == HIGH) && (sensorVal2 == HIGH)) {
    lcd.setCursor(0, 0);
    lcd.print("LED 1 off");
    lcd.setCursor(0, 1);
    lcd.print("LED 2 off");
    delay(100);
  }
  if ((sensorVal1 == LOW) && (sensorVal2 == HIGH)) {
    lcd.setCursor(0, 0);
    lcd.print("LED 1 on");
    lcd.setCursor(0, 1);
    lcd.print("LED 2 off");
    delay(100);
  }
}

@xfpd - very kind of you but I think you meant @RandyTomlinson :wink:

Yes. Thank you, and sorry.

1 Like

i solved the problem. i am using a 16x2 display. here is the code:

#include <LCDI2C_Multilingual.h>

LCDI2C_Latin lcd(0x27, 16, 2);    

const int ledPin1 = 12; 
const int ledPin2 = 14;
const int buttonPin1 = 15; 
const int buttonPin2 = 16;

// Variablen
int buttonState1 = 0;
int lastButtonState1 = 0;

int buttonState2 = 0;
int lastButtonState2 = 0;

bool ledState1 = false; // Zustand von LED 1
bool ledState2 = false; // Zustand von LED 2

void setup() {
  lcd.init();
  lcd.backlight();  

  // Pins initialisieren
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);

  Serial.begin(9600);

  // Anfangszustand der LEDs und Anzeige setzen
  updateDisplay();
}

void loop() {
  // Tastenstatus auslesen
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);

  // Button 1 Logik
  if (buttonState1 != lastButtonState1) {
    if (buttonState1 == HIGH) {
      ledState1 = !ledState1; // Zustand von LED 1 umschalten
      digitalWrite(ledPin1, ledState1 ? HIGH : LOW); // LED aktualisieren
      updateDisplay(); // Anzeige aktualisieren
    }
    delay(50); // Entprellen
  }
  lastButtonState1 = buttonState1;

  // Button 2 Logik
  if (buttonState2 != lastButtonState2) {
    if (buttonState2 == HIGH) {
      ledState2 = !ledState2; // Zustand von LED 2 umschalten
      digitalWrite(ledPin2, ledState2 ? HIGH : LOW); // LED aktualisieren
      updateDisplay(); // Anzeige aktualisieren
    }
    delay(50); // Entprellen
  }
  lastButtonState2 = buttonState2;
}

// Hilfsfunktion zum Aktualisieren der LCD-Anzeige
void updateDisplay() {
  lcd.setCursor(0, 0); // Erste Zeile
  lcd.print("LED 1 ");
  lcd.print(ledState1 ? "ON " : "OFF"); // LED 1 Status anzeigen
  lcd.print("  "); // Leerzeichen zur Löschung alter Zeichen
  
  lcd.setCursor(0, 1); // Zweite Zeile
  lcd.print("LED 2 ");
  lcd.print(ledState2 ? "ON " : "OFF"); // LED 2 Status anzeigen
  lcd.print("  "); // Leerzeichen zur Löschung alter Zeichen

  // Debug-Ausgabe
  Serial.print("LED 1: ");
  Serial.println(ledState1 ? "ON" : "OFF");
  Serial.print("LED 2: ");
  Serial.println(ledState2 ? "ON" : "OFF");
}

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.