Too many initializers error when compiling

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

Because you gave it 52 values for the 50 elements?

HAHA, Yes that would be the problem. SILLY ME! that's what happens when you write code at 3am in the morning.

Silly you. You didn't let the compiler figure out the array's size for you. It's much better at counting than you are. Also, how about some line breaks to make it more readable?

  String wordBank[] = {"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"
                      };

  const size_t numWords = sizeof(wordBank) / sizeof(wordBank[0]);
2 Likes

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