Skip to content

Commit

Permalink
Send note duration in ms to tone function
Browse files Browse the repository at this point in the history
  • Loading branch information
jcarranz97 committed Dec 3, 2023
1 parent c77a146 commit 3668c42
Showing 1 changed file with 50 additions and 49 deletions.
99 changes: 50 additions & 49 deletions tone/christmas_melody/christmas_melody.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,106 +41,107 @@
#define NOTE_E5 659
#define NOTE_F5 698

#define SPEED 400U // Speed of each note in ms
#define SILENCE 10U // 10 ms of silence after each note
#define NOTE_DURATION 400U // Speed of each note in ms

uint slice_num; // Variable to save the PWM slice number
uint32_t get_note_duration_in_ms(float note_duration) {
return (uint32_t)(note_duration * NOTE_DURATION);
}

int main() {
// Configure tone generation on BUZZER_PIN
tone_init(BUZZER_PIN);
while (1) {
// 1
tone(BUZZER_PIN, NOTE_G4, 1.5f);
tone(BUZZER_PIN, NOTE_A4, .5f);
tone(BUZZER_PIN, NOTE_G4, 1.f);
tone(BUZZER_PIN, NOTE_G4, get_note_duration_in_ms(1.5f));
tone(BUZZER_PIN, NOTE_A4, get_note_duration_in_ms(.5f));
tone(BUZZER_PIN, NOTE_G4, get_note_duration_in_ms(1.f));

// 2
tone(BUZZER_PIN, NOTE_E4, 3.f);
tone(BUZZER_PIN, NOTE_E4, get_note_duration_in_ms(3.f));

// 3
tone(BUZZER_PIN, NOTE_G4, 1.5f);
tone(BUZZER_PIN, NOTE_A4, .5f);
tone(BUZZER_PIN, NOTE_G4, 1.f);
tone(BUZZER_PIN, NOTE_G4, get_note_duration_in_ms(1.5f));
tone(BUZZER_PIN, NOTE_A4, get_note_duration_in_ms(.5f));
tone(BUZZER_PIN, NOTE_G4, get_note_duration_in_ms(1.f));

// 4
tone(BUZZER_PIN, NOTE_E4, 3.f);
tone(BUZZER_PIN, NOTE_E4, get_note_duration_in_ms(3.f));

// 5
tone(BUZZER_PIN, NOTE_D5, 2.f);
tone(BUZZER_PIN, NOTE_D5, 1.f);
tone(BUZZER_PIN, NOTE_D5, get_note_duration_in_ms(2.f));
tone(BUZZER_PIN, NOTE_D5, get_note_duration_in_ms(1.f));

// 6
tone(BUZZER_PIN, NOTE_B4, 3.f);
tone(BUZZER_PIN, NOTE_B4, get_note_duration_in_ms(3.f));

// 7
tone(BUZZER_PIN, NOTE_C5, 2.f);
tone(BUZZER_PIN, NOTE_C5, 1.f);
tone(BUZZER_PIN, NOTE_C5, get_note_duration_in_ms(2.f));
tone(BUZZER_PIN, NOTE_C5, get_note_duration_in_ms(1.f));

// 8
tone(BUZZER_PIN, NOTE_G4, 3.f);
tone(BUZZER_PIN, NOTE_G4, get_note_duration_in_ms(3.f));

// 9
tone(BUZZER_PIN, NOTE_A4, 2.f);
tone(BUZZER_PIN, NOTE_A4, 1.f);
tone(BUZZER_PIN, NOTE_A4, get_note_duration_in_ms(2.f));
tone(BUZZER_PIN, NOTE_A4, get_note_duration_in_ms(1.f));

// 10
tone(BUZZER_PIN, NOTE_C5, 1.5f);
tone(BUZZER_PIN, NOTE_B4, .5f);
tone(BUZZER_PIN, NOTE_A4, 1.f);
tone(BUZZER_PIN, NOTE_C5, get_note_duration_in_ms(1.5f));
tone(BUZZER_PIN, NOTE_B4, get_note_duration_in_ms(.5f));
tone(BUZZER_PIN, NOTE_A4, get_note_duration_in_ms(1.f));

// 11
tone(BUZZER_PIN, NOTE_G4, 1.5f);
tone(BUZZER_PIN, NOTE_A4, .5f);
tone(BUZZER_PIN, NOTE_G4, 1.f);
tone(BUZZER_PIN, NOTE_G4, get_note_duration_in_ms(1.5f));
tone(BUZZER_PIN, NOTE_A4, get_note_duration_in_ms(.5f));
tone(BUZZER_PIN, NOTE_G4, get_note_duration_in_ms(1.f));

// 12
tone(BUZZER_PIN, NOTE_E4, 3.f);
tone(BUZZER_PIN, NOTE_E4, get_note_duration_in_ms(3.f));

// 13
tone(BUZZER_PIN, NOTE_A4, 2.f);
tone(BUZZER_PIN, NOTE_A4, 1.f);
tone(BUZZER_PIN, NOTE_A4, get_note_duration_in_ms(2.f));
tone(BUZZER_PIN, NOTE_A4, get_note_duration_in_ms(1.f));

// 14
tone(BUZZER_PIN, NOTE_C5, 1.5f);
tone(BUZZER_PIN, NOTE_B4, .5f);
tone(BUZZER_PIN, NOTE_A4, 1.f);
tone(BUZZER_PIN, NOTE_C5, get_note_duration_in_ms(1.5f));
tone(BUZZER_PIN, NOTE_B4, get_note_duration_in_ms(.5f));
tone(BUZZER_PIN, NOTE_A4, get_note_duration_in_ms(1.f));

// 15
tone(BUZZER_PIN, NOTE_G4, 1.5f);
tone(BUZZER_PIN, NOTE_A4, .5f);
tone(BUZZER_PIN, NOTE_G4, 1.f);
tone(BUZZER_PIN, NOTE_G4, get_note_duration_in_ms(1.5f));
tone(BUZZER_PIN, NOTE_A4, get_note_duration_in_ms(.5f));
tone(BUZZER_PIN, NOTE_G4, get_note_duration_in_ms(1.f));

// 16
tone(BUZZER_PIN, NOTE_E4, 3.f);
tone(BUZZER_PIN, NOTE_E4, get_note_duration_in_ms(3.f));

// 17
tone(BUZZER_PIN, NOTE_D5, 2.f);
tone(BUZZER_PIN, NOTE_D5, 1.f);
tone(BUZZER_PIN, NOTE_D5, get_note_duration_in_ms(2.f));
tone(BUZZER_PIN, NOTE_D5, get_note_duration_in_ms(1.f));

// 18
tone(BUZZER_PIN, NOTE_F5, 1.5f);
tone(BUZZER_PIN, NOTE_D5, .5f);
tone(BUZZER_PIN, NOTE_B4, 1.f);
tone(BUZZER_PIN, NOTE_F5, get_note_duration_in_ms(1.5f));
tone(BUZZER_PIN, NOTE_D5, get_note_duration_in_ms(.5f));
tone(BUZZER_PIN, NOTE_B4, get_note_duration_in_ms(1.f));

// 19
tone(BUZZER_PIN, NOTE_C5, 3.f);
tone(BUZZER_PIN, NOTE_C5, get_note_duration_in_ms(3.f));

// 20
tone(BUZZER_PIN, NOTE_E5, 3.f);
tone(BUZZER_PIN, NOTE_E5, get_note_duration_in_ms(3.f));

// 21
tone(BUZZER_PIN, NOTE_C5, 1.5f);
tone(BUZZER_PIN, NOTE_G4, .5f);
tone(BUZZER_PIN, NOTE_E4, 1.f);
tone(BUZZER_PIN, NOTE_C5, get_note_duration_in_ms(1.5f));
tone(BUZZER_PIN, NOTE_G4, get_note_duration_in_ms(.5f));
tone(BUZZER_PIN, NOTE_E4, get_note_duration_in_ms(1.f));

// 22
tone(BUZZER_PIN, NOTE_G4, 1.5f);
tone(BUZZER_PIN, NOTE_F4, .5f);
tone(BUZZER_PIN, NOTE_D4, 1.f);
tone(BUZZER_PIN, NOTE_G4, get_note_duration_in_ms(1.5f));
tone(BUZZER_PIN, NOTE_F4, get_note_duration_in_ms(.5f));
tone(BUZZER_PIN, NOTE_D4, get_note_duration_in_ms(1.f));

// 23 & 24
tone(BUZZER_PIN, NOTE_C4, 5.f);
tone(BUZZER_PIN, NOTE_C4, get_note_duration_in_ms(5.f));
// tone(BUZZER_PIN, <Silencio>, 2.f);
// Fin
}
Expand Down

0 comments on commit 3668c42

Please sign in to comment.