cant get code to work as I want? independent functions outside of 'loop"?

hey gang-

Im editing the WaveHC/WaveShield examples I see posted.. to suit my needs.

I dont mess with the initial SD/partition reading stuff at the top of all the sketches..etc....until 'READY'..

in most (all) the examples..they use a 6 button configuration.. and use a 'loop' to keep checking the states of all the buttons...etc
(default examples here)


hey gang-

Im editing the WaveHC/WaveShield examples I see posted.. to suit my needs.
(default examples here)

I am looking for more discussion/feedback...even a more broader/general understanding on how the code executes..and moving outside of the loop..and back..etc..

my question (first general) is more about WHERE (or when) in the code we can actually perform other tasks???

this may be my lack of Arduino/C programming.. and having to keep the main loop going..etc..

but I cant seem to get another function (PWM fade out an LED).. to happen STANDALONE outside of the audio playing function(s)?

what I mean by this is...

from what I have read (and most of it confusing).. only certain pins/timers are left after the WaveShield/WaveHC lib..etc..

I have done some mods..and I have these pins free:
A4 - I2C
A5 - I2C

D0 - RX
D1 - TX
D3 - PWM
D5 - PWM
D6 - PWM
D9 - PWM

although I understand I cant use PIn 9 as it uses the same timer as the WaveHC lib.

I have Pin3, Pin5 & Pin6 open..

I think I understand the basics of the lib.. (setting/giving several button state reference to use in your playing of the audio file (looping, stopping, playing complete..etc)

what I cant grasp is how/where I can code so I can do OTHER tasks, while the audio stuff is going on.

Im not sure what or how MANY times the the shield/lib uses in total? Just one? or two?

Also from searching here (and google) and seeing the limited details given.. I have only seen suggestions/answers of:

you have a tight loop here:

//do whatever
//is done playing now

to add my code.

Which Im assuming was meant for the playcomplete() function????

the problem is.. if you use the while(isplaying){} function in one of the playfile() or playcomplete() functions..

your are STUCK inside that code snippet/routine is seems.. ..even if the audio is done playing?

maybe having/trying a quick goal would be better to get the idea/point across.

using the waveHC lib examples.. I hacked together a simple variant..

that when button[0] is pressed... if calls a routine where I check for a HIGH/LOW value of another switch..

if HIGH...... if use the normal playfile() function...etc

if LOW... I then start a 'timer' (think blink without delay).. so I can repeatedly call this playfile() function...

basically giving me a normal firing sound.. or an autofire sound (if the button is held down)..

code example:

unsigned long currentAutoMillis = millis();  
  //main trigger button code/routine
  if (justpressed[0]) {
    if(digitalRead(modeSelect)== HIGH){
      //reset button var/value to show not pressed
      justpressed[0] = 0;
      putstring_nl("slider is high");
    else if(digitalRead(modeSelect) == LOW){
      //auto/burst mode
      if(currentAutoMillis - previousAutoMillis > autoInterval) {
        // save the last time you blinked the LED 
        previousAutoMillis = currentAutoMillis;

        putstring_nl("slider is low");
        //wave.stop(); //needed?  better form?  what about..breaks code.
        if(pressed[0] != justpressed[0]){
          putstring_nl("button released");
          //reset button var/value to show not pressed
          justpressed[0] = 0;

this all seems to be working just fine.. (as far as sound effects and the button reading/states)..etc

the problem comes into play when I want to try and take this another step further.... and add a LED into the mix.. to visually represent the sound being played.. (basically turn an led on when the sound plays.. and fade it out quickly... if on auto fire.. the led will turn on and fade out (as far as it can) before the sound is played again)...

here are the default playcomplete() and playfile() functions given in the examples:

// Plays a full file from beginning to end
void playcomplete(char *name) {
  // call our helper to find and play this name
  while (wave.isplaying) {
    // do nothing while its playing
  // now its done playing

void playfile(char *name) {
  // see if the wave object is currently doing something
  if (wave.isplaying) {// already playing something, so stop it!
    //putstring_nl("stopping current audio.........."); 
    wave.stop(); // stop it
  // look in the root directory and open the file
  if (!, name)) {
    putstring("Couldn't open file "); 
  // OK read the file and turn it into a wave object
  if (!wave.create(f)) {
    putstring_nl("Not a valid WAV"); 
  // ok time to play! start playback;

if you add code to the:


like.. lets say a loop to turn on the led (255)..and fade it down to 0..
you are stuck in that loop... even if you are in autofire mode..and should be firing over & over..and hence light up the led each time..

So my problem(s) outside of the above is understand where or how I can have other functions (like this led fade out for example) execute/run independently from this audio function??

a quick example would help turn the light on.. as I have tried moving blocks of code all over.. from using delays in the led fade out to using another milis() check....

from my end it doesnt HAVE to be connected to when the wave.isplaying...

it should just be a function that is called/executed when the file is triggered to play.. q quick blip..and fade out.. each time.. like for a prop blaster/gun toy..

suggestions on how I can go about accomplishing this?


The Arduino calls the loop() function repeatedly. You can do whatever things you want in that function.

One style of programming is to do things synchronously. In this style, the program stops and waits until each action has completed before moving on to the next action. If you write a program like this, it can only do one thing at a time. This style of programming is demonstrated in the 'blink' example sketch.

A more flexible style of programming is to do things asynchronously. In this style, the program runs without stopping and while running it looks for things it needs to do. So it could see whether any input has arrived on the serial port (and handle it, if there is any), and see whether enough time has passed for it to be time to turn an output on or off to make that LED blink, and see whether any of the inputs it uses has changed (and handle the change if it has). This preferred style of programming is demonstrated in the 'blink without delay' example sketch.


thanks for the reply..

If you look.. I already am using the 'blink without delay' style approach..

(this is what is getting me my 'auto-fire' results)

in the MAIN loop.. it checks the status of the button state(s) and executes code based on those results of the button(s) states. (justpressed, justreleased or pressed (ie: curently being pressed))

which basically just calls/triggers an audio file to play.

what I cant get working is a led on ANY of my open pins.. to turn on and fade out very quickly.. every time the audio triggers.

I usually get stuck waiting until the fade out is completed.. (even though in auto fire mode.. it should rarely have enough time to fade out completely..etc)..

from reading I was hoping/thinking that using SoftPWM since it uses a timer/interrupt that can be run outside of the main loop/code..etc
that this would let me turn on an LED instantly..and start fading it out (using a pre-set fade out speed)..

but after having problems finally getting it to compile under IDE v.23...

every time I even import the lib.. I get odd results.. and code wont run right....etc.. Im thinking a conflict in timers somewhere?

(but was under impression they could be used together)..etc..

I already am using the 'blink without delay' style approach..

In that case you should already understand that this technique enables you to do things independently within loop().


I just dont think you understand it correctly.. (probably I didnt explain it good enough then) :wink:

either way I got it sorted out.. and what I wanted was overkill anyways..

the problem was being stuck in a loop (for some led blinking).. when the main loop needed to still be running.

(hence asking about running independently outside of loop)

the problem was being stuck in a loop (for some led blinking).. when the main loop needed to still be running.

Yes, this is exactly the problem you avoid by using the 'blink without delay' approach.