Merging two sketches (bigger picture: asynchronous events)

Hi guys..

having some trouble getting two sketches merged into one.. outside of specific 'code'.... I am just having difficulty grasping all this 'control' (over memory, space, code/events..etc) in C++/micro controller programming still.. (spoiled from exposure to web programming languages..etc)

So outside any specific 'code' help/answers.. any (dumbed up) conversation, explanations are encourage/helpful as well..

ok.. onward. :slight_smile:

Summary:
Working with WaveHC lib & LedControl library (MAX7219/7221).. separate, work fine.. when trying to merge.. the functions (obviously execute one after another.. instead of both separately/individually/asychronously)

Resources/links:

WaveHC Library: Google Code Archive - Long-term storage for Google Code Project Hosting.
LedControl Library: Arduino Playground - LedControl

I have a Arduino/Waveshield that I am using to play audio with.... the problem(s) I am having, keep running into is that the audio (while taking a lot of resources/overhead..etc).. seems to keep the code 'busy/locked up'.... because (in the code) Im using 'stuck' inside one of the playAudio() functions.

(if that makes sense?)

From my understanding of how the WaveHC lib works..
I read it use Timer1 to keep things asynchronous... but from usage and looking at the code..etc.. I think the timer set-up and usage if just for constantly checking the array of buttons/switches states..and setting an appropriate 'flag' (variable).....

(I thought the actual audio playback was based on a timer..which allowed the Arduino to be free to run/execute other functions..etc..)

So my new understanding is that if your project calls for it (doesnt need it.. only 1 button for example..whatever).. you dont NEED to setup the timer to call the 'checkSwitches()' function constantly.......correct? (and hence not use Timer1, have no conflict with any Servo stuff?, not affect PWM pin(s)?....etc..etc... is my understanding of this correct?)

--or--

alternately, assign that 'timer' a different/new function (instead of the checkSwitches function it currently calls).. possibly one that handles/controls the LedControl library (MAX7219/7221) I'm trying to merge it?

if I load my sample LedControl sketch.. works fine..
if I load my sample WaveHC (looping audio attempt) sketch....works fine.

If I try to merge both sketches... Im not understanding how I can keep the audio looping (which more or less keep the code inside the

if(wave.isPlaying){
  //code that executes while loop is playing
}

if I try to put my 'LedControl' code inside that loop.. it obviously only works when the audio is playing... but then is stuck inside the LedControl 'loop' (for animations on the Led Matrix..etc)..and the Aduio doesnt 'loop/start again' until the LedControl 'loop' is complete...

I know this 'makes' sense.. and is really just doing what the code is telling it to do.. Im looking for not only a solution to MY problem.. but general overview discussion on how more advanced coders/developers over come this type of obstacle? Im sure its a pretty common one that beginners like myself run into...

Maybe this is where more advanced knowledge/use of timers & interrupts come into play? My understanding of those is that you cant (shouldnt)really 'do' much inside those functions/routines.. just set some 'flags'.. and exit...let the MAIN loop (or sub routines) handle the newly set flags/variables and act accordingly..... right?

bottom line is .. Im looking to be able to play audio.. whether single sounds/slips.. 'or' looping an audio track.. and be able to do a few other things... change leds states..etc..etc.

hopefully it'll 'click' soon... haha..

**edit: please excuse my code.. and the TONS of comments.. I tend to 'over-do-it' as ithelps me follow along in my code where I am..what is executing.. where its stopping/breaking.. (as well trying new things I have no clue what it will return)

attached are the:
LedControl: breathing demo sketch
WaveHC: looping audio track sketch
Attempted Merge of both above sketches

if there is anything more I can provide.. let me know.

thanks everyone, appreciate all legit advice! :slight_smile:

LedControl_breathingDemo.txt (5.32 KB)

WaveHC_audioLoop.txt (9.18 KB)

Merge_attempt1.txt (10.4 KB)