Possibilty of multiple processes

So I was thinking. As the arm processor is 72MHz with an arduino at only 16MHz. In theory the core should be over 4 times faster then the arduino. Now while this is the case and the core is going to have a newer loop method for the cloud check and one for the user loop. Could the core not in theory have 4 processes running at the same time and still run the same as an arduino?

I know that some places have it so that one process is run from each loop in order, so the core wouldin theory be processing 4 loops at over 16MHz each.

It would be great if this could be introduced at some point in the future. What do we all think? This would obviously become an issue where some things take a long time, like the cloud check could or even a delay in a loop. So it would be nice to see it run tasks in order aslong as nothing is delaying it and if something is then skip that until it is free to run the next task again

It doesn’t quite work like that, sadly. A microprocessor executes instructions one by one; it takes quite a bit of time to flush the pipeline and load in a new set of instructions, so you wouldn’t be able to run 4 processes in parallel flat out, like you’re thinking. You’d have to use some sort of time division multiplexing scheme to give each process enough resources to finish a given task before moving on to the next one. Unfortunately, this means your PWM outputs will drop off while the “time slices” of the other 3 processes are active, this includes anything else that expects “real time” control. Essentially you’d be making a very lightweight multitasking OS.

You could even run something like Linux on the Cortex line, but without direct memory access (DMA) you’re still going to run into the problem of no predictable real time control of tasks. This is why it’s difficult to do Robotics with a Raspberry Pi by itself.

That said, you could create as many functions as you wanted and conditionally call them from the main loop, so that under certain conditions the main loop branches out and runs through your function(s) in a loop.

1 Like

Im still very new to this so im not entirely suure how it works. I read the word arm and think, mobile phone. But then my phone has over 200 times the performance of these in a single core.

Would be nice if it could happen though. Especially when you have things like a udp time server check. This can take over a second to complete which when running sebsors can be a pain.

My current arduino actually does as you said, has a bunch of conditions then runs certain tasks if the conditions are correct.

It gets a little confusing as I originally started off building and programming picaxes which had their “multi processing” which is what I described earlier. Ive been using an arduino for about 4 weeks and now im jumping to the core and things are changing quickly. Blame university for making me want more and more complex things that are overkill.

My first project when it comes us a simple light control. However it has a motion sensor and light sensor. So it will only trigger the lights on motion if the room brightness is below a set amount. It also runs to the local time so it wont trigger between set times. And can be controlled directly from the internet and hopefully an android app to override the sensor control entirely.

Massive overkill but is there ever a better option? :-p

Well, if you want to do a lot of things at once and really enjoy overkill, a Raspberry Pi (Model A) is actually cheaper than a Spark Core ($25), which you could couple with a $5 USB WiFi dongle and essentially have unlimited programability.

I did think that, but their a little big for this project. I have a 12v transformer. 5v converter. Motion sensor. Light sensor. And the main board so size was a factor on this one. But I may have to get one in january just to mess about with

It sounds like you should be able to accomplish what you’re trying to do with a single loop. Once you dig into it, if you want to share some sample code, we can help you debug and refactor if you’re not yet familiar with the embedded/arduino world

@bigjme Sounds “simple” but it is actually a complex project for most beginners. Fortunately the Spark Core does much of the hard work for you making this easy and can handle reading one analog (light sensor) and one digital (motion sensor) input, manage a real time clock for timed action, and allow override control of this through exposing Spark Functions (turnOn() and turnOff() or slightly more complex turnDevice(bool val) and pass it “ON” or “OFF”), all in the name of controlling one output.

My suggestion for you is to start by experimenting with each piece separately until they work flawlessly. Then start coupling them together.

You may need to debounce your digital input if your motion sensor does not have any hysteresis built into it, however it’s output should change states (high or low) and stay that way for a little bit… at least while there is motion. When the motion settles it should change back to the at rest state (high or low). If not, the output could jitter high and low while the motion is in an in-between state. This would require debouncing.

Your analog input shouldn’t require any filtering if your light levels are not wildly changing. If they are expected to change rapidly you might want to add a digital filter. I have an example here of how to filter an analog input https://community.spark.io/t/spark-core-execution-speed/884/6 This example also shows how to setup timed loops, and counters for other timed processes. You would need something like that to effectively debounce a digital input.

Here is how to expose internal functions over the internet: http://docs.spark.io/#/firmware/cloud-spark-function

You could put together a simple single page web application (SPA) that controls your functions and returns status on the current condition of the output with something like this pretty easily: http://jflasher.github.io/spark-helper/

To figure out what the current state of the output is, you could access an internal state variable that controls your output like this: http://docs.spark.io/#/firmware/cloud-spark-variable

I don’t believe there is a Real Time clock example yet, but I’m sure there will be one soon. The functionality is there already. You just need an easy way of setting the time (maybe via atomic server) and then polling for the current time and comparing vs. some preset constants. Maybe later you can work up a way to set the time over the internet through a function.