Text switching to blinking text on LCD with button

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

If the switch is wired to ground and the internal pullup is enabled the switch will read HIGH when NOT pressed and LOW when pressed. The code in the tutorial is for and active HIGH switch (HIGH when pressed). My tutorial on active low switch state change detection may be of interest.

Your code would be more readable if you use the IDE autoformat tool (ctrl-t or Tools, Auto Format) to more properly indent the code.

Thanks for your quick response,
i’ll go read your tutorial,
but my question remains,
it works perfectly when displaying regular text, looping everytime the button is pushed,
but as soon as i put this blinking code, it doesn’t work anymore :

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

Isn’t there an easy fix for this as i’m really a beginner and would like to look into this simple use first ?

That for loop lasts for 200 seconds. Did you wait long enough.

The delay(500) used in the blink routine in the for loop is blocking, and you can not read a button, or do anything else for 200 seconds while blinking in case2. You will continue to reenter case 2.

The program should be recrafted to use non blocking techniques.

If you want to go back to the first text after the blocking blink period, just set the buttonPushCounter = 1 (or perhaps =0 depending on what you want to see on the display) after the blocking loop.

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);
  }
  buttonPushCounter = 1;

You’re absolutely right, it works if i wait for 200 seconds, when the blinking stops, thanks !

But, i still would like to have it work with the button and blinking infinitely…
So how would you go about making it non blocking ?

Is it too complex for a beginner like me ?

Non-blocking timing tutorials:
Several things at a time.
Beginner’s guide to millis().
Blink without delay().

And I would split up the reading the switch and the handling of the LCD. You need to read the switches pretty often, say, 20 to 50 times a second. The switch is checked 50 times a second in my state change detection tutorial.

But the LCD does not need updating but maybe 1 to 3 times a second.

A state machine which blinks an LED and does other stuff, too - Timer counter state change toggle FSM - all at once

Auto response :
the easiest way to use this code and return to the 1st text was to wire another button to the RESET pin. Simple as that.

Thanks to the others for helping out !

Can you share the final code. I have some similar kind of requirement

I would suggest that you start your own thread. Tell us what you have. Post a schematic. Tell us what you want to do.

The code hasn’t changed, i just wired a RESET button to the Reset pin.

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