There is a way…it’s called cooperative multi-tasking. Not simple, but hey…give it a try…
But…this might only be possible in Arduino IDE using C…I don’t know mBlock well enough to know if it handles variables well.
Anyways…if you get the itch to go deep…here’s the basic idea…
The trick is to have four different functions (call them music, lights, leds, matrix).
You call them in a loop similar to what you are already doing:
forever{
music
lights
leds
matrix
}
Now for the complicated part (if you’ve never done it before)!
Each function only does a tiny part of it’s overall functionality…it uses variables to track where it is in the sequence. Using the music and leds function and global variables int whichNote and int ledState as an example…
Before calling the first time, you set all variables to their starting value (whichNote = 0; ledState = 0; etc.)
On the first call, the function sees that whichNote = 0 and plays the first note. (We use 0 for the first item in programming). It then increments whichNote to 1 and exits.
The loop code can now call other functions to allow them to do one small piece of their process.
When the loop comes around and calls music again, it sees that whichNote = 1 and plays the second note and increments whichNote again to equal 2.
When whichNote equals a particular value, it can be used to invoke a wait instead. Note that this is a crude method of declaring a wait and the other functions will be stalled also during the wait. A more complicated method uses the system clock, but hey…you’ve go to start somewhere.
Example of music and leds functions (written in pseudo mBlock mixed with C, but you can see how mBlock is similar):
define music
if (whichNote == 0){
play tone on note e4 beat Quarter
}
if (whichNote == 1){
play tone on note e4 beat Quarter
}
if (whichNote == 2){
play tone on note e4 beat Quarter
}
if (whichNote == 3){
wait .3 secs
}
…etc.
whichNote = whichNote +1; //this gets done every call to move to the next note on the next call
define leds
if (ledState == 0){
turn on LED1
}
if (ledState == 1){
turn on LED2
}
if (ledState == 2){
//do nothing
}
if (ledState == 3){
turn off Led1
turn off Led2
}
…etc.
ledState = ledState +1; //this gets done every call to move to the next state for the next call