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