too many initializers for 'char []'

I’m trying to play a song through a piezo buzzer, it worked before but after I added more notes and code it stopped working.

void play( char note, int beats)
  int numNotes = 19;  // number of notes in our note and frequency array (there are 15 values, but arrays start at 0)
  //Note: these notes are C major (there are no sharps or flats)

  //this array is used to look up the notes
  char notes[] = { "b2", 'c', "c#", 'd', 'e', 'f', "gb", 'g', "ab", 'a', "bb", 'b', 'C', "Db", 'D', 'E', 'F', 'G', 'A', 'B', ' '};
  //this array matches frequencies with each letter (e.g. the 4th note is 'f', the 4th frequency is 175)
  int frequencies[] = {123.5, 131, 138.59, 147, 165, 175, 185, 196, 207.65, 220, 233, 247, 262, 277.18, 294, 330, 349, 392, 440, 494, 0};
  int currentFrequency = 0;    //the frequency that we find when we look up a frequency in the arrays
  int beatLength = 150;   //the length of one beat (changing this will speed up or slow down the tempo of the song)

  //look up the frequency that corresponds to the note
  for (int i = 0; i < numNotes; i++)  // check each value in notes from 0 to 14
    if (notes[i] == note)             // does the letter passed to the play function match the letter in the array?
      currentFrequency = frequencies[i];   // Yes! Set the current frequency to match that note

  //play the frequency that matched our letter for the number of beats passed to the play function
  tone(speakerPin, currentFrequency, beats * beatLength);   
  delay(beats* beatLength);   //wait for the length of the tone so that it has time to play
  delay(50);                  //a little delay between the notes makes the song sound more natural

./opt/arduino-builder/arduino-builder -compile -core-api-version 10611 -hardware opt/arduino-builder/hardware -hardware ./opt/cores -tools opt/arduino-builder/tools -tools ./opt/tools -built-in-libraries opt/libraries/latest -logger humantags -fqbn arduino:avr:uno -build-cache /tmp -build-path /tmp/825536684/build -verbose -libraries /tmp/825536684/custom -libraries /tmp/825536684/pinned /tmp/825536684/sketch_oct24a

Compiling sketch...

/tmp/825536684/sketch_oct24a/sketch_oct24a.ino: In function 'void play(char, int)':

/tmp/825536684/sketch_oct24a/sketch_oct24a.ino:121:128: error: too many initializers for 'char []'

exit status 1

I'm going to guess the problem is "b2", etc. That's s string literal, it's not a type char.

Take a look at the [u]Play Melody Example[/u]. It uses #define macros, and you should be able to use any allowable variable names to re represent each frequency. .

Also 123.5 is not an integer. I don't know if the digits to the right of the decimal point will simply be thrown-away, or if that's going to generate an error/bug.

And, tone() takes an integer so you can't get more than integer precision anyway... And, because it's done with a frequency divider and some frequencies don't divide evenly, you don't always get the exact frequency you're asking for (but it should be close enough for music).


error: too many initializers for 'char '

Sometimes the error message is misleading.... The compiler doesn't know what you're trying to do, it's just knows you're "saying" something that doesn't make sense. If you are lucky it will point to the line of code where the problems is, but sometimes it gets that wrong too.

I've one misplaced curly-bracket and I got hundreds of errors (in a big program). None of the error messages said anything about a curly-bracket.

  char notes[] = { "b2", 'c', "c#",

You've declared 'notes' to be a character array and therefore you should initialize it with characters. 'c' is a character, but "b2" is a pointer to a character string and it attempts to initialize that character array entry with a pointer. You'll have to rethink how to represent the notes.


Somewhat a repeat of what el_supremo replied while I was writing this, but I'll post anyway because there's some extra info:

"b2" is a string. You could represent it like this:

char b2[] = {'b', '2', 0}

So an array of strings must be a two dimensional array. But your array is a one dimensional array. The two dimensional array would look like this:

char notes[][3] = { "b2", "c", "c#", "d", "e", "f", "gb", "g", "ab", "a", "bb", "b", "C", "Db", "D", "E", "F", "G", "A", "B", " "};

Multidimensional arrays are required to have bounds for all dimensions except the first. So the [3] is the maximum string length plus one for the string's null terminator.

Your change from char to string note names will also require you to change the function signature:

void play( char note[], int beats)

and your comparison will need to be done using strcmp instead of ==.

Warning - before you get too far helping this person, note that he is a thread deleter expected ';' before 'play' - Audio - Arduino Forum