(Solved) Show latest change status (pin) on LCD display, how?

Hi,

I have i little question about the LCD display, i want to show on the 2e line of the LCD display, the latest status what happens with the arduino.

So i right a code (combine from lcd and button state i find on the forum), i know why he always show Led 1 is off or led 2 is off, but i don't know how i can fix it.

I must be like this, when i push on button 1, led 1 goes on, and in the display must sow "led 1 is on", wel i push again on the button, the led wil go off, and must show "led 1 is off", thats also for pushbutton/led2 , sound simple but, what goes wrong here, i see when i hold the pushbutton, the he wil show OFF.

Thanks alot for helping

Kind regards

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display

const int buttonPinA0 = A0;     // the number of the pushbutton pin 
const int buttonPinA1 = A1;     // the number of the pushbutton pin 
const int ledPin9 =  9;         // the number of the LED pin 
const int ledPin10 =  10;      // the number of the LED pin

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


void setup() {
  // initialize the LED  pin as an output:
  pinMode(ledPin9, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPinA0, INPUT);
  // initialize the LED  pin as an output:
  pinMode(ledPin10, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPinA1, INPUT);
  
  //Make analog pins digital
  pinMode(A0, OUTPUT);
  digitalWrite(A0, LOW);
  pinMode(A1, OUTPUT);
  digitalWrite(A1, LOW);
  
  // initialize serial communication:
  Serial.begin(9600);
    
  // initialize the lcd  
  lcd.init();                      
 
   for(int i = 0; i< 3; i++)
  {
    lcd.backlight();
    delay(250);
    lcd.noBacklight();
    delay(250);
  }
  lcd.backlight(); // finish with backlight on  
 }

void loop() {

 
  // read the pushbutton input pin:
  buttonState1 = digitalRead(buttonPinA0);
  // 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
      // wend from off to on:
      buttonPushCounter1++;
        lcd.setCursor(0,1);
        lcd.print("Led 1 is off");
      Serial.println(buttonPushCounter1);
    } 
    else {
      // if the current state is LOW then the button
      // wend from on to off:
         lcd.setCursor(0,1);
         lcd.print("Led 1 is on");; 
    }
  }
  // save the current state as the last state, 
  //for next time through the loop
  lastButtonState1 = buttonState1;
  // turns on the LED every four button pushes by 
  if (buttonPushCounter1 % 2 == 0) {
    digitalWrite(ledPin9, HIGH);
  } else {
   digitalWrite(ledPin9, LOW);
  }

  // read the pushbutton input pin:
  buttonState2 = digitalRead(buttonPinA1);
  // compare the buttonState to its previous state
  if (buttonState2 != lastButtonState2) {
    // if the state has changed, increment the counter
    if (buttonState2 == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounter2++;
        lcd.setCursor(0,1);
        lcd.print("Led 2 is off");
      Serial.println(buttonPushCounter2);
    } 
    else {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off");
         lcd.setCursor(0,1);
         lcd.print("Led 2 is on");;  
    }
  }
  // save the current state as the last state, 
  //for next time through the loop
  lastButtonState2 = buttonState2;
  // turns on the LED every four button pushes by 
  if (buttonPushCounter2 % 2 == 0) {
    digitalWrite(ledPin10, HIGH);
  } else {
   digitalWrite(ledPin10, LOW);
  }

  }

You are changing the message based on the switch being pressed or released, NOT on the state of the LED. Why?

Wel i don't know how to do that.
I have also try this:

    digitalWrite(ledPin10, HIGH);
         lcd.setCursor(0,1);
  lcd.print("Led 2 is off");
  } else {
   digitalWrite(ledPin10, LOW);
        lcd.setCursor(0,1);
  lcd.print("Led 2 is on");

But than the display is in a loop and can't find the code what says, if ledpin10 state is high, than....
Thats te reason i post this question

thanks

But than the display is in a loop and can't find the code what says, if ledpin10 state is high, than....

I have no idea what that is supposed to mean. I have no idea what the code you didn't post looks like, either.

Before you post any more code, use Tools + Auto Format to fix that mess.

Okay sorry, maybe my explanation is not so good.

The display must shows the last changes. So if led 1 is on, that it says, "led 1 is on", and if turn led 2 off, that it says "led 2 is off" ect.

What has now happened is that he every print to the lcd displays shows quickly through each other.

The code I have created is probably not good, i think it must should be something like this, but i don't really know:

If last update of ledpin9 is HIGH, show "led 1 is on", or when last update is ledpin10 is LOW, than show "led 2 is off"

This is the full code, with Auto Format to Fix what i have be created:

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display

const int buttonPinA0 = A0;     // the number of the pushbutton pin
const int buttonPinA1 = A1;     // the number of the pushbutton pin
const int ledPin9 =  9;         // the number of the LED pin
const int ledPin10 =  10;      // the number of the LED pin

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


void setup() {
  // initialize the LED  pin as an output:
  pinMode(ledPin9, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPinA0, INPUT);
  // initialize the LED  pin as an output:
  pinMode(ledPin10, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPinA1, INPUT);

  //Make analog pins digital
  pinMode(A0, OUTPUT);
  digitalWrite(A0, LOW);
  pinMode(A1, OUTPUT);
  digitalWrite(A1, LOW);

  // initialize serial communication:
  Serial.begin(9600);

  // initialize the lcd
  lcd.init();

  for (int i = 0; i < 3; i++)
  {
    lcd.backlight();
    delay(250);
    lcd.noBacklight();
    delay(250);
  }
  lcd.backlight(); // finish with backlight on
}

void loop() {


  // read the pushbutton input pin:
  buttonState1 = digitalRead(buttonPinA0);
  // 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
      // wend from off to on:
      buttonPushCounter1++;

      Serial.println(buttonPushCounter1);
    }
    else {
      // if the current state is LOW then the button
      // wend from on to off:

    }
  }
  // save the current state as the last state,
  //for next time through the loop
  lastButtonState1 = buttonState1;
  // turns on the LED every four button pushes by
  if (buttonPushCounter1 % 2 == 0) {
    digitalWrite(ledPin9, HIGH);
    lcd.setCursor(0, 1);
    lcd.print("Led 1 is on");
  } else {
    digitalWrite(ledPin9, LOW);
    lcd.setCursor(0, 1);
    lcd.print("Led 1 is off");
  }

  // read the pushbutton input pin:
  buttonState2 = digitalRead(buttonPinA1);
  // compare the buttonState to its previous state
  if (buttonState2 != lastButtonState2) {
    // if the state has changed, increment the counter
    if (buttonState2 == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounter2++;

      Serial.println(buttonPushCounter2);
    }
    else {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off");
    }
  }
  // save the current state as the last state,
  //for next time through the loop
  lastButtonState2 = buttonState2;
  // turns on the LED every four button pushes by
  if (buttonPushCounter2 % 2 == 0) {
    digitalWrite(ledPin10, HIGH);
    lcd.setCursor(0, 1);
    lcd.print("Led 2 is on");
  } else {
    digitalWrite(ledPin10, LOW);
    lcd.setCursor(0, 1);
    lcd.print("Led 2 is off");
  }

}

Why are you printing information about the state of LED 1 and the state of LED 2 in the same place?

Because i don't know how i must do that. thats the reason i make this topic in the forum for help/support.
where must i place de printing rule?

Perhaps you should draw a picture. Show us what you expect the LCD screen to look like.

The lcd.setCursor() method positions the cursor to where the first character is to be drawn.

he shows now:
Led 1 is on
Led 1 is off
Led 2 is on
Led 2 is off
ect ect in a loop.

This is what we see,

So, there must something else, he must show the latest changes on the display.

so how, when pinout is high, show led1 is on, after that, when somehting else change on the uno, example pinuot is LOW, then show led 1 is off....

sounds simple but i think its not so easy i understand

You are trying to display the status of both LEDs in one place. When you wise up and use BOTH lines, one for LED 1 and one for LED 2, I'm sure that the text being displayed will make more sense.

Thanks Paul,

But thats not what i want, on the first line there wil be comes what else. and there more buttons and pinouts than 2, i sow here de code for 2 buttons, if i know how this works than i can figure it out for the other but first..... how show we de latest changes in the display.

I think its not poisable right?

how show we de latest changes in the display.

First, you decide what you want to display, and where.

If the idea is to show, briefly, when the state of an LED changes, then you do NOT display the state on every pass through loop.

If the idea is to show the state of an LED, then you can NOT show both of the states IN THE SAME PLACE.

So, what IS it that you want to do?

if i push a button, the led wil goes on, i want to see in the LCD display, led is on.
If i press the button again, the led wil be off, than clear display and show led is off.

if i push button2, led2 wil goes on, i want to see in the LCD display, led 2 is on.
If i press button2 again, led2 will be off, than clear display and show led 2 is off.

Where: on the second line, not the first line, but that is not the problem, i understand how i can use the first and second line.

Only write to the LCD when the buttons become pressed or released, not when they are pressed or released. That way you will only see the message for the most recent change. Look at the StateChangeDetection example in the IDE to see how to do it. Basically you only act when the button state changes, not every time through loop().

if i push a button, the led wil goes on, i want to see in the LCD display, led is on.
If i press the button again, the led wil be off, than clear display and show led is off.

if i push button2, led2 wil goes on, i want to see in the LCD display, led 2 is on.
If i press button2 again, led2 will be off, than clear display and show led 2 is off.

How can you expect to see that LED 1 is on AND LED 2 is off, for instance, IN THE SAME SPACE?

When you have some reasonable expectations, you stand a chance of implementing them.

You haven't a snowball's chance in hell of implementing your impossible requirements, no matter how much you wish that it was possible.

Thanks, UkHeilBob and Paul,

I have found it, this is the code i was searching for:

    ledpinState10 = digitalRead(ledPin10);
    if (ledpinState10 == HIGH) {
      lcd.setCursor(0, 1);
      lcd.print("Led 1 is off ");
    }
    else
    {
      lcd.setCursor(0, 1);
      lcd.print("Led 1 is on");

ect ect..

Kind regards

That code snippet does not do what you said you wanted in reply #13

if i push a button, the led wil goes on, i want to see in the LCD display, led is on. 
If i press the button again, the led wil be off, than clear display and show led is off.