Stepped sequence programming in Arduino

I've gotten a solid grasp on performing singular functions in arduino... eg, holding a temperature at a certain set point, ala sous vide... or opening a valve on a button press, or after a delay... and performing looped functions for repetitive automation. Arduino has proven to be a valuable tool in those settings.

I'm wondering if someone can point me to an example of a stepped sequence (not sure if theres a better term for it) program, for more complex automation?

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?

I remember playing with QBasic in the 90s and can map out how I'd do it there... but QBasic didnt operate on a loop function unless you told it to, and you could have multiple sequential loops in the order you desire.

I'm not intending to knock Arduino, as I recognize I am not an expert in C language, but rather trying to find its limitations so I know when to use it vs other microprocessors. If it is possible, I would love to learn it in that capacity.

Thanks for any insight.

An Arduino can do this sort of thing. My example program is not on the device that I am using for a response so I cannot provide an example at this time.

The loop() function can handle this. I prefer to do this using a variable to keep track of the state. You need to learn about the FSM (Finite State Machine). Maybe the Arduino IDE has an example. Perhaps you can use a search engine (e.g., Google) to get more information. Good Luck!

The C++ datatype enum is helpful but not required.

In your library manager look at LC_baseTools There is a library in there that I use on ALL my programs idler. Gives you the ability to run as many things as you want at the same time.

Well.. Not at the same time. One processor can only do one thing at a time these days. But they switch so fast, you'll never notice.

I never do..

-jim lee

there is a command that is used when coding state machines it is called switch case.

switch case does mutually exclusive execute parts of code for each "case" you define.

As a short and basic description a statemachine does

Start with executing "startcode" (and only execute startcode)

if conditions to go ahead with step2 are fullfilled switch over to execute code of "step 2" (and only execute code of step2)

if conditions to go ahead with step3 are fullfilled switch over to execute code of "step 3" (and only execute code of step3)

etc. etc.

There are a lot of examples about state machines. Most of them are like most other tutorials: not aware of the low knowledge-level a newbee has and therefore using too much technical terms (that are (yet) unnkown by a newbee) or doing too much new things at the same time

I haven't found yet a tutorial about state-machines that fullfills my high requirements on "easyness to understand"
If somebody has suggestions please post the links

To me this tutorial seems to be a bit easier than others

state machine example

jimLee:
In your library manager look at LC_baseTools There is a library in there that I use on ALL my programs idler. Gives you the ability to run as many things as you want at the same time.

Well.. Not at the same time. One processor can only do one thing at a time these days. But they switch so fast, you'll never notice.
I never do..
-jim lee

Hi Jim,
I have looked into the two examples that you provide on GitHUB,
These two examples just show how to blink but show nothing about how you would use it with a state-machine.
Nor does it show how to use it to do multiple things in different time intervals.
I call this the "experts blindness to beginners difficulties"
May I suggest to add more examples how to use it?
best regards Stefan

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.

Guys, you only have to make the human think it multi threaded.

My stuff give you a base class that has a method void idle(void) { fill the blank }

Is it multi threaded? No! But that doesn't matter. As far as the human is concerned, it lets them think it is. Each object does its little thing during its idle method. And, when you step back, its all running concurrently. You get the result you are looking for, without all the hassle.

Visual BASIC had the same thing. You could get an object that ran in idle time to do things while the program was running. Same idea.

In fact Metroweks had it too for building Mac applications. Back in the day.

@StefanL38 : Yes, it needs more examples. But more than that, it needs a few people to actually try it!

-jim lee

Hi pcbbc,

that is a very good allday-example of the basic principles of the state-machine.
If I find time I will write some demo-code based on this example that does a serial outputted simulation of cooking a meal.

best regards Stefan

There is a short program in this link (in Reply #15) that illustrates working through a sequence of states - it may give you some ideas.

...R