Calling a state from within another state

I am using an Arduino Mega to move 4 linear acutators.

Here is my issue - when I first load up the sketch onto the Mega, actuators 1, 2 and 3 do what I expect and switch positions, etc.

However, actuator 4 does not move, instead it seems to get stuck going the wrong way. It is not a wiring issue, I tried reversing them. My sketch is telling the actuator to extend but it does not.

It is not a power issue because the other actuators are either all moving or have moved and "settled" into their targets.

In addition, after several switch "sequences" sometimes actuators 1 and 2 will start to move in reverse, i.e. instead of extending they retract.

I am concerned about the Boolean operators I am using but before that - help me to understand how the "states" work.

I am pretty certain that the state is called on from the void loop. In my sketch, I have a loop calling to the state readbutton(), and within this state, it calls on other states to do work based on switch positions.

I am wondering if this is a mistake (calling a new state from an existing state) and whether I should move my entire if/else chain into the loop and eliminate the state readbutton().

My second question relating to states - is when does the sketch exit the state? does it look to the condition, the condition calls the state, the state runs, then the sketch looks to the condition to see if it is still true, and then runs the state again? Or does the state run continuously "in the background" until the conditions change? Why I ask this is I am wondering how many states can run simultaneously successfully before something locks up.

Or is there a way to "clear the memory" when a condition changes?

For example this:

void loop() {
  readButton();
  }

void readButton()
{
  // All switches off - default position all actuators
  // All switches on - default position all actuators

  b1State = digitalRead(b1Pin);
  // Serial.print("B1:");
  // Serial.print(b1State);
  b2State = digitalRead(b2Pin);
  // Serial.print(" B2:");
  // Serial.print(b2State);
  b3State = digitalRead(b3Pin);
  // Serial.print(" B3:");
  // Serial.print(b3State);
  b4State = digitalRead(b4Pin);
  //  Serial.print(" B4:");
  //  Serial.print(b4State);
  b5State = digitalRead(b5Pin);
  //  Serial.print(" B5:");
  //  Serial.print(b5State);
  b6State = digitalRead(b6Pin);
  //  Serial.print(" B6:");
  //  Serial.println(b6State);
  // Check voltage on Switch 1, If it is HIGH, then Check Voltage on Switch 6
  // Else If Switch 1 is HIGH and 6 is HIGH, then move all actuators to default position

  if (b1State == HIGH && b6State == HIGH)
  {
    updateActuator1Default();
    updateActuator2Default();
    updateActuator3Default();
    updateActuator4Default();

  }
  else if (b1State == HIGH && b6State == LOW && b5State == HIGH)
    //  Switch 1 and 5 are on, 6 is off, POSITION 6
  {
    updateActuator1Position6Right();
    updateActuator2Position6Right();
    updateActuator3Default();
    updateActuator4Position6();

  }
  else if (b1State == HIGH && b6State == LOW && b5State == LOW && b4State == HIGH)
  {
    // Switch 1 is on, switches 5 and 6 are off, POSITION 5
    updateActuator1Position5Left();
    updateActuator2Position5Right();
    updateActuator3Default();
    updateActuator4Position5();
...etc

or this:

void loop() 
{
  // All switches off - default position all actuators
  // All switches on - default position all actuators

  b1State = digitalRead(b1Pin);
  Serial.print("B1:");
  Serial.print(b1State);
  b2State = digitalRead(b2Pin);
  Serial.print(" B2:");
  Serial.print(b2State);
  b3State = digitalRead(b3Pin);
  Serial.print(" B3:");
  Serial.print(b3State);
  b4State = digitalRead(b4Pin);
  Serial.print(" B4:");
  Serial.print(b4State);
  b5State = digitalRead(b5Pin);
  Serial.print(" B5:");
  Serial.print(b5State);
  b6State = digitalRead(b6Pin);
  Serial.print(" B6:");
  Serial.println(b6State);

  // Check voltage on Switch 1, If it is HIGH, then Check Voltage on Switch 6
  // Else If Switch 1 is HIGH and 6 is HIGH, then move all actuators to default position

  if (b1State == HIGH && b6State == HIGH)
  {
    updateActuator1Default();
    updateActuator2Default();
    updateActuator3Default();
    updateActuator4Default();

  }
  else if (b1State == HIGH && b6State == LOW && b5State == HIGH)
    //  Switch 1 and 5 are on, 6 is off, POSITION 6
  {
    updateActuator1Position6Right();
    updateActuator2Position6Right();
    updateActuator3Default();
    updateActuator4Position6();
etc...

You use the word "state" in an unusual way.

SandFarmer wrote (in part):

I am pretty certain that the state is called on from the void loop. In my sketch, I have a loop calling to the state readbutton(), and within this state, it calls on other states to do work based on switch positions.

loop() is neither a state nor a void. It is a function that returns void. void is another name for nothing.

readbutton() is not a state. It is a function. It is a function that returns void.

setup() is a function that returns void and gets called once. That is, it executes exactly once (unless you do something intentional to mess this up).

loop() is a function that returns void that gets called after setup() gets called and returns. loop() will get called again as soon as the previous execution of loop() is ended and not before.

The statements in a function get executed in sequence. It is perfectly acceptable for one of these statements to be the use of (the calling of) another function.

A function ends when it executes a "return" statement, or when the sequence of statements that are executed reaches the end of the function.

No statements and no functions run simultaneously, but they run so fast they may SEEM to execute simultaneously.

When f1() calls f2(), a sort-of bookmark is used. When f2() is complete (because the end of f2() has been reached or because a "return" statement was executed in f2()), execution continues in f1() where execution left off to go to f2(). The "bookmark" helps with this. There is a large (but not infinite) supply of bookmarks, so that f2() may call f3(), f3() may call f4(), and so on.

You show two examples. The examples are not similar because of the comment marks (double slashes). However, if we ignore the comments, then these two examples will provide the same result. The one with readButton() may be slightly easier to read and maintain. The one without readButton() is infinitesimally faster but may be harder to read.

By the way, when you are tempted to name variables b1State, b2State, b3State, b4State, and b5State, it is time to learn about arrays and how to use them.

I have attempted to use terms here appropriate to a beginner (such as "bookmark"). The pedants in the crowd may complain. Oh well.

I have attempted to use terms here appropriate to a beginner (such as "bookmark"). The pedants in the crowd may complain. Oh well.

Anyone who has ever taught a programming class understands that the key is finding an analogy that the student can understand. Those that haven't need to remember back to when they were learning. No one will complain about a reasonable analogy, which yours is.

No one will complain about a reasonable analogy, which yours is.

PaulS: I consider that high praise. THANKS!

Thanks for the tips and the clarifications, I truly appreciate your input.

Don't fret about oversimplifying your lingo - each industry and professional has their own set, it's your ability to explain it us that are new at it that means you truly understand it.

I guess I was too proud of myself that 3 of 4 actuators were working that I started running and tripped. I will be going back a step and trying just one at a time to see if my functions, if that is correct, are working one by one.

And I'll do some reading on arrays...