Go Down

Topic: [SOLVED] Problem with PlayMelody example (Read 1 time) previous topic - next topic

podsix

Feb 26, 2011, 10:59 am Last Edit: Mar 10, 2011, 11:55 pm by David Cuartielles Reason: 1
The first PlayMelody example given on http://www.arduino.cc/en/Tutorial/PlayMelody won't compile because of the following #defines:

Code: [Select]
// TONES  ==========================================
// Start by defining the relationship between
//       note, period, &  frequency.
#define  c     3830    // 261 Hz
#define  d     3400    // 294 Hz
#define  e     3038    // 329 Hz
#define  f     2864    // 349 Hz
#define  g     2550    // 392 Hz
#define  a     2272    // 440 Hz
#define  b     2028    // 493 Hz
#define  C     1912    // 523 Hz
// Define a special note, 'R', to represent a rest
#define  R     0


The preprocessor is then replacing all occurrences of those letters with numbers in the code giving some unexpected errors.

I removed those #defines and since they were only used in one place I changed them there from:
Code: [Select]

int melody[] = {  C,  b,  g,  C,  b,   e,  R,  C,  c,  g, a, C };

to:
Code: [Select]

int melody[] = {  1912,  2028,  2550,  1912,  2028,   3038,  0,  1912,  3830,  2550, 2272, 1912 };


and the code worked.

PaulS

Quote
The preprocessor is then replacing all occurrences of those letters with numbers in the code giving some unexpected errors.

This is exactly why #define names are generally all uppercase names, and more than one character long. NOTE_C is very unlikely to appear anywhere else in the code, where 'c' can appear all over the place.
The art of getting good answers lies in asking good questions.

podsix

I agree Paul. Any idea what needs to be done to get this changed? I'd hate to have other Ardunio fans try a non-working example.

DojoDave

Hej,

I just fixed it. However I have to say you are wrong in you assumptions on why this wasn't working.

The reason for it is that "tone" has become a predefined function in Arduino, and you can see that the example, that I made back in 2005 and that is no longer part of the official distribution (it is kept for historical purposes), contains a variable defined as "tone". I have fixed it by simply renaming it to "tone_" and it compiles again.

Thanks for the hint,

/d

Go Up