snakejaw:
An example would be for automating a beer brewing or cheese making process. There are multiple separate loops/steps, instead of one big voidloop... holding temps at specific intervals, activating specific valves etc, never returning to the previous loop again.
With a single void loop setup, is this kind of automation better suited to a raspberry pi/computer programming setting?
Unless I’m mistaken QBasic was single threaded and couldn’t do more than one thing at a time any more than Arduino can. So the programs you are familiar with writing would be either beer brewing OR cheese making, but NOT both tasks at the same time.
What you are describing is traditional linear programming (do this, do that, loop doing that other thing until this is true, etc...) where all of the steps of your sequence are coded in order one after another. If you need to repeat something a certain number of times, or wait for something to happen, you code that loop or wait directly in your program code.
In Arduino you could put all your code in setup() and it would be just the same - a linear programming model.
Linear programming is very good at doing a single task. For example making some rice:
- Weigh out the rice
- Put some water in a pan
- Put the pan on the stove
- Wait for the pan to boil
- Add the rice
- Wait for the rice to cook (stirring occasionally)
- Strain the rice
- Place the rice on a plate
However consider cooking a stir fry to go with it. You could...
a) linearly cook the rice
Followed by...
b) Linearly cook the stir fry
However now your rice is cold.
Microcontroller applications (except very simple ones) very rarely perform just one task, they often have to do multiple tasks at the same time.
How you actually prepare a meal in real life is by interleaving the steps of each individual cooking task. In between doing each step of cooking the rice (and in particular the loops involved while waiting for the water to boil and the rice to cook) you’ll be doing parts of preparing and cooking the stir fry.
This is a state based programming model and any programming language can implement it. So whereas linear programming only allows you to do one thing at once, if you split that task up into small enough “chunks” and use a variable to keep track of which chunk you are currently doing, you can make it appear as though you are doing several different tasks at the same time, even though your processor is single threaded.
So inside the Arduino loop() you’d have code to do all the steps individually but controlled by an “if” or “switch/case” which depends on the current state of the task (i.e. which step of the task we are on). Each step will do a single step of the process (e.g. weigh rice), or check for a particular condition (e.g. is the water boiling yet?) and then advance the state as necessary to ensure that next time round loop() we do the next step of that task.
As long as all your “chunks” of task are short enough and you never wait (all of your waiting should be of the form “Check the clock, has X minutes passed? Yes: move to next state, No: carry on looping), then all of your tasks can be made to appear to run together.