I'm trying to play a tone (on an Arduino Mega through the tone library) and have the tone not be interrupted if an interrupt routine is called while the tone is playing. I'm not sure it's possible. The code block is below. Relevant functionality is:
-A while loop runs indefinitely until Button S1 is pressed. Pressing Button S1 triggers an interrupt routine in which interruptFlag_s1 is set to 1.
-This starts a tone playing for 300ms.
-There's a possibility that another button press interrupt is triggered during the 300ms tone. This interrupts the tone for about 0.5ms, enough to make a perceptible break in the tone that I'd like to avoid.
-Ideal scenario is that the audible tone isn't interrupted by the interrupt routine.
while (restart == 0) {
if (interruptFlag_s1 == 1 || interruptFlag_s2 == 1) {
if (interruptFlag_s1 == 1) {
primary_sensor = 1;
interruptFlag_s1 = 0;
rBot = false;
gBot = true;
bBot = false;
} else if (interruptFlag_s2 == 1) {
primary_sensor = 2;
interruptFlag_s2 = 0;
rBot = false;
gBot = false;
bBot = true;
}
interruptFlag_rtc = 0;
finish_id = 0;
button_flag = 0;
time_diff = 0.00;
alt_finish_time = 0.00;
tone(speakerPin, 3000, 300); // Start tone
restart = 1;
}
}
1 Answer 1
The sound is itself generated by an interrupt routine. Since only one interrupt routine can happen at a time, if your own interrupt routine takes too long, then you will get interruption in the sound.
There is nothing you can do about that other than making your interrupt routine short enough that it doesn't cause any interruption in the sound.
tone()
unless the ISR takes too long to execute. Keep your ISR short and all should be good.