I’m trying to play a little music through a piezo buzzer at the same time I do a simple NeoPixel lightshow. To support the parallelism necessary, I’m trying to avoid delay and use Timers instead. I’m having some trouble getting the timers to work, and it seems like the timers never start or for some other reason my callbacks are never called.
Before I keep digging myself into this hole, I just want to make sure: There’s nothing fancy/funky about tone that prevents it from working with Timer, right? Here’s the basic gist of what I’m trying to do:
toneTimer = new Timer(0, &MusicPlayer::endNote, *this);
pauseTimer = new Timer(0, &MusicPlayer::endPause, *this);
tone(pin, myNote);
toneTimer->changePeriod(toneDuration);
void MusicPlayer::endNote() {
noTone(pin);
toneTimer->stop();
pauseTimer->changeDuration(pauseDuration);
pauseTimer->reset();
}
void MusicPlayer::endPause() {
pauseTimer->stop();
tone(pin, nextNote);
toneTimer->changePeriod(toneDuration);
toneTimer->reset();
}
It’s a bit more complicated than that, but hopefully you get the basic idea. I’m using 2 timers: one for playing the note, one for waiting a bit before the next note. Is this crazy? Will it never work?
Note In case anyone cares, I’m using a similar timer setup with the lightshow and it works like a charm! So, if you need to run a NeoPixel lightshow in parallel with other tasks, Timer is the way to go.