@@ -60,7 +60,7 @@ uint8_t tone_ntimer = TONE_TIMER; // timer used to generate freque
6060bool tone_state = true ; // last pin state for toggling
6161short tone_pin = -1 ; // pin for outputting sound
6262short 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
6464uint16_t tone_tcount = 0 ; // time between handler calls in 1/36 usec
6565uint16_t tone_ncount = 0 ; // handler call between toggling
6666uint16_t tone_n = 0 ; // remaining handler calls before toggling
@@ -146,12 +146,12 @@ void tone(uint32_t pin, uint32_t freq, uint32_t duration) {
146146
147147 tone_timer->pause ();
148148
149- if (freq > 0 && duration > 0 ){
149+ if (freq > 0 ){
150150 uint32_t count = (F_CPU/4 )/freq; // timer counts per half wave
151151 tone_ncount = tone_n = (count>>16 )+1 ; // number of 16-bit count chunk
152152 tone_tcount = count/tone_ncount; // size of count chunk
153153 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 ;
155155 else // no duration specified, continuous sound until noTone() called
156156 tone_nhw = 0 ;
157157
@@ -186,6 +186,7 @@ void tone(uint32_t pin, uint32_t freq, uint32_t duration) {
186186 pinMode (tone_pin, INPUT);
187187
188188 }
189+ while (tone_nhw) ; // blocks till duration elapsed
189190}
190191
191192// //////////////////////////////////////////////////////////////////////////////
0 commit comments