Hi,
i’ve made a code to have 2 different texts switching in a loop every time a physical button (connected to input 12 and the ground with no resistor) is pressed on my Arduino Uno and displayed on 20x4 Velleman 450 LCD Screen.
The problem is i’ve used a slice of code to make the second text blink, and after this blinking text, the button doesn’t go back to the first text, so i think this is the problematic part of the code, though it compiles fine. I’m not knowledgeable enough to correct what’s causing the problem.
The basic text switching code is from the forum i think with annotations, so i leave them as this can be useful to sort this out.
Thanks if anyone can help me with that.
Here’s the code :
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
//create an object called lcd, with address 0x3F
LiquidCrystal_I2C lcd(0x27, 20, 4);
const byte buttonPin = 12;
int buttonPushCounter = 0; // counter for the number of button presses
boolean buttonState = LOW; // current state of the button
boolean lastButtonState = LOW; // previous state of the button
// lcd constructor made global in scope so the whole program can sse it
void setup()
{
Serial.begin(9600);
lcd.begin();
// Print a message to the LCD.
lcd.print("Hello All");
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT_PULLUP);
}
void loop()
{
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState)
{
if (buttonState == HIGH)
{
// if the current state is HIGH then the button
// went from off to on:
buttonPushCounter++; // add one to counter
lcd.clear();
if (buttonPushCounter > 2) // if counter over 3 reset the counter to 1 to show "Jon"
// and not "Hello All"
{
buttonPushCounter = 1;
}
Serial.println(buttonPushCounter);
switch (buttonPushCounter) // choose what to display based on buttonPushCounter value
{
case 0:
lcd.print("Hello All"); // show "Hello All until first button press
break;
case 1:
lcd.setCursor(0, 0);
lcd.print(" Step 1");
break;
case 2:
lcd.setCursor(0,0);
lcd.print(" Step 2 ");
for (int counttwo =0; counttwo <= 200; counttwo++)
{
lcd.setCursor(0,2);
lcd.print(" ");
delay(500);
lcd.setCursor(0,2);
lcd.print(" blink test ");
delay(500);
}
break;
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
}
}