Skip to content

Instantly share code, notes, and snippets.

@alejzeis
Created December 12, 2022 01:48
Show Gist options
  • Save alejzeis/963739d71abcd1c9a4bc634acbed5aba to your computer and use it in GitHub Desktop.
Save alejzeis/963739d71abcd1c9a4bc634acbed5aba to your computer and use it in GitHub Desktop.
Snippet of code to easily play a song on an Arduino
#define PITCH_B0 31
#define PITCH_C1 33
#define PITCH_CS1 35
#define PITCH_D1 37
#define PITCH_DS1 39
#define PITCH_E1 41
#define PITCH_F1 44
#define PITCH_FS1 46
#define PITCH_G1 49
#define PITCH_GS1 52
#define PITCH_A1 55
#define PITCH_AS1 58
#define PITCH_B1 62
#define PITCH_C2 65
#define PITCH_CS2 69
#define PITCH_D2 73
#define PITCH_DS2 78
#define PITCH_E2 82
#define PITCH_F2 87
#define PITCH_FS2 93
#define PITCH_G2 98
#define PITCH_GS2 104
#define PITCH_A2 110
#define PITCH_AS2 117
#define PITCH_B2 123
#define PITCH_C3 131
#define PITCH_CS3 139
#define PITCH_D3 147
#define PITCH_DS3 156
#define PITCH_E3 165
#define PITCH_F3 175
#define PITCH_FS3 185
#define PITCH_G3 196
#define PITCH_GS3 208
#define PITCH_A3 220
#define PITCH_AS3 233
#define PITCH_B3 247
#define PITCH_C4 262
#define PITCH_CS4 277
#define PITCH_D4 294
#define PITCH_DS4 311
#define PITCH_E4 330
#define PITCH_F4 349
#define PITCH_FS4 370
#define PITCH_G4 392
#define PITCH_GS4 415
#define PITCH_A4 440
#define PITCH_AS4 466
#define PITCH_B4 494
#define PITCH_C5 523
#define PITCH_CS5 554
#define PITCH_D5 587
#define PITCH_DS5 622
#define PITCH_E5 659
#define PITCH_F5 698
#define PITCH_FS5 740
#define PITCH_G5 784
#define PITCH_GS5 831
#define PITCH_A5 880
#define PITCH_AS5 932
#define PITCH_B5 988
#define PITCH_C6 1047
#define PITCH_CS6 1109
#define PITCH_D6 1175
#define PITCH_DS6 1245
#define PITCH_E6 1319
#define PITCH_F6 1397
#define PITCH_FS6 1480
#define PITCH_G6 1568
#define PITCH_GS6 1661
#define PITCH_A6 1760
#define PITCH_AS6 1865
#define PITCH_B6 1976
#define PITCH_C7 2093
#define PITCH_CS7 2217
#define PITCH_D7 2349
#define PITCH_DS7 2489
#define PITCH_E7 2637
#define PITCH_F7 2794
#define PITCH_FS7 2960
#define PITCH_G7 3136
#define PITCH_GS7 3322
#define PITCH_A7 3520
#define PITCH_AS7 3729
#define PITCH_B7 3951
#define PITCH_C8 4186
#define PITCH_CS8 4435
#define PITCH_D8 4699
#define PITCH_DS8 4978
#define SIXTENTH_NOTE 32
#define EIGHTH_NOTE 16
#define QUARTER_NOTE 4
#define HALF_NOTE 2
#define WHOLE_NOTE 1
/**
* Plays a song through the speaker connected to "speaker_pin". The pitches array is an array of note pitches,
* and the durations array is an array of the same size describing the duration of said pitches (quarter, half note, etc.)
* Size is the amount of notes, tempo is the tempo of the song (in BPM). The time_signature_numerator allows specifying if
* the song is in 4/4, 3/4, etc.
*/
void playSong(int speaker_pin, int pitches[], int durations[], int size, int tempo, int time_signature_numerator) {
int note_duration;
int measure_duration = ((60.0 / tempo) * 1.0 * time_signature_numerator) * 1000;
for(int i = 0; i < size; i++) {
note_duration = measure_duration * (1.0 / durations[i]);
tone(speaker_pin, pitches[i], note_duration);
delay(1.3 * note_duration);
noTone(speaker_pin);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment