Este ejemplo muestra como usar la función tone() para generar notas. Reproduce una pequeña melodia que quizás hayas escuchado antes.(la resistencia es de 100 Ohms o menor)
Ejemplo 1
#define NOTE_B0 31
#define NOTE_C1 33
#define NOTE_CS1 35
#define NOTE_D1 37
#define NOTE_DS1 39
#define NOTE_E1 41
#define NOTE_F1 44
#define NOTE_FS1 46
#define NOTE_G1 49
#define NOTE_GS1 52
#define NOTE_A1 55
#define NOTE_AS1 58
#define NOTE_B1 62
#define NOTE_C2 65
#define NOTE_CS2 69
#define NOTE_D2 73
#define NOTE_DS2 78
#define NOTE_E2 82
#define NOTE_F2 87
#define NOTE_FS2 93
#define NOTE_G2 98
#define NOTE_GS2 104
#define NOTE_A2 110
#define NOTE_AS2 117
#define NOTE_B2 123
#define NOTE_C3 131
#define NOTE_CS3 139
#define NOTE_D3 147
#define NOTE_DS3 156
#define NOTE_E3 165
#define NOTE_F3 175
#define NOTE_FS3 185
#define NOTE_G3 196
#define NOTE_GS3 208
#define NOTE_A3 220
#define NOTE_AS3 233
#define NOTE_B3 247
#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
#define NOTE_C8 4186
#define NOTE_CS8 4435
#define NOTE_D8 4699
#define NOTE_DS8 4978
int melody[ ] = {
NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};
// duracion de las notas: 4 = cuarto de nota, 8 = octavo de nota, etc.:
int noteDurations[] = {
4, 8, 8, 4,4,4,4,4 };
void setup() {
for (int thisNote = 0; thisNote < 8; thisNote++) {
int noteDuration = 1000/noteDurations[thisNote];
tone(8, melody[thisNote],noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
}
}
void loop() {
}
Ejemplo 2
#define sil 0
#define do1 15289
#define do2 7645
#define do3 3822
#define do4 1911
#define do5 956
#define do6 478
#define do7 239
#define dos1 14431
#define dos2 7215
#define dos3 3608
#define dos4 1804
#define dos5 902
#define dos6 451
#define dos7 225
#define re1 13621
#define re2 6810
#define re3 3405
#define re4 1703
#define re5 851
#define re6 426
#define re7 213
#define res1 12856
#define res2 6428
#define res3 3214
#define res4 1607
#define res5 804
#define res6 402
#define res7 201
#define mi1 12135
#define mi2 6067
#define mi3 3034
#define mi4 1517
#define mi5 758
#define mi6 379
#define mi7 190
#define fa1 11454
#define fa2 5727
#define fa3 2863
#define fa4 1432
#define fa5 716
#define fa6 358
#define fa7 179
#define fas1 10811
#define fas2 5405
#define fas3 2703
#define fas4 1351
#define fas5 676
#define fas6 338
#define fas7 169
#define sol1 10204
#define sol2 5102
#define sol3 2551
#define sol4 1276
#define sol5 638
#define sol6 319
#define sol7 159
#define sols1 9631
#define sols2 4816
#define sols3 2408
#define sols4 1204
#define sols5 602
#define sols6 301
#define sols7 150
#define la1 9091
#define la2 4545
#define la3 2273
#define la4 1136
#define la5 568
#define la6 284
#define la7 142
#define las1 8581
#define las2 4290
#define las3 2145
#define las4 1073
#define las5 536
#define las6 268
#define las7 134
#define si1 8099
#define si2 4050
#define si3 2025
#define si4 1012
#define si5 506
#define si6 253
#define si7 127
/*
Melodias con Arduino - Jorge García Tíscar
*/
// Variables de control
int pinAltavoz = 8;
//int tempo = 69; //para elisa
int tempo = 155; //himno alegria
//int tempo = 144; //tetris
//int m[] = {4,mi5,4,res5,4,mi5,4,res5,4,mi5,4,si4,4,re5,4,do5,2,la4,4,sil,4,do4,4,mi4,4,la4,2,si4,4,sil,4,mi4,4,sols4,4,si4,2,do5,4,sil,4,mi4,4,mi5,4,res5,4,mi5,4,res5,4,mi5,4,si4,4,re5,4,do5,2,la4,4,sil,4,do4,4,mi4,4,la4,2,si4,4,sil,4,mi4,4,do5,4,si4,2,la4,4,sil,4,sil,4,si4,4,do5,4,re5,2,mi5,4,sol4,4,fa5,4,mi5,2,re5,4,fa4,4,mi5,4,re5,2,do5,4,mi4,4,re5,4,do5,2,si4,4,sil,4,sol4,4,mi5,4,mi4,4,mi5,4,mi5,4,mi6,4,res5,4,mi5,4,res5,4,mi5,4,res5,4,mi5,4,res5,4,mi5,4,re5,4,mi5,4,res5,4,mi5,4,si4,4,re5,4,do5,2,la4,4,sil,4,do4,4,mi4,4,la4,2,si4,4,sil,4,mi4,4,sols4,4,si4,2,do5,4,sil,4,mi4,4,mi5,4,res5,4,mi5,4,res5,4,mi5,4,si4,4,re5,4,do5,2,la4,4,sil,4,do4,4,mi4,4,la4,2,si4,4,sil,4,mi4,4,do5,4,si4,2,la4};
int m[] = {-2,si4,1,do5,1,re5,1,re5,1,do5,1,si4,1,la4,1,sol4,1,sol4,1,la4,1,si4,-2,si4,2,la4,-2,la4,-2,si4,1,do5,1,re5,1,re5,1,do5,1,si4,1,la4,1,sol4,1,sol4,1,la4,1,si4,-2,la4,2,sol4,-2,sol4,2,sil,-2,la4,1,si4,1,sol4,1,la4,2,si4,2,do5,1,si4,1,sol4,1,la4,2,si4,2,do5,1,si4,1,la4,1,sol4,1,la4,-2,re4,-2,si4,1,do5,1,re5,1,re5,1,do5,1,si4,1,la4,1,sol4,1,sol4,1,la4,1,si4,-2,la4,2,sol4,-2,sol4};
//int m[]={1,la4,2,mi4,2,fa4,1,sol4,2,fa4,2,mi4,1,re4,2,re4,2,fa4,1,la4,2,sol4,2,fa4,1,mi4,2,mi4,2,fa4,1,sol4,1,la4,1,fa4,1,re4,-2,re4,2,sil,1,sol4,2,las4,2,re5,2,re5,2,do5,2,las4,1,la4,2,fa4,2,la4,2,la4,2,sol4,2,fa4,1,mi4,2,mi4,2,fa4,1,sol4,1,la4,1,fa4,1,re4,-2,re4};
// Variables auxiliares
long d[sizeof(m)/sizeof(int)];
int ta[sizeof(m)/sizeof(int)];
int i = 0;
int id = 0;
long t = 0;
void setup(){
pinMode(pinAltavoz, OUTPUT);
//Redistribuir vector melodia
for(i=0;i<sizeof(m)/sizeof(int);i=i+2){
if(m[i]>0){
d[id] = 60000/(tempo*m[i]);
}else{
d[id] = (-60000*m[i])/tempo;
}
ta[id]=m[i+1];
id++;
}
}
void loop(){
i = 0;
for (i=0;i<sizeof(ta)/sizeof(int);i++){
t = 0;
if(ta[i]==0){
delay(d[i]); // Silencio
}else{
while(t<d[i]*1000){ // Generar PWM
digitalWrite(pinAltavoz, HIGH);
delayMicroseconds(ta[i]);
digitalWrite(pinAltavoz, LOW);
delayMicroseconds(ta[i]);
t = t + 2*ta[i];
}
}
delay(10); // Pausa tras cada nota
}
delay(5000); // Pausa tras melodia
}