0

To clarify I'm extremely new to this and I'm using an Arduino Uno with a wave shield. My circuit works as intended but I haven't learned enough of the code to know what to do to make it do exactly what I want.

Right now the following code makes the sound play on button press but the led sequence doesn't at all. If I move root.rewind(); play(root); to after the last digitalWrite then the led sequence runs but the audio doesn't play until the led sequence ends. I'm out to make the audio play while the led sequence runs. Any suggestions?

if (state == 1) {
    root.rewind();
    play(root);
    for (i = 0; i < 255; i++) { // loop from 0 to 254 (fade in)
        analogWrite(LED,i);     // set the LED brightness
        delay(15);              // wait 15ms because analogWrite
                                // is instantaneous and we would
                                // not see any change
    }
    analogWrite(LED, HIGH);     // turns the LED on
    delay(300);                 // waits for a second
    analogWrite(LED, LOW);      // turns the LED off
    delay(100);                 // waits for a second
    analogWrite(LED, HIGH);     // turns the LED on
    delay(300);                 // waits for a second
    analogWrite(LED, LOW);      // turns the LED off
    delay(100);                 // waits for a second

    digitalWrite(RESET, LOW);
}
2
  • analogWrite(LED, HIGH); is equivalent to analogWrite(LED, 1);
    – gre_gor
    Commented Jul 19, 2016 at 15:58
  • @gre_gor in the code I provided its an i not a one if that's what your referring to.
    – Dan Lexie
    Commented Jul 19, 2016 at 22:55

1 Answer 1

3

As this can not be your entire sketch we can only guess that you are using some sort of SDCard music playing Arduino shield and library.

An application can not call the delay() function if the application is trying to accomplish more than one task. Calls to delay() block the processor from doing anything other than waiting for the delay to time out.

In order to (appear to) do multiple tasks, run a loop with no calls to the delay() function. Instead call the millis() function checking if enough time has elapsed to call the LED function or the sound function. In neither function should you call the delay() function! If you did not write the function (for example you are using a library) you may need to edit the code in the library to ensure it is following the no-calls-to-delay()-function rule!

In general terms, this is referred to as a state machine. In this case, the state machine is driven by the amount of elapsed time as reported by the call to the millis() function.

1
  • Thanks, that works perfectly. Still learning the vocab with this thing, which is why I was blissfully unaware of the millis function. And yes I'm using an adafruit wave shield to play audio from an sd card. I didn't include all that code because the majority of it is one of adafruit example codes to get it working. Thanks again, cheers
    – Dan Lexie
    Commented Jul 19, 2016 at 23:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.