0

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;
 }
 }
asked Mar 8, 2020 at 17:19
2
  • 1
    Interrupts should not affect tone() unless the ISR takes too long to execute. Keep your ISR short and all should be good. Commented Mar 8, 2020 at 17:50
  • Did you try it and experience discontinuity? Commented Mar 8, 2020 at 20:26

1 Answer 1

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.

answered Mar 8, 2020 at 21:25

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.