HI Guys,
Hope someone can help me. I've got the following code which selects a random word from an array of words, displays it on a screen and uses the talkie library to speak the word too.
When I go to compile it, I get the error message too many initializers for 'String [50]'.
Does anyone know what I can do to resolve it?
Code below:
// Include necessary libraries
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Talkie.h>
// Define OLED screen dimensions
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
// Define OLED screen pins
#define OLED_RESET 4
#define OLED_DC 5
#define OLED_CS 6
// Initialize OLED screen object <- NEW ERROR MESSAGE
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Define speaker pin
#define SPEAKER_PIN 7
// Define word bank //<- Error here solved (TOO MANY WORDS)
String wordBank[50] = {"apple", "banana", "carrot", "dog", "elephant", "flower", "giraffe", "house", "ice cream", "jungle", "kangaroo", "lion", "monkey", "nest", "ocean", "penguin", "queen", "rainbow", "sun", "tiger", "umbrella", "volcano", "watermelon", "xylophone", "yacht", "zebra", "airplane", "butterfly", "cactus", "dolphin", "eagle", "fire", "guitar", "honey", "island", "jellyfish", "koala", "lemon", "mountain", "night", "octopus", "panda", "quilt", "rocket", "snake", "tulip", "unicorn", "violet", "whale", "xylophone", "yogurt", "zeppelin"};
// Define array to keep track of used words
bool usedWords[50] = {false};
// Function to generate random number between 10 and 45
int generateRandomTime() {
return random(10, 46);
}
// Function to display word on OLED screen
void displayWord(String word) {
// Clear OLED screen
display.clearDisplay();
// Set text size and color
display.setTextSize(2);
display.setTextColor(WHITE);
// Set cursor position
display.setCursor(0, 0);
// Print word on OLED screen
display.println(word);
// Display on OLED screen
display.display();
}
// Function to say word through speaker using Talkie library
void sayWord(String word) {
// Convert word to char array
char wordArray[word.length() + 1];
word.toCharArray(wordArray, word.length() + 1);
// Say word through speaker
Talkie.say(wordArray);
}
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Initialize OLED screen
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
// Initialize speaker
pinMode(SPEAKER_PIN, OUTPUT);
// Seed random number generator
randomSeed(analogRead(0));
}
void loop() {
// Generate random index to select word from word bank
int index = random(0, 50);
// Check if word has already been used
if (!usedWords[index]) {
// Get word from word bank
String word = wordBank[index];
// Display word on OLED screen
displayWord(word);
// Say word through speaker
sayWord(word);
// Mark word as used
usedWords[index] = true;
// Wait for random time
delay(generateRandomTime() * 1000);
}
}
Thanks in Advance.
Brett