Arduino (C/C++) Code To Display Contents of Array on LCD

3.3k Views Asked by At

I've tried to do as much researching as I could before posting this, but I am new to programming, so my general ignorance is at this point preventing me from really being able to know how to ask the right questions.

Current goals:

  1. Building an array that stores 50+ English words/phrases;
  2. Access the array on my Arduino, and have individual words/phrases display on my LCD; and
  3. Toggle through words/phrases by clicking a button on the Arduino.

Hardware Specs: SainSmart UnoR3, LCD based on HD44780

Issue: Writing a code that will display a new word when I push a button.

Code for "Hello, world!" LCD

void setup() {
 // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("hello, world!");
}

void loop() {
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  lcd.print(millis()/1000);
}

Code for random string from an array

#include <stdio.h>
#include <stdlib.h>

int main() {
    const char *messages[] = {
        "Hello!",
        "How are you?",
        "Good stuff!"
    };
    const size_t messages_count = sizeof(messages) / sizeof(messages[0]);
    char input[64];
    while (1) {
        scanf("%63s", input);
        printf("%s\n", messages[rand() % messages_count]);
    }
    return 0;
}
1

There are 1 best solutions below

0
On

I also have an Arduino Uno and an LCD display. Your task is going to be to both debug the hardware and the software. So, let me ask some questions.

In your code listing, when you run the sketch do you get a "hello world!" display at the LCD?

How is the main() that you provided relevant to this problem. Specifically, where is main() running? I hope it is NOT part of your sketch!!

In your loop() you do NOT have a delay. At a starting programmer... usually when displaying something you want to pause for a few seconds, otherwise you are going to drive the LCD with thousands of changes per second.

So add a delay(3000); statement to delay by 3 seconds (3,000 milliseconds), between updates to the LCD.

Next, in the 'loop()` you will need to test for a button push, but for now just get the LCD to display.

Please do this stuff and update your question accordingly, and I will follow-up with more suggestions/questions.