|
Come far suonare Arduino
Introduzione
RTTL altrimenti noto come Ring Tone Transfer Language, è il protocollo con cui venivano tradotte le canzoni in suonerie per i cellulari di una volta (mi ricordo che si usava quando io ero alle scuole medie quindi molto tempo fa). Con la libreria Tone potrete far suonare al vostro arduino questo tipo di suonerie che troverete in questo sito.
Istruzioni della libreria tone
Tone buzzer1; creo un oggetto tone.
buzzer1.begin(pin); dichiaro a quale pin è connesso il nostro buzzer o il nostro altoparlante.
buzzer1.play(notes[i]); faccio suonare al buzzer una determinata nota.
buzzer1.stop(); faccio smettere di suonare al buzzer una determinata nota.
buzzer1.isPlaying(); restituisce una variabile booleana, Falso se non sta suonando Vero se sta suonando.
Le note si dichiarano di solito in un array di questo tipo int notes[] = { ...,...,...} e le note che possiamo usare sono indicate nella tabella sotto.
| Constant Name | Frequency (Hz) |
| NOTE_B2 | 123 |
| NOTE_C3 | 131 |
| NOTE_CS3 | 139 |
| NOTE_D3 | 147 |
| NOTE_DS3 | 156 |
| NOTE_E3 | 165 |
| NOTE_F3 | 175 |
| NOTE_FS3 | 185 |
| NOTE_G3 | 196 |
| NOTE_GS3 | 208 |
| NOTE_A3 | 220 |
| NOTE_AS3 | 233 |
| NOTE_B3 | 247 |
| NOTE_C4 | 262 |
| NOTE_CS4 | 277 |
| NOTE_D4 | 294 |
| NOTE_DS4 | 311 |
| NOTE_E4 | 330 |
| NOTE_F4 | 349 |
| NOTE_FS4 | 370 |
| NOTE_G4 | 392 |
| NOTE_GS4 | 415 |
| NOTE_A4 | 440 |
| NOTE_AS4 | 466 |
| NOTE_B4 | 494 |
| NOTE_C5 | 523 |
| NOTE_CS5 | 554 |
| NOTE_D5 | 587 |
| NOTE_DS5 | 622 |
| NOTE_E5 | 659 |
| NOTE_F5 | 698 |
| NOTE_FS5 | 740 |
| NOTE_G5 | 784 |
| NOTE_GS5 | 831 |
| NOTE_A5 | 880 |
| NOTE_AS5 | 932 |
| NOTE_B5 | 988 |
| NOTE_C6 | 1047 |
| NOTE_CS6 | 1109 |
| NOTE_D6 | 1175 |
| NOTE_DS6 | 1245 |
| NOTE_E6 | 1319 |
| NOTE_F6 | 1397 |
| NOTE_FS6 | 1480 |
| NOTE_G6 | 1568 |
| NOTE_GS6 | 1661 |
| NOTE_A6 | 1760 |
| NOTE_AS6 | 1865 |
| NOTE_B6 | 1976 |
| NOTE_C7 | 2093 |
| NOTE_CS7 | 2217 |
| NOTE_D7 | 2349 |
| NOTE_DS7 | 2489 |
| NOTE_E7 | 2637 |
| NOTE_F7 | 2794 |
| NOTE_FS7 | 2960 |
| NOTE_G7 | 3136 |
| NOTE_GS7 | 3322 |
| NOTE_A7 | 3520 |
| NOTE_AS7 | 3729 |
| NOTE_B7 | 3951 |
| NOTE_C8 | 4186 |
| NOTE_CS8 | 4435 |
| NOTE_D8 | 4699 |
| NOTE_DS8 |
4978 |
Il programma RTTL che si trova nella libreria tone è uno dei più divertenti che abbia mai provato, ecco un video che dimostra qualche canzone ed ecco lo sketch con qualche canzoncina aggiunta.
Per selezionare una canzone da suonare basterà scaricare questo file, copiare tutte le canzoni e metterle al posto di MrRoboto e per sentirne una basterà togliere i // e commentare sempre con // quella che stavate sentendo prima. Si può suonare solo una canzone alla volta.
Tone tone1;
#define OCTAVE_OFFSET 0
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 = "MrRoboto:d=32,o=6,b=50:p,b.5,8a5,d.,8c#,a.5,8g5,p,d.,d.,d.,16e,p,d.,d.,d.,d.,e.";
void setup(void)
{
Serial.begin(9600);
tone1.begin(8);
}
#define isdigit(n) (n >= '0' && n 0) default_dur = num;
p++; // skip comma
}
Serial.print("ddur: "); Serial.println(default_dur, 10);
// get default octave
if(*p == 'o')
{
p++; p++; // skip "o="
num = *p++ - '0';
if(num >= 3 && num <=7) default_oct = num;
p++; // skip comma
}
Serial.print("doct: "); Serial.println(default_oct, 10);
// get BPM
if(*p == 'b')
{
p++; p++; // skip "b="
num = 0;
while(isdigit(*p))
{
num = (num * 10) + (*p++ - '0');
}
bpm = num;
p++; // skip colon
}
Serial.print("bpm: "); Serial.println(bpm, 10);
// BPM usually expresses the number of quarter notes per minute
// this is the time for whole note (in milliseconds)
wholenote = (60 * 1000L / bpm) * 4;
Serial.print("wn: "); Serial.println(wholenote, 10);
// now begin note loop
while(*p)
{
// first, get note duration, if available
num = 0;
while(isdigit(*p))
{
num = (num * 10) + (*p++ - '0');
}
if(num) duration = wholenote / num;
// we will need to check if we are a dotted note after
else duration = wholenote / default_dur;
// now get the note
note = 0;
switch(*p)
{
case 'c':
note = 1;
break;
case 'd':
note = 3;
break;
case 'e':
note = 5;
break;
case 'f':
note = 6;
break;
case 'g':
note = 8;
break;
case 'a':
note = 10;
break;
case 'b':
note = 12;
break;
case 'p':
default:
note = 0;
}
p++;
// now, get optional '#' sharp
if(*p == '#')
{
note++;
p++;
}
// now, get optional '.' dotted note
if(*p == '.')
{
duration += duration/2;
p++;
}
// now, get scale
if(isdigit(*p))
{
scale = *p - '0';
p++;
}
else
{
scale = default_oct;
}
scale += OCTAVE_OFFSET;
if(*p == ',')
p++; // skip comma for next note (or we may be at the end)
// now play the note
if(note)
{
Serial.print("Playing: ");
Serial.print(scale, 10); Serial.print(' ');
Serial.print(note, 10); Serial.print(" (");
Serial.print(notes[(scale - 4) * 12 + note], 10);
Serial.print(") ");
Serial.println(duration, 10);
tone1.play(notes[(scale - 4) * 12 + note]);
delay(duration);
tone1.stop();
}
else
{
Serial.print("Pausing: ");
Serial.println(duration, 10);
delay(duration);
}
}
}
void loop(void)
{
play_rtttl(song);
Serial.println("Done.");
while(1);
}
articolo liberamente tratto dal playground arduino inglese
![]()