[SOLVED] Problem with PlayMelody example

The first PlayMelody example given on http://www.arduino.cc/en/Tutorial/PlayMelody won't compile because of the following #defines:

 // 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:

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

to:

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

and the code worked.

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.

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.

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