@@ -60,7 +60,7 @@ uint8_t tone_ntimer = TONE_TIMER; // timer used to generate freque
60
60
bool tone_state = true ; // last pin state for toggling
61
61
short tone_pin = -1 ; // pin for outputting sound
62
62
short tone_freq = 444 ; // tone frequency (0=pause)
63
- uint32_t tone_nhw = 0 ; // tone duration in number of half waves
63
+ volatile uint32_t tone_nhw = 0 ; // tone duration in number of half waves
64
64
uint16_t tone_tcount = 0 ; // time between handler calls in 1/36 usec
65
65
uint16_t tone_ncount = 0 ; // handler call between toggling
66
66
uint16_t tone_n = 0 ; // remaining handler calls before toggling
@@ -146,12 +146,12 @@ void tone(uint32_t pin, uint32_t freq, uint32_t duration) {
146
146
147
147
tone_timer->pause ();
148
148
149
- if (freq > 0 && duration > 0 ){
149
+ if (freq > 0 ){
150
150
uint32_t count = (F_CPU/4 )/freq; // timer counts per half wave
151
151
tone_ncount = tone_n = (count>>16 )+1 ; // number of 16-bit count chunk
152
152
tone_tcount = count/tone_ncount; // size of count chunk
153
153
if (duration > 0 ) // number of half waves to be generated
154
- tone_nhw = ((duration*( freq> 0 ?freq: 100 ) )/1000 )<<1 ;
154
+ tone_nhw = ((duration*freq)/1000 )<<1 ;
155
155
else // no duration specified, continuous sound until noTone() called
156
156
tone_nhw = 0 ;
157
157
@@ -186,6 +186,7 @@ void tone(uint32_t pin, uint32_t freq, uint32_t duration) {
186
186
pinMode (tone_pin, INPUT);
187
187
188
188
}
189
+ while (tone_nhw) ; // blocks till duration elapsed
189
190
}
190
191
191
192
// //////////////////////////////////////////////////////////////////////////////
0 commit comments