I have a speaker connected to one of the PWM ports and am modulating it however I would like to tun a stream of signals to the port but have to wait until the last one has played, At present if I send the next signal the one playing stops and the new one starts playing.
Is there a way to check to see if the PWM has finished?
I have managed to do it by setting playback to either 0 or 1 but it would be nice to be able to check to see if this has finished tather than use this. not so much for this project but another one I will be working on at a later date.
The code is part of Michael Smith speaker_pcm and with a few alterations it works really well for my project.
The end goal is to have an anoying sound play at random intervals and when the sound is not playing the unit wil be put to sleep or in standby mode so as to conserve power.
// This is called at 8000 Hz to load the next sample.
ISR(TIMER1_COMPA_vect) {
if (sample >= sounddata_length) {
if (sample == sounddata_length + lastSample) {
stopPlayback();
playback = 0; //sound not playing
Serial.println("Playback Ended");
}
else {
// Ramp down to zero to reduce the click at the end of playback.
OCR2A = sounddata_length + lastSample - sample;
}
}
else {
OCR2A = pgm_read_byte(&sounddata_data[sample]);
}
++sample;
}