Looped soundtracks
Now we will learn how to play a constant, looped soundtrack in the background.
Connect your last button to pin 7.
Open the looped-soundtracks sketch. Note that it has another button, and we added all of the following to support the new button:
int button3 = 7
Pb_switch sw3(100)
flag3
pinMode
declaration insetup()
- read instruction for
flag3
inside thereadinputs()
function
Notice that there is a second tab open next to the looped-soundtracks sketch called mytunes.h. We are now defining all the songs in the mytunes.h, instead of in the main
program file, and importing the contents of mytunes.h by typing #include "mytunes.h"
at the top of the main sketch (similar to how we #include <Pinball.h>
). Using multiple tabs is a way of keeping the code more readable.
Upload the code. Do you recognize the melody?
CHECKPOINT!
Inside setup()
, we have:
spkr.loopstart(marionotes2, mariotimes2, 61);
This is another example of a looped event. The arrays marionotes2[]
and mariotimes2[]
have been
defined inside mysongs.h
. The command starts playing that melody
in loop in the background. It is interrupted whenever a button is
pressed, and each button plays a different melody (also defined in mysongs.h
).
You can stop the loop track anytime by using loopstop()
, just
like for looped timed events.
Note the spkr.update()
line inside writeoutputs()
.
The program has a new flag flag3
, and new switch mysw3
. Take a look at writeoutputs()
function and try to
figure out what they are doing.
CHECKPOINT!
Next we’ll put together everything we’ve learned so far!