Help with random display upon button press

Howdy all.

I am working on a father's day project that is essentially a small box that produces random messages I have specified with each button press, sort of like a magic 8 ball. I am using an mega 2560 and 1602 LCD. I have wired my lcd correctly but may have a glaring mistake with my button that I wish to remediate, as well as my code not doing what I would like it to.

My intention is to have a start message and then once the button is pressed it will select from a section of ~20 or so phrases randomly. I also want these phrases to populate both rows of my screen. I am not especially familiar with arduinos but any help is greatly appreciated. I will post my code and my current wiring status. Thanks! :blush:

#include <LiquidCrystal.h>

// Initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

// Array of messages (each pair of lines is a message)
const char *messages[][2] = {
  {"Hello, World!", "Line 2 Here!"},
  {"Arduino Rocks!", "Second Line!"},
  {"Press the Btn", "To Change Text"},
  {"LCD Display", "Second Line"},
  {"Random Msg 1", "Random Msg 2"},
};

const int numMessages = sizeof(messages) / sizeof(messages[0]);
const int buttonPin = 2;
int buttonState = 0;
int lastButtonState = 0;
int currentMessageIndex = 0;

void setup() {
  // Set up the LCD's number of columns and rows:
  lcd.begin(16, 2);

  // Print a default message to the LCD.
  lcd.print("Press the Btn");
  lcd.setCursor(0, 1);
  lcd.print("To Change Text");

  // Initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

void loop() {
  // Read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // Check if the pushbutton is pressed.
  // If it is, the buttonState is HIGH:
  if (buttonState != lastButtonState) {
    if (buttonState == HIGH) {
      // Generate a random index
      currentMessageIndex = random(numMessages);

      // Clear the LCD
      lcd.clear();

      // Set the cursor to the first column, first row
      lcd.setCursor(0, 0);

      // Print the random message first line
      lcd.print(messages[currentMessageIndex][0]);

      // Set the cursor to the first column, second row
      lcd.setCursor(0, 1);

      // Print the random message second line
      lcd.print(messages[currentMessageIndex][1]);
    }
    // Delay to avoid button bouncing issues
    delay(50);
  }

  // Save the current state as the last state,
  // for next time through the loop
  lastButtonState = buttonState;
}



Don't leave us hanging, what is the glaring mistake and what in terms of the code, what does/doesn't it do that you want?

Are you using pin 2 for both the LCD display and the button?

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

...
...
...

const int buttonPin = 2;

You can save some wiring and a resistor if you set buttonPin to INPUT_PULLUP mode and use the internal pullup resistor.

The pictures do not appear to match the code. Are you using direct wiring to the LCD, or an I2C backpack?

Sorry boss, I was just trying to build some suspense there. Joking, the message does not change when I hit the button, it is static on the same beginning message as shown in the photo. I hope this clears things up.

I see now that the way I coded this includes more pins than I'm actually using, the LCD I have has the soldered backpack included on it so it only uses the 5v, GND, SCL, and SDA. What would you suggest I change to remediate this? I found some example code that I hoped to emulate but I see now that it may be for a different LCD setup.

the LCD I have has the soldered backpack included on it so it only uses the 5v, GND, SCL, and SDA. What would you suggest I change to remediate this?

The best available library for the i2c lcd displays is called hd44780.h. written by Bill Perry. It is available through the library manager.

I would suggest that you convert to it. It is plug and play for a variety of configurations of the i2c backpack modules. It will detect both the i2c address and the configuration of the driver.

As part of the library there is a comprehensive diagnostic sketch which will test some internal details of the display. It's called I2CexpDiag

In the in the ide you can navigate to the library example

File>Examples>hd44780>ioclass>hd44780_I2Cexp>I2CexpDiag

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