#define OCTAVE_OFFSET 0
#define TONE_PIN 8
#define NOTE_C4 262
#define NOTE_CS4 277
#define NOTE_D4 294
#define NOTE_DS4 311
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_FS4 370
#define NOTE_G4 392
#define NOTE_GS4 415
#define NOTE_A4 440
#define NOTE_AS4 466
#define NOTE_B4 494
#define NOTE_C5 523
#define NOTE_CS5 554
#define NOTE_D5 587
#define NOTE_DS5 622
#define NOTE_E5 659
#define NOTE_F5 698
#define NOTE_FS5 740
#define NOTE_G5 784
#define NOTE_GS5 831
#define NOTE_A5 880
#define NOTE_AS5 932
#define NOTE_B5 988
#define NOTE_C6 1047
#define NOTE_CS6 1109
#define NOTE_D6 1175
#define NOTE_DS6 1245
#define NOTE_E6 1319
#define NOTE_F6 1397
#define NOTE_FS6 1480
#define NOTE_G6 1568
#define NOTE_GS6 1661
#define NOTE_A6 1760
#define NOTE_AS6 1865
#define NOTE_B6 1976
#define NOTE_C7 2093
#define NOTE_CS7 2217
#define NOTE_D7 2349
#define NOTE_DS7 2489
#define NOTE_E7 2637
#define NOTE_F7 2794
#define NOTE_FS7 2960
#define NOTE_G7 3136
#define NOTE_GS7 3322
#define NOTE_A7 3520
#define NOTE_AS7 3729
#define NOTE_B7 3951
int notes[] = { 0,
NOTE_C4, NOTE_CS4, NOTE_D4, NOTE_DS4, NOTE_E4, NOTE_F4, NOTE_FS4, NOTE_G4, NOTE_GS4, NOTE_A4, NOTE_AS4, NOTE_B4,
NOTE_C5, NOTE_CS5, NOTE_D5, NOTE_DS5, NOTE_E5, NOTE_F5, NOTE_FS5, NOTE_G5, NOTE_GS5, NOTE_A5, NOTE_AS5, NOTE_B5,
NOTE_C6, NOTE_CS6, NOTE_D6, NOTE_DS6, NOTE_E6, NOTE_F6, NOTE_FS6, NOTE_G6, NOTE_GS6, NOTE_A6, NOTE_AS6, NOTE_B6,
NOTE_C7, NOTE_CS7, NOTE_D7, NOTE_DS7, NOTE_E7, NOTE_F7, NOTE_FS7, NOTE_G7, NOTE_GS7, NOTE_A7, NOTE_AS7, NOTE_B7
};
char *song = "The Simpsons:d=4,o=5,b=160:c.6,e6,f#6,8a6,g.6,e6,c6,8a,8f#,8f#,8f#,2g,8p,8p,8f#,8f#,8f#,8g,a#.,8c6,8c6,8c6,c6";
void setup(void)
{
Serial.begin(9600);
}
#define isdigit(n) (n >= '0' && n <= '9')
void play_rtttl(char *p)
{
byte default_dur = 4; //duracion
byte default_oct = 6;
int bpm = 64;// pulsos por minuti
int num;
long notatotal;
long duration;
byte nota;
byte escala;
// formato: d=N,o=N,b=NNN:
// encontramos el principio
while(*p != ':') p++; // ignorar el nombre
p++;
// obtiene la duracion
if(*p == 'd')
{
p++; p++; // no cojer la "d="
num = 0;
while(isdigit(*p))
{
num = (num * 10) + (*p++ - '0');
}
if(num > 0) default_dur = num;
p++; // no cojer la coma
}
Serial.print("duracion: "); Serial.println(default_dur, 10);
// obtener la octava
if(*p == 'o')
{
p++; p++; // no cojer la "o="
num = *p++ - '0';
if(num >= 3 && num <=7) default_oct = num;
p++; // no cojer la coma
}
Serial.print("doct: "); Serial.println(default_oct, 10);
// obtener los BPM
if(*p == 'b')
{
p++; p++; // omitir "b="
num = 0;
while(isdigit(*p))
{
num = (num * 10) + (*p++ - '0');
}
bpm = num;
p++; // omitir colon
}
Serial.print("bpm: "); Serial.println(bpm, 10);
// BPM por lo general expresa el n�mero de cuartos por minuto
notatotal = (60 * 1000L / bpm) * 4;
//la nota completa (en milisegundos)
Serial.print("wn: "); Serial.println(notatotal, 10);
//empesamos el recorrido de la nota
while(*p)
{
// primer ,obtener duraci�n de la nota, si est� disponible
num = 0;
while(isdigit(*p))
{
num = (num * 10) + (*p++ - '0');
}
if(num) duration = notatotal / num;
else duration = notatotal / default_dur;
// obetenemos la nota
nota = 0;
switch(*p)
{
case 'c':
nota = 1;
break;
case 'd':
nota = 3;
break;
case 'e':
nota = 5;
break;
case 'f':
nota = 6;
break;
case 'g':
nota = 8;
break;
case 'a':
nota = 10;
break;
case 'b':
nota = 12;
break;
case 'p':
default:
nota = 0;
}
p++;
//obtiene la nota #aguda
if(*p == '#')
{
nota++;
p++;
}
//obtiene la nota "." punteada
if(*p == '.')
{
duration += duration/2;
p++;
}
// obetener la escala
if(isdigit(*p))
{
escala = *p - '0';
p++;
}
else
{
escala = default_oct;
}
escala+= OCTAVE_OFFSET;
if(*p == ',')
p++;
//omitir las comas para la nota siguiente
// tocar la nota
if(nota)
{
Serial.print("tocando: ");
Serial.print(escala, 10); Serial.print(' ');
Serial.print(nota, 10); Serial.print(" (");
Serial.print(notes[(escala - 4) * 12 + nota], 10);
Serial.print(") ");
Serial.println(duration, 10);
tone(TONE_PIN, notes[(escala - 4) * 12 + nota]);
delay(duration);
noTone(TONE_PIN);
}
else
{
Serial.print("Pausing: ");
Serial.println(duration, 10);
delay(duration);
}
}
}
void loop(void)
{
play_rtttl(song);
Serial.println("Done.");
while(1);
}