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! ![]()
#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;
}


