Trouble with "Volatile" qualifier

I am using an interrupt routine but I have a conflict when the associated function should execute.

I was expecting the Loop to stop and to hear my 5 second buzz, then the loop to continue its on and off buzz.

Instead, my high pitch buzz goes about 500msec and the loop takes over.

What am I doing wrong?

Thanks a lot

Interrupt_experiment_Mitch_ino.ino (624 Bytes)

I was expecting the Loop to stop

Then your expectation was unreasonable. The tone function is non-blocking, which is a good thing, since you do not want to diddle around in an ISR.

Then the interrupt happens, the tone() function is called, to start a tone sounding. Then, the ISR ends, and loop() resumes. No less than 1/5 a second later, you stop the tone that is happening, or start another tone.

Since freq is not modified in the ISR, it doesn't need to be volatile.

Paul,

Thanks so much for your help. So is there another way to make this happen with an ISR?
Have one freq in the loop and when the IRQ is triggered to have the new tone for the specified time?

Mitch

So is there another way to make this happen with an ISR?
Have one freq in the loop and when the IRQ is triggered to have the new tone for the specified time?

The ISR should stop the current tone, and start the new one.

loop() should NOT be using delay() to determine how long to wait before starting the next tone.

Look at the blink without delay example, without delay. Read, understand, and embrace it.

Each time a tone starts, you should note when that event occurred. Then, on each pass through loop(), determine if it is time to start a new tone. Obviously, then the ISR changes the tone, it needs to record when that happened, so loop() knows about the new lastToneStarted time.