|
41 | 41 | #define NOTE_E5 659 |
42 | 42 | #define NOTE_F5 698 |
43 | 43 |
|
44 | | -#define SPEED 400U // Speed of each note in ms |
45 | | -#define SILENCE 10U // 10 ms of silence after each note |
| 44 | +#define NOTE_DURATION 400U // Speed of each note in ms |
46 | 45 |
|
47 | | -uint slice_num; // Variable to save the PWM slice number |
| 46 | +uint32_t get_note_duration_in_ms(float note_duration) { |
| 47 | + return (uint32_t)(note_duration * NOTE_DURATION); |
| 48 | +} |
48 | 49 |
|
49 | 50 | int main() { |
50 | 51 | // Configure tone generation on BUZZER_PIN |
51 | 52 | tone_init(BUZZER_PIN); |
52 | 53 | while (1) { |
53 | 54 | // 1 |
54 | | - tone(BUZZER_PIN, NOTE_G4, 1.5f); |
55 | | - tone(BUZZER_PIN, NOTE_A4, .5f); |
56 | | - tone(BUZZER_PIN, NOTE_G4, 1.f); |
| 55 | + tone(BUZZER_PIN, NOTE_G4, get_note_duration_in_ms(1.5f)); |
| 56 | + tone(BUZZER_PIN, NOTE_A4, get_note_duration_in_ms(.5f)); |
| 57 | + tone(BUZZER_PIN, NOTE_G4, get_note_duration_in_ms(1.f)); |
57 | 58 |
|
58 | 59 | // 2 |
59 | | - tone(BUZZER_PIN, NOTE_E4, 3.f); |
| 60 | + tone(BUZZER_PIN, NOTE_E4, get_note_duration_in_ms(3.f)); |
60 | 61 |
|
61 | 62 | // 3 |
62 | | - tone(BUZZER_PIN, NOTE_G4, 1.5f); |
63 | | - tone(BUZZER_PIN, NOTE_A4, .5f); |
64 | | - tone(BUZZER_PIN, NOTE_G4, 1.f); |
| 63 | + tone(BUZZER_PIN, NOTE_G4, get_note_duration_in_ms(1.5f)); |
| 64 | + tone(BUZZER_PIN, NOTE_A4, get_note_duration_in_ms(.5f)); |
| 65 | + tone(BUZZER_PIN, NOTE_G4, get_note_duration_in_ms(1.f)); |
65 | 66 |
|
66 | 67 | // 4 |
67 | | - tone(BUZZER_PIN, NOTE_E4, 3.f); |
| 68 | + tone(BUZZER_PIN, NOTE_E4, get_note_duration_in_ms(3.f)); |
68 | 69 |
|
69 | 70 | // 5 |
70 | | - tone(BUZZER_PIN, NOTE_D5, 2.f); |
71 | | - tone(BUZZER_PIN, NOTE_D5, 1.f); |
| 71 | + tone(BUZZER_PIN, NOTE_D5, get_note_duration_in_ms(2.f)); |
| 72 | + tone(BUZZER_PIN, NOTE_D5, get_note_duration_in_ms(1.f)); |
72 | 73 |
|
73 | 74 | // 6 |
74 | | - tone(BUZZER_PIN, NOTE_B4, 3.f); |
| 75 | + tone(BUZZER_PIN, NOTE_B4, get_note_duration_in_ms(3.f)); |
75 | 76 |
|
76 | 77 | // 7 |
77 | | - tone(BUZZER_PIN, NOTE_C5, 2.f); |
78 | | - tone(BUZZER_PIN, NOTE_C5, 1.f); |
| 78 | + tone(BUZZER_PIN, NOTE_C5, get_note_duration_in_ms(2.f)); |
| 79 | + tone(BUZZER_PIN, NOTE_C5, get_note_duration_in_ms(1.f)); |
79 | 80 |
|
80 | 81 | // 8 |
81 | | - tone(BUZZER_PIN, NOTE_G4, 3.f); |
| 82 | + tone(BUZZER_PIN, NOTE_G4, get_note_duration_in_ms(3.f)); |
82 | 83 |
|
83 | 84 | // 9 |
84 | | - tone(BUZZER_PIN, NOTE_A4, 2.f); |
85 | | - tone(BUZZER_PIN, NOTE_A4, 1.f); |
| 85 | + tone(BUZZER_PIN, NOTE_A4, get_note_duration_in_ms(2.f)); |
| 86 | + tone(BUZZER_PIN, NOTE_A4, get_note_duration_in_ms(1.f)); |
86 | 87 |
|
87 | 88 | // 10 |
88 | | - tone(BUZZER_PIN, NOTE_C5, 1.5f); |
89 | | - tone(BUZZER_PIN, NOTE_B4, .5f); |
90 | | - tone(BUZZER_PIN, NOTE_A4, 1.f); |
| 89 | + tone(BUZZER_PIN, NOTE_C5, get_note_duration_in_ms(1.5f)); |
| 90 | + tone(BUZZER_PIN, NOTE_B4, get_note_duration_in_ms(.5f)); |
| 91 | + tone(BUZZER_PIN, NOTE_A4, get_note_duration_in_ms(1.f)); |
91 | 92 |
|
92 | 93 | // 11 |
93 | | - tone(BUZZER_PIN, NOTE_G4, 1.5f); |
94 | | - tone(BUZZER_PIN, NOTE_A4, .5f); |
95 | | - tone(BUZZER_PIN, NOTE_G4, 1.f); |
| 94 | + tone(BUZZER_PIN, NOTE_G4, get_note_duration_in_ms(1.5f)); |
| 95 | + tone(BUZZER_PIN, NOTE_A4, get_note_duration_in_ms(.5f)); |
| 96 | + tone(BUZZER_PIN, NOTE_G4, get_note_duration_in_ms(1.f)); |
96 | 97 |
|
97 | 98 | // 12 |
98 | | - tone(BUZZER_PIN, NOTE_E4, 3.f); |
| 99 | + tone(BUZZER_PIN, NOTE_E4, get_note_duration_in_ms(3.f)); |
99 | 100 |
|
100 | 101 | // 13 |
101 | | - tone(BUZZER_PIN, NOTE_A4, 2.f); |
102 | | - tone(BUZZER_PIN, NOTE_A4, 1.f); |
| 102 | + tone(BUZZER_PIN, NOTE_A4, get_note_duration_in_ms(2.f)); |
| 103 | + tone(BUZZER_PIN, NOTE_A4, get_note_duration_in_ms(1.f)); |
103 | 104 |
|
104 | 105 | // 14 |
105 | | - tone(BUZZER_PIN, NOTE_C5, 1.5f); |
106 | | - tone(BUZZER_PIN, NOTE_B4, .5f); |
107 | | - tone(BUZZER_PIN, NOTE_A4, 1.f); |
| 106 | + tone(BUZZER_PIN, NOTE_C5, get_note_duration_in_ms(1.5f)); |
| 107 | + tone(BUZZER_PIN, NOTE_B4, get_note_duration_in_ms(.5f)); |
| 108 | + tone(BUZZER_PIN, NOTE_A4, get_note_duration_in_ms(1.f)); |
108 | 109 |
|
109 | 110 | // 15 |
110 | | - tone(BUZZER_PIN, NOTE_G4, 1.5f); |
111 | | - tone(BUZZER_PIN, NOTE_A4, .5f); |
112 | | - tone(BUZZER_PIN, NOTE_G4, 1.f); |
| 111 | + tone(BUZZER_PIN, NOTE_G4, get_note_duration_in_ms(1.5f)); |
| 112 | + tone(BUZZER_PIN, NOTE_A4, get_note_duration_in_ms(.5f)); |
| 113 | + tone(BUZZER_PIN, NOTE_G4, get_note_duration_in_ms(1.f)); |
113 | 114 |
|
114 | 115 | // 16 |
115 | | - tone(BUZZER_PIN, NOTE_E4, 3.f); |
| 116 | + tone(BUZZER_PIN, NOTE_E4, get_note_duration_in_ms(3.f)); |
116 | 117 |
|
117 | 118 | // 17 |
118 | | - tone(BUZZER_PIN, NOTE_D5, 2.f); |
119 | | - tone(BUZZER_PIN, NOTE_D5, 1.f); |
| 119 | + tone(BUZZER_PIN, NOTE_D5, get_note_duration_in_ms(2.f)); |
| 120 | + tone(BUZZER_PIN, NOTE_D5, get_note_duration_in_ms(1.f)); |
120 | 121 |
|
121 | 122 | // 18 |
122 | | - tone(BUZZER_PIN, NOTE_F5, 1.5f); |
123 | | - tone(BUZZER_PIN, NOTE_D5, .5f); |
124 | | - tone(BUZZER_PIN, NOTE_B4, 1.f); |
| 123 | + tone(BUZZER_PIN, NOTE_F5, get_note_duration_in_ms(1.5f)); |
| 124 | + tone(BUZZER_PIN, NOTE_D5, get_note_duration_in_ms(.5f)); |
| 125 | + tone(BUZZER_PIN, NOTE_B4, get_note_duration_in_ms(1.f)); |
125 | 126 |
|
126 | 127 | // 19 |
127 | | - tone(BUZZER_PIN, NOTE_C5, 3.f); |
| 128 | + tone(BUZZER_PIN, NOTE_C5, get_note_duration_in_ms(3.f)); |
128 | 129 |
|
129 | 130 | // 20 |
130 | | - tone(BUZZER_PIN, NOTE_E5, 3.f); |
| 131 | + tone(BUZZER_PIN, NOTE_E5, get_note_duration_in_ms(3.f)); |
131 | 132 |
|
132 | 133 | // 21 |
133 | | - tone(BUZZER_PIN, NOTE_C5, 1.5f); |
134 | | - tone(BUZZER_PIN, NOTE_G4, .5f); |
135 | | - tone(BUZZER_PIN, NOTE_E4, 1.f); |
| 134 | + tone(BUZZER_PIN, NOTE_C5, get_note_duration_in_ms(1.5f)); |
| 135 | + tone(BUZZER_PIN, NOTE_G4, get_note_duration_in_ms(.5f)); |
| 136 | + tone(BUZZER_PIN, NOTE_E4, get_note_duration_in_ms(1.f)); |
136 | 137 |
|
137 | 138 | // 22 |
138 | | - tone(BUZZER_PIN, NOTE_G4, 1.5f); |
139 | | - tone(BUZZER_PIN, NOTE_F4, .5f); |
140 | | - tone(BUZZER_PIN, NOTE_D4, 1.f); |
| 139 | + tone(BUZZER_PIN, NOTE_G4, get_note_duration_in_ms(1.5f)); |
| 140 | + tone(BUZZER_PIN, NOTE_F4, get_note_duration_in_ms(.5f)); |
| 141 | + tone(BUZZER_PIN, NOTE_D4, get_note_duration_in_ms(1.f)); |
141 | 142 |
|
142 | 143 | // 23 & 24 |
143 | | - tone(BUZZER_PIN, NOTE_C4, 5.f); |
| 144 | + tone(BUZZER_PIN, NOTE_C4, get_note_duration_in_ms(5.f)); |
144 | 145 | // tone(BUZZER_PIN, <Silencio>, 2.f); |
145 | 146 | // Fin |
146 | 147 | } |
|
0 commit comments