If statement improovement

So i am reading a staemachines last state using EEPROM and reading it. i then want to compare last state and then continue from there. As such i put together a couple of if statements to check which statement is true and execute to enter correct state.

here is a dummy code version

  void setup() {
  Serial.begin(9600);
  unsigned long LastCellNumber = readElapsedTimeCellsFromEEPROM(87); //reading timer value
  unsigned long LastFanNumber = readElapsedTimeFanFromEEPROM(93);   // reading timer value
  int LastState = readStateFromEEPROM(96);                          // Reading stae machine's last state
 
if (LastState == 1) stateMachine.(state1); // Initialise state machine in state 1
if (LastState == 2) stateMachine.(state2); // Initialise state machine in state 2
if (LastState == 3) stateMachine.(state3); // Initialise state machine in state 3
if (LastState == 4) stateMachine.(state4); // Initialise state machine in state 4
if (LastState == 5) stateMachine.(state5); // Initialise state machine in state 5
if (LastState == 6) stateMachine.(state6); // Initialise state machine in state 6
}

Is there a better way to code the if statements i.e do it in less lines or have it do the process quicker? Basically trying to improve my coding knowledge and skills.

Hello
you can use the switch/case instruction, too

1 Like

Interesting, haven't tried a switch statement before, any chance you know if there are any benefits to using one over the other? if unsure I'll have a browse around.

Hello
Generaly I´m using the if-instruction for data flow control and the switch case instruction for state flow control.

Thankyou

Depending on if/where you add the 'breaks', the switch statement will force the flow through one statement, rather than a set of ifs that could all trigger actions.

You could also use an array to hold the states and have something like stateMachine.states[lastState] and get rid of the ifs/switch altogether

1 Like

If state1, state2 etc are functions (nobody knows because you did not provide a complete example), an array of function pointers is the way to go

Below code demonstrates

#define NUM_ELEMENTS(x) (sizeof(x) / sizeof(x[0]))

void state1();
void state2();

void (* stateFunctions [])() =
{
  state1,
  state2,
};

// last state; one based
uint16_t lastState = 2;


void setup()
{
  Serial.begin(57600);
  Serial.print("There are ");
  Serial.print(NUM_ELEMENTS(stateFunctions));
  Serial.println(" state functions");

  //stateFunctions[0]();
  //stateFunctions[1]();

  // hardening to prevent out of bounds
  if (lastState > NUM_ELEMENTS(stateFunctions))
  {
    Serial.println("lastState refers to a non-existing state function");
    for (;;);
  }
  Serial.print("Executing last state (");
  Serial.print(lastState);
  Serial.println(")");

  // hardening to prevent executing a non-specified function
  if (stateFunctions[lastState - 1] == NULL)
  {
    Serial.println("lastState has no function associated with it");
    for (;;);
  }

  stateFunctions[lastState - 1]();

}

void loop()
{
  // put your main code here, to run repeatedly:

}


void state1()
{
  Serial.println("in state1");
}

void state2()
{
  Serial.println("in state2");
}

In your case, it will probably be something like below (without all the hardening)

stateMachine.(stateFunctions[lastState-1]);

The string of if statements isn’t really that bad, and it’s easier to debug than a table-driven approach.

Hi, I can ask what is this strange (for me) syntax ?
x = stateMachine.(state1);

If statemachine() is a function don't understand the point, if is an object, usally you have a method name after point.

yeah, as @sterretje said

If you define things right you could use
stateMachine(LastState);
or
stateMachine(States[LastState]);

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.