0

EDIT: have solved the problem. It was missing the quotation marks around each letter. Thanks for your help, st2000!


I am very new to Arduino and C++ (coding in general!). Have copied an example code, only modifying the notes to make my own tune. But when I click 'verify', I am getting the error above, and I can't for the life of me, work out what is wrong! I've searched and read several forums and FAQs, but am just coming up blank. This is the code I have, where the error is occuring:

  int melody[]={
    a,C,D, 
    a,a,C,D,D,C, 
    C,C,D,D,C, 
    C,D,E,F,E,D,C};

Any help would be greatly appreciated!

  • 2
    Can you provide the source from where you copied the code? Can you provide the wole code? It is hard to tell what is wrong if we don't see the definition of those variables a C D E F. – Kwasmich Oct 21 '19 at 12:17
  • High likely those notes (a to g, A to G) are either frequency values or mapped frequency values. – Michel Keijzers Oct 21 '19 at 12:36
  • I think the same but I question that they have been defined anywhere in the code. Thus leading to the compiler error message. – Kwasmich Oct 21 '19 at 12:42
  • Guessing, I think the OP has based their project on this example. – st2000 Oct 21 '19 at 12:46
  • 1
    Close it. It is a question about an error caused by a mere typo – AnT Oct 22 '19 at 14:21
0

To answer your question we will assume your project is based on the Melody example at www.arduino.cc.

Likely you wanted to enter the ASCII values of these letters into an array of type char (type int would work as well. But, in some cases, char would be more efficient). To do this you need to place single quotes around each letter. Specifically, you need to change your code:

int melody[]={ a,C,D, a,a,C,D,D,C, C,C,D,D,C, C,D,E,F,E,D,C};

...so that your line of code look like this:

int melody[]={'a','C','D','a','a','C','D','D','C','C','C','D','D','C','C','D','E','F','E','D','C'};
|improve this answer|||||
  • This is an assumption that doesn't seem right to me. Storing char-s in an array of int-s is a waste of space. – Kwasmich Oct 21 '19 at 12:30
  • @Kwasmich, you may be correct. I think I have see some processor / C compiler combinations which handled 8 bit values as if they were 16 bits. Wasting memory space in favor of speed. – st2000 Oct 21 '19 at 12:37
  • @st2000: In C, for example. In C language character constants are int value. There's nothing unusual in seeing a C platform with 16-bit int to handle character constants as 16-bit values. That's how C language requires them to be handled. – AnT Oct 22 '19 at 1:17
  • I have no idea what the above means (yet!), but this solution has worked for me. Thank you :) – user60325 Oct 22 '19 at 11:16
  • @user60325, What a compiler does "behind the curtain" when optimizing code is sometimes confusing. And is not necessarily the domain of the beginner programmer. But, if you want to know more this Stack Overflow Q/A (read the answer which starts w/"Let's look at your output...") can get you started. – st2000 Oct 22 '19 at 13:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.