Paso 4: Cargar el código
1. comprobar todas las conexiones.
un) de la junta al altavoz.
b) conexión USB de la tarjeta al ordenador.
2. Abra el Arduino Software (con la placa Arduino conectada al ordenador)
3. proceda a añadir el siguiente código: (hay varios tones(songs) en el código a copiar y pegar en la línea correcta).
CÓDIGO:
Un divertido sketch para demostrar el uso de la biblioteca de tono.
#include < Tone.h >
Tone1 tono;
#define OCTAVE_OFFSET 0
int notas [] = {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 * canción = "El 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";
char * canción = "Indiana: d = 4, o = 5, b = 250:e, p 8, 8f, 8 g, p 8, 1 c 6, p. 8, d, p 8, 8e, 1f, p., g, p 8, 8a, 8b, 8 p, 1f6, p, a, p 8, 8b, c 2 6, 2 d 6, 2e6, e, p 8, 8f, 8 g, p 8, 1 c 6, p, d6, 8P, 8e6, 1f.6, g, p 8, 8 g, e.6, p 8, d6, p 8, 8 g, e.6, p 8, d6, p 8, 8 g, f.6, p 8, e6 , 8p, 8d 6 2c 6";
char *song = "TakeOnMe:d=4,o=4,b=160:8f#5,8f#5,8f#5,8d5,8p,8b,8p,8e5,8p,8e5,8p,8e5,8g#5,8g#5,8a5,8b5,8a5,8a5,8a5,8e5,8p,8d5,8p,8f#5,8p,8f#5,8p,8f#5,8e5,8e5,8f#5,8e5,8f#5,8f#5,8f#5,8d5,8p,8b,8p,8e5,8p,8e5,8p,8e5,8g#5,8g#5,8a5,8b5,8a5,8a5,8a5,8e5,8p , 8d 5, 8P, 8f #5, p 8, 8f #5, p 8, 8f #5, 8e5, 8e5 ";
char * canción = "Entertainer:d=4,o=5,b=140:8d,8d#,8e,c6,8e,c6,8e,2c.6,8c6,8d6,8d#6,8e6,8c6,8d6,e6,8b,d6,2c6,p,8d,8d#,8e,c6,8e,c6,8e,2c.6,8p,8a,8g,8f#,8a,8c6,e6,8d6,8c6,8a,2d6";
char * canción = "Muppets:d = 4, o = 5, b = 250:c6, c6, a, b, 8a, b, g, p, c6, c6, a, 8b, 8a, 8 p, g, p, e, e, g, f, 8e, f, 8 c 6, 8 c, 8 d, e, 8e, 8e, p 8, 8e, g, 2P, c6, c6, a, b, 8a, b, g, p, c6, c6, a, 8b, a, g., p, e, e, g, f, 8e f, 8 c 6, 8 c, 8 d, e, 8e, d, 8 d, c ";
char * canción = "Xfiles:d = 4, o = 5, b = 125:e, b, b, d6, 2b., 1 p, e, b, a, b, e6, 2b., 1 p, g6, f #6, e6, d6, e6, 2b., 1 p, g6, f #6, e6, d6, f #6, 2b., 1 p, e, b, a, b, d6, 2b., 1 p, e, b, a, b, e6, 2b., 1 p, e6, 2b.";
char * canción = "Looney: d = 4, o = 5, b = p 140:32, c6, 8f6, 8e6, 8 d 6, c 8 6, a., 8 c 6, 8f6, 8e6, 8 d 6, 8D #6,e.6,8e6,8e6,8c6,8d6,8c6,8e6,8c6,8d6,8a,8c6,8g,8a # 8a, 8f";
char * canción = "20thCenFox:d = 16, o = 5, b = 140:b, p 8, b, b, 2b, p, c6, 32 p, b, p 32, c6, 32 p, b, p 32, c6, 32 p, b, p 8, b, b, b, 32 p, b, 32 p, b, 32 p, b, 32 p, b, 32 p, b, 32 p, b, 32 p, g #, 32 p, un, 32 p, b, p 8, b, b, 2b, p 4, 8e, g # 8, 8b, 1 c# 6, 8f #, 8a, 8 c# 6, 1e6 8a, 8C #6 8e6, 1e6, 8b, 8 g #, 8, 2b ";
char *song = "Bond:d=4,o=5,b=80:32p,16c#6,32d#6,32d#6,16d#6,8d#6,16c#6,16c#6,16c#6,16c#6,32e6,32e6,16e6,8e6,16d#6,16d#6,16d#6,16c#6,32d#6,32d#6,16d#6,8d#6,16c#6,16c#6,16c#6,16c#6,32e6,32e6,16e6,8e6,16d#6,16d6,16c#6,16c#7,c.7,16g#6,16f#6,g#.6";
char * canción = "MASH:d = 8, o = 5, b = 140:4a, 4 g, f #, g, p, f #, p, g, p, f #, p, 2e., p, f #, e, 4f #, e, f #, p, e, p, 4 d., p, f #, 4e, d, e, p, d, p, e, p, d, p, c# 2., p, d, c#, 4D, c#, d, p, e, p, 4f #, p, a, p, 4b, a, b, p, un p, b, p, 2a., 4 p, a, b, a, 4b, a, b, p, 2a., 4f #, a,b,p,d6,p,4e.6,d6,b,p,a,p, 2b ";
char *song = "StarWars:d=4,o=5,b=45:32p,32f#,32f#,32f#,8b.,8f#.6,32e6,32d#6,32c#6,8b.6,16f#.6,32e6,32d#6,32c#6,8b.6,16f#.6,32e6,32d#6,32e6,8c#.6,32f#,32f#,32f#,8b.,8f#.6,32e6,32d#6,32c#6,8b.6,16f#.6,32e6,32d#6,32c#6,8b.6,16f#.6,32e6,32d#6,32e6,8c#6";
char * canción = "GoodBad:d = 4, o = 5, b = 56:32 p, 32a #, 6 # 32d 32a #, 6 # 32d, 8a #., 16f #., 16 g #., d # 32a #, 6 # 32d 32a #, 6 # 32d, 8a #., 16f #., 16 g #., c# 6, 32a #, 6 # 32d 32a #, 6 # 32d, 8a #., 16f #., 32f. 32d #, c#, 32a #, 6 # 32d 32a #, 6 # 32d, 8a #., 16 g #., d #";
char * canción = "TopGun:d = 4, o = 4, b = 31:32 p, 16 c#, g # 16, 16 g #, 32f #, 32f, 32f #, 32f, d # 16, 16 d #, 32 c#, # 32d, 16f, # 32d, 32f, 16f #, 32f, 32 c#, 16f, d #, c# 16, 16 g #, g # 16, 32f #, 32f, 32f #, 32f, 16 d #, 16 d #, 32 c#, # 32d, 16f, # 32d, 32f, 16f #, 32f, 32 c#, g #";
char * canción = "A-Team: d = 8, o = 5, b = 125:4 d #6, un #, 2d #6, p 16, g #, 4a #, d # 4., p, 16 g, 16 #, d #6, un #, f6, 2d #6, p 16, c#.6, 16 c 6, 16a #, g #., 2a #";
char *song = "Flinstones:d=4,o=5,b=40:32p,16f6,16a#,16a#6,32g6,16f6,16a#.,16f6,32d#6,32d6,32d6,32d#6,32f6,16a#,16c6,d6,16f6,16a#.,16a#6,32g6,16f6,16a#.,32f6,32f6,32d#6,32d6,32d6,32d#6,32f6,16a#,16c6,a#,16a6,16d.6,16a#6,32a6,32a6,32g6,32f#6,32a6,8g6,16g6,16c.6,32a6,32a6,32g6,32g6,32f6,32e6,32g6,8f6,16f6,16a#. , 16a #6, 32g 6, 16f6, 16a #., 16f6, 32d #6, 32d 6 d 32 6, 32d #6, 32f6, 16a #, 16c.6,32d6, 32d #6, 32f6, 16a #, 16c.6,32d6, 32d #6, 32f6, 16a #6, 16c 7, 8a #.6";
char * canción = "peligro: d = 4, o = 6, b = 125:c, f, c, f5, c, f, 2 c, c, f, c, f, a., 8 g, 8f, 8e, 8 d, 8 c#, c, f, c, f5, c, f, 2 c, f., 8 d, c, a #5 a5, g5, f5, p, d #, g #, d #, g #5, d #, g #, 2d #, d #, g #, d #, g #, c.7, 8a #, g # 8, 8 g, 8f, 8e, d # , g #, d #, g #5 d #, g #, # 2d, g #., 8f, d #, c#, c, p, #5, p, g.5 #, d #, g #";
char * canción = "Gadget: d = 16, o = 5, b = 50:32d # 32f, 32f #, 32 g #, a #, f #, a, f, g #, f #, # 32d, 32f, 32f #, 32 g #, a #, d #6, 4 d 6, # 32d, 32f, 32f #, 32 g #, a #, f #, a, f, g #, f #, d # 8";
char * canción = "Smurfs:d = 32, o = 5, b = 200:4 c# 6, p 16, 4f #6, p, 16 c# 6, p, 8 d #6, p, 8b, p, g # 4, p 16, 4C #6, p, 16a #, p, 8f #, p, 8a #, p, 4 g #, 4P, g #, p, un #, p, b, p, c6, p, 4 c# 6, p 16, 4f #6, p, 16 c# 6, p, 8 d #6, p, 8b, p, g # 4, p 16, 4C #6, p, 16a # , p, 8b, p, 8f, p, 4f #";
char * canción = "MahnaMahna:d = 16, o = 6, b = 125:c#, c., b5, 8a #.5, 8f., 4 g #, a #, g., d #, 8 4P, c#, c., b5, 8a #.5, 8f., g #., 8a #., 4 g, 8 p, c#, c., b5, 8a #.5, 8f., 4 g #, f, g., 8 d #., f, g., 8 d #., f, 8 g, 8 d #., f, g 8, d #, c 8, 5 #, 8 d #., 8 d #., 4 d #, d # 8.";
char * canción = "LeisureSuit:d=16,o=6,b=56:f.5,f#.5,g.5,g#5,32a#5,f5,g#.5,a#.5,32f5,g#5,32a#5,g#5,8c#.,a#5,32c#,a5,a#.5,c#.,32a5,a#5,32c#,d#,8e,c#.,f.,f.,f.,f.,f,32e,d#,8d,a#.5,e,32f,e,32f,c#,d#.,c#";
char *song = "smb:d=4,o=5,b=100:16e6,16e6,32p,8e6,16c6,8e6,8g6,8p,8g,8p,8c6,16p,8g,16p,8e,16p,8a,8b,16a#,8a,16g.,16e6,16g6,8a6,16f6,8g6,8e6,16c6,16d6,8b,16p,8c6,16p,8g,16p,8e,16p,8a,8b,16a#,8a,16g.,16e6,16g6,8a6,16f6,8g6,8e6,16c6,16d6,8b,8p,16g6 ,16f#6,16f6,16d#6,16p,16e6,16p,16g#,16a,16c6,16p,16a,16c6,16d6,8p,16g6,16f#6,16f6,16d#6,16p,16e6,16p,16c7,16p,16c7,16c7,p,16g6,16f#6,16f6,16d#6,16p,16e6,16p,16g#,16a,16c6,16p,16a,16c6,16d6,8p,16d#6,8p,16d6,8p,16c6";
char *song = "smb_under:d=4,o=6,b=100:32c,32p,32c7,32p,32a5,32p,32a,32p,32a#5,32p,32a#,2p,32c,32p,32c7,32p,32a5,32p,32a,32p,32a#5,32p,32a#,2p,32f5,32p,32f,32p,32d5,32p,32d,32p,32d#5,32p,32d#,2p,32f5,32p,32f,32p,32d5,32p,32d,32p,32d#5,32p,32d#";
char * canción = "smbdeath:d=4,o=5,b=90:32c6,32c6,32c6,8p,16b,16f6,16p,16f6,16f.6,16e.6,16d6,16c6,16p,16e,16p,16c";
char * canción = "ducktales:d = 4, o = 5, b = 112:8e6, 8e6, p 16, 16 g 6, 8b6, g #6, p, 8e6, 8 d 6, 8 c 6, 8 d 6, 8e6, 8 d 6, 8 c 6, 8 d 6, 8e6, 8e6, p 16, 16 g 6, 8b6, g #6, p, 8e6, 8 d 6, 8 c 6, 8 d 6, 8e6, 8 d 6, 8 c 6, 8 g 6, 8e6, 8e6";
char *song = "Zelda1:d=4,o=5,b=125:a#,f.,8a#,16a#,16c6,16d6,16d#6,2f6,8p,8f6,16f.6,16f#6,16g#.6,2a#.6,16a#.6,16g#6,16f#.6,8g#.6,16f#.6,2f6,f6,8d#6,16d#6,16f6,2f#6,8f6,8d#6,8c#6,16c#6,16d#6,2f6,8d#6,8c#6,8c6,16c6,16d6,2e6,g6,8f6,16f,16f,8f,16f,16f,8f,16f,16f,8f,8f,a#,f.,8a# 16a#,16c6,16d6,16d#6,2f6,8p,8f6,16f.6,16f#6,16g#.6,2a#.6,c#7,c7,2a6,f6,2f#.6,a#6,a6,2f6,f6,2f#.6,a#6,a6,2f6,d6,2d#.6,f#6,f6,2c#6,a#,c6,16d6,2e6,g6,8f6,16f,16f,8f,16f,16f,8f,16f,16f,8f,8f ";
char *song = "smario2:d=4,o=5,b=125:8g,16c,8e,8g.,16c,8e,16g,16c,16e,16g,8b,a,8p,16c,8g,16c,8e,8g.,16c,8e,16g,16c#,16e,16g,8b,a,8p,16b,8c6,16b,8c6,8a.,16c6,8b,16a,8g,16f#,8g,8e.,16c,8d,16e,8f,16e,8f,8b.4,16e,8d.,c";
char *song = "smb3lvl1:d=4,o=5,b=80:16g,32c,16g.,16a,32c,16a.,16b,32c,16b,16a.,32g#,16a.,16g,32c,16g.,16a,32c,16a,4b.,32p,16c6,32f,16c.6,16d6,32f,16d.6,16e6,32f,16e6,16d.6,32c#6,16d.6,16c6,32f,16c.6,16d6,32f,16d6,4e.6,32p,16g,32c,16g.,16a,32c,16a.,16b,32c,16b,16a.,32g#,16a.,16c6 , 8c.6,32p,16c6,4c.6 ";
char * canción = "shinobi:d = 4, o = 5, b = 140:b, f #6, d6, b, g, f #, e, 2f #., 1f #, p, b, f #6, d6, b, g, f #, e, 1f #., 8a, 1b., 8a, 1f #., 8a, 1b., 8a, 1f #.";
char * canción = "outrun_magic:d = 4, o = 5, b = 160:f6, d #6, 8 g #.6, f6, d #6, c 8.6 #, d #6, c6 2 g #., c# 6, c6, 8 d.6 #, c# 6, c6, 8f., un #, 16c.6, 1a #, f6, d #6, 8 g #.6, f6, d #6, c 8.6 #, d #6, c6 2 g #., c# 6 c6, 8 d.6 #, c# 6, c6, 16f, 16 g #., c6, 2a #.";
char *song = "Popeye:d=4,o=5,b=140:16g.,16f.,16g.,16p,32p,16c.,16p,32p,16c.,16p,32p,16e.,16d.,16c.,16d.,16e.,16f.,g,8p,16a,16f,16a,16c6,16b,16a,16g,16a,16g,8e,16g,16g,16g,16g,8a,16b,32c6,32b,32c6,32b,32c6,32b,8c6";
char * canción = "Wonderboy:d = 4, o = 5, b = 225:f6, d6, f6, 8 d 6, f6, 32 p, 8f6, d6, f6, d6, e6, c6, e6, 8 c 6, e6, 32 p, 8e6, c6, e6, c6";
char *song = "smwwd1:d=4,o=5,b=125:a,8f.,16c,16d,16f,16p,f,16d,16c,16p,16f,16p,16f,16p,8c6,8a.,g,16c,a,8f.,16c,16d,16f,16p,f,16d,16c,16p,16f,16p,16a#,16a,16g,2f,16p,8a.,8f.,8c,8a.,f,16g#,16f,16c,16p,8g#.,2g,8a.,8f.,8c,8a.,f,16g# , 16f, 8c, 2c 6";
char * canción = "dkong:d = 4, o = 5, b = 160:2 c 8 d., d #., c., 16b, 16 c 6, 16b, 16 c 6, 16b, 16 c 6, 16b, 16 c 6, 16b, 16 c 6, 16b, 16C 6, 16b, 2 c 6";
char * canción = "dkong:d = 4, o = 5, b = 160:2 c 8 d., d #., c., 16b, 16 c 6, 16b, 16 c 6, 16b, 16 c 6, 16b, 16 c 6, 16b, 16 c 6, 16b, 16C 6, 16b, 2 c 6";
void setup(void)
{
Serial.Begin(9600);
tone1.Begin(8);
}
#define isdigit(n) (n > = '0' & & n < = '9')
void play_rtttl(char *p)
{
Absolutamente ningún error check-in aquí
default_dur bytes = 4;
byte default_oct = 6;
int bpm = 63;
int num;
wholenote largo;
de larga duración;
Nota de bytes;
escala de bytes;
formato: d = N, o = N, b = NNN:
encontrar el inicio (omitir nombre, etc.)
mientras que (* p! = ':') p ++; omitir nombre
p ++; saltar ':'
obtener duración predeterminada
Si (* p == había ')
{
p ++; p ++; saltar "d ="
Num = 0;
while(isdigit(*p))
{
Num = (num * 10) + (* p ++ - '0');
}
Si (num > 0) default_dur = num;
p ++; omitir la coma
}
Serial.Print ("ddur:"); Serial.println (default_dur, 10);
conseguir la octava por defecto
Si (* p == ' o ')
{
p ++; p ++; saltar "o ="
Num = * p ++ - '0';
Si (num > = 3 & & num < = 7) default_oct = num;
p ++; omitir la coma
}
Serial.Print ("pued:"); Serial.println (default_oct, 10);
Haz BPM
Si (* p == 'b')
{
p ++; p ++; saltar "b ="
Num = 0;
while(isdigit(*p))
{
Num = (num * 10) + (* p ++ - '0');
}
BPM = num;
p ++; Pase colon
}
Serial.Print ("bpm:"); Serial.println (bpm, 10);
BPM generalmente expresa la cantidad de notas de cuarto por minuto
wholenote = (60 * 1000L / bpm) * 4; Este es el momento para la nota entera (en milisegundos)
Serial.Print ("wn:"); Serial.println (wholenote, 10);
ahora iniciar bucle Nota
while(*p)
{
en primer lugar, obtener la duración de la nota, si existen
Num = 0;
while(isdigit(*p))
{
Num = (num * 10) + (* p ++ - '0');
}
if(NUM) duración = wholenote / num;
más duración = wholenote / default_dur; tendremos que comprobar si somos una nota punteada después
ahora conseguir la nota
Nota = 0;
Switch(*p)
{
caso 'c':
Nota = 1;
rotura;
caso sería ':
Nota = 3;
rotura;
caso 'e':
Nota = 5;
rotura;
caso 'f':
Nota = 6;
rotura;
caso 'g':
Nota = 8;
rotura;
caso 'a':
Nota = 10;
rotura;
caso 'b':
Nota = 12;
rotura;
caso 'p':
por defecto:
Nota = 0;
}
p ++;
Ahora, conseguir opcional '#' sharp
Si (* p == '#')
{
Nota ++;
p ++;
}
Ahora, conseguir opcional '.' Nota puntos
Si (* p == '.')
{
+= duración duración/2;
p ++;
}
Ahora, conseguir la escala
if(isdigit(*p))
{
escala = * p - '0';
p ++;
}
otra cosa
{
escala = default_oct;
}
escala += OCTAVE_OFFSET;
Si (* p == ',')
p ++; omitir la coma para la siguiente nota (o seamos al final)
ahora tocar la nota
if(Note)
{
Serial.Print ("jugando:");
Serial.Print (escala, 10); Serial.Print(' ');
Serial.Print (Nota 10); Serial.Print ("('');
Serial.Print (notas [(scale-4) * 12 + Nota], 10);
Serial.Print(")");
Serial.println (duración, 10);
tone1.Play (notas [(scale-4) * 12 + Nota]);
Delay(Duration);
tone1.STOP();
}
otra cosa
{
Serial.Print ("pausa:");
Serial.println (duración, 10);
Delay(Duration);
}
}
}
void loop(void)
{
play_rtttl(Song);
Serial.println("Done.");
while(1);
}