Hello.
I've just started Arduino and I'd love some input. I'm trying to play & shine an LED continuously while the button is pressed. I've been able to get the LED to shine and the sound to play when I press the LED. However, only the LED continues to shine while the sound only plays once.
How do I get the sound to play continuously ??
This is my code (sorry that it's sloppy)
Maybe start a millis timer together with your sound file. Make it restart the sound file after x seconds (when sound file is over). Then also reset the timer.
I guess the playtime of your file will be proportional to its size. So you can automate timing if you have different sound files.
To repeat the playback, it is in fact a one liner (tested).
In the PCM library module named PCM.c, replace this:
// 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();
}
with this:
// This is called at 8000 Hz to load the next sample.
ISR(TIMER1_COMPA_vect) {
if (sample >= sounddata_length) {
if (sample == sounddata_length + lastSample) {
sample=0;
}
Of course it will never stop repeating, so a more elegant solution could be imagined.
The entire library code is so simple and short that you might as well copy/paste it into your sketch, at the bottom, rather than use #include. Then change it in any way you like.
Rather than changing the library, you can call startPlayback(sample, sizeof(sample)); again when the sound ends:
if (buttonState == LOW)
{
digitalWrite(LED, LOW);
// If the sound is not playing, start it.
if (TIMSK1 & _BV(OCIE1A) == 0)
startPlayback(sample, sizeof(sample));
}