Run several functions at the same time


#1

Hi,

I making my first steps into scratch and I’m still not familiar with some procedures. Taking the code below, I would like to have the board in addition to play the Jingle Bells song I would like the onboard leds to flash alternatively as well as to have the led Matrix to display a message. The problem is that I don’t know how to make the music, the leds and the led matrix to work at the same time.

Thanks,


#2

@aha,

Unfortunately, this is not possible. The Arduino based processors do not have multi-processing capabilities. This means that they can only do sequential processing (one instruction must complete, before the next one can be executed).

That said, you could try adding the desired lights and messages,as needed, to the music function.

Play Sound
Set LED Color
Show Message
Play Sound
Set LED Color
Show Message

Good Luck,

Mike


#3

Mike thanks for the response. Do you know if this can be possible by connecting the arduino board to a raspberry pi?

Thanks,
Alex


#4

@aha

Alex,

Since the Raspberry Pi run on the Linux OS, it should be able to do multi-thread operations. I haven’t played around with the Raspberry Pi much, but reading through the forum, there seems to have been at least one discussion on the subject.

Mike


#5

Mike,

Broadcast command can help perform multithreading but the processor does not have enough power.

See code below


#6

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


#7

All,

For reference:

learn.adafruit

using-freertos-multi-tasking-in-arduino

As you read through these references, you will see that multitasking is not possible on Arduino based processors. There are however, a few ways to improve performance and make it look like multitasking is happening.

Good Luck,

Mike


#8