Running Two FSM's simultaneously

hi guys,

My gate project is almost done, it is running perfectly separated, but I want to combine the entrance and outgoing gate at the same time, since you have to wait before the other fsm is done.
How would the following look like when case 1 and case 4 run at the same time?

switch(pressCount) {
    case 1:                      // entrance gate
    if (buttonState == LOW){     // reading card
    digitalWrite(zoemerPin, HIGH); // beep on
    delay(1000);
    digitalWrite(zoemerPin, LOW); // beep off
    digitalWrite(groenPin, LOW); // green light on
    pressCount = 2;
    break;
         
  }else if (button4State == HIGH) {    // outgoing gate    
    pressCount = 4;
    break;
 
    case 2:
    if (button2State == HIGH) {  //check for push  entrance gate
    digitalWrite(groenPin, HIGH); // green light off
    digitalWrite(motorPin, LOW);   // turn
    delay(500);
    pressCount = 3;
    break; 
    
    case 3:
    if (button3State == HIGH) { // 45°check
    digitalWrite(motorPin, HIGH); // stop turning
    pressCount = 1;
    break;
     
    case 4:                    // outgoing gate
    if (button4State == HIGH); // check for push
    digitalWrite(remPin, LOW); // release brake
    digitalWrite(motor2Pin, LOW);   // turn
    delay(750);
    pressCount = 5;
    break; 
    
    case 5:
    if (button5State == HIGH) { // 45°check
    digitalWrite(motor2Pin, HIGH); // stop turning
    digitalWrite(remPin, HIGH); // apply brake
    delay(500);
      
      
    pressCount = 1;
      
    break;

  }
    if (buttonState == LOW){     // reading card

This does not read anything, and as you have not posted the full code I don't know if you read the state of the switch somewhere else.

Your title does not seem to relate to your question.

You can run lots of FSMs at the same time - see the demo several things at a time.

...R

This is the whole code;

const int kaartPin = 6; // cardreader
const int poortPin = 5; // push entrance gate
const int motorPin = 11; // motor relais
const int remPin = 4; // brake
const int zoemerPin = 2; // beep 
const int checkPin = 3; // 45° check entrance
const int poort2Pin = 7; // push outgoing gate
const int check2Pin = 8; // 45° check outgoing
const int motor2Pin = 12; // motor relais outgoing
const int groenPin = 13; // green light entrance

int buttonState = 0;
int button2State = 0;
int button3State = 0;
int button4State = 0;
int button5State = 0;


void setup() {
  Serial.begin(9600);
  digitalWrite(motorPin, HIGH);
  digitalWrite(motor2Pin, HIGH);
  digitalWrite(remPin, HIGH);
  digitalWrite(groenPin, HIGH);
  
  pinMode(zoemerPin, OUTPUT);
  pinMode(remPin, OUTPUT);  
  pinMode(motorPin, OUTPUT);
  pinMode(motor2Pin, OUTPUT);
  pinMode(groenPin, OUTPUT);
  pinMode(kaartPin, INPUT);
  pinMode(poortPin, INPUT);
  pinMode(poort2Pin, INPUT); 
  pinMode(checkPin, INPUT);
  pinMode(check2Pin, INPUT);
  
}
byte pressCount = 1;

void loop(){
  
  buttonState = digitalRead(kaartPin);
  button2State = digitalRead(poortPin);
  button3State = digitalRead(checkPin);
  button4State = digitalRead(poort2Pin);
  button5State = digitalRead(check2Pin);
  
  switch(pressCount) {
  case 1:                      // entrance gate
  if (buttonState == LOW){     // reading card
    digitalWrite(zoemerPin, HIGH); // beep
    delay(1000);
    digitalWrite(zoemerPin, LOW);
    digitalWrite(groenPin, LOW); // green light on
    pressCount = 2;
    break;
         
  }else if (button4State == HIGH) { // gate out      
    pressCount = 4;
    break;
 
    case 2:
    if (button2State == HIGH) {  //check for push  
    digitalWrite(groenPin, HIGH); // green light off
    digitalWrite(motorPin, LOW);   // turn
    delay(500);
    pressCount = 3;
    break; 
    
    case 3:
    if (button3State == HIGH) { // 45°check
    digitalWrite(motorPin, HIGH); // stop turning
    pressCount = 1;
    break;
     
    case 4:                    // outgoing gate
    if (button4State == HIGH); // check for push
    digitalWrite(remPin, LOW); // release brake
    digitalWrite(motor2Pin, LOW);   // turn
    delay(750);
    pressCount = 5;
    break; 
    
    case 5:
    if (button5State == HIGH) { // 45°check
    digitalWrite(motor2Pin, HIGH); // stop turning
    digitalWrite(remPin, HIGH); // apply brake
    delay(500);
      
      
    pressCount = 1;
      
    break;

  }
  }
  }
  }
  }
  }

The use of braces doesn't look right. For example:

    case 3:
    if (button3State == HIGH) { // 45°check
    digitalWrite(motorPin, HIGH); // stop turning
    pressCount = 1;
    break;

Shouldn't there be a closing brace before the break? I note that you've got it to compile by throwing in enough braces at the end, but I'm not convinced that that's what you need.

Not able to test in it in reallife today, but is this kinda right?

byte pressCount = 1;
byte press2Count = 1;

void loop(){
  
  buttonState = digitalRead(kaartPin);
  button2State = digitalRead(poortPin);
  button3State = digitalRead(checkPin);
  button4State = digitalRead(poort2Pin);
  button5State = digitalRead(check2Pin);

}
    void ingangState(){
      
    switch(pressCount) {
    case 1:                      // entrance gate
    if (buttonState == LOW){     // reading card
    digitalWrite(zoemerPin, HIGH); // beep
    delay(1000);
    digitalWrite(zoemerPin, LOW);
    digitalWrite(groenPin, LOW); // green light on
    pressCount = 2;
    break;
     
    case 2:
    if (button2State == HIGH) {  //check for push  
    digitalWrite(groenPin, HIGH); // green light off
    digitalWrite(motorPin, LOW);   // turn
    delay(500);
    pressCount = 3;
    break; 
    
    case 3:
    if (button3State == HIGH) { // 45°check
    digitalWrite(motorPin, HIGH); // stop turning
    pressCount = 1;
    break;
    }
    }
    }
    }
    }
    void uitgangState(){
      
    switch(press2Count) {
    case 1:                    // outgoing gate
    if (button4State == HIGH); // check for push
    digitalWrite(remPin, LOW); // release brake
    digitalWrite(motor2Pin, LOW);   // turn
    delay(750);
    press2Count = 2;
    break; 
    
    case 2:
    if (button5State == HIGH) { // 45°check
    digitalWrite(motor2Pin, HIGH); // stop turning
    digitalWrite(remPin, HIGH); // apply brake
    delay(500);
    press2Count = 1;
    break;

    }    
    }
    }

Not really. I'd expect this:

case 1:                      // entrance gate
    if (buttonState == LOW){     // reading card
    digitalWrite(zoemerPin, HIGH); // beep
    delay(1000);
    digitalWrite(zoemerPin, LOW);
    digitalWrite(groenPin, LOW); // green light on
    pressCount = 2;
    break;

to be this:

case 1:                      // entrance gate
    if (buttonState == LOW){     // reading card
    digitalWrite(zoemerPin, HIGH); // beep
    delay(1000);
    digitalWrite(zoemerPin, LOW);
    digitalWrite(groenPin, LOW); // green light on
    pressCount = 2;
    }
    break;

And the other clauses need the same treatment

It probably works, but generally, you would never expect to see a switch structured that way.

I mean; would these 2 loops run simultaneously? Or is something wrong?

}
    void ingangState(){
      
    switch(pressCount) {
    case 1:                      // entrance gate
    if (buttonState == LOW){     // reading card
    digitalWrite(zoemerPin, HIGH); // beep
    delay(1000);
    digitalWrite(zoemerPin, LOW);
    digitalWrite(groenPin, LOW); // green light on
    pressCount = 2;
    }
    break;
     
    case 2:
    if (button2State == HIGH) {  //check for push  
    digitalWrite(groenPin, HIGH); // green light off
    digitalWrite(motorPin, LOW);   // turn
    delay(500);
    pressCount = 3;
    }
    break; 
    
    case 3:
    if (button3State == HIGH) { // 45°check
    digitalWrite(motorPin, HIGH); // stop turning
    pressCount = 1;
    }
    break;
    }
    }

    
    void uitgangState(){
      
    switch(press2Count) {
    case 1:                    // outgoing gate
    if (button4State == HIGH); // check for push
    digitalWrite(remPin, LOW); // release brake
    digitalWrite(motor2Pin, LOW);   // turn
    delay(750);
    press2Count = 2;
    
    break; 
    
    case 2:
    if (button5State == HIGH) { // 45°check
    digitalWrite(motor2Pin, HIGH); // stop turning
    digitalWrite(remPin, HIGH); // apply brake
    delay(500);
    press2Count = 1;
    }
    break;
    }    
    }
  switch(pressCount) {
  case 1:                      // entrance gate
  if (buttonState == LOW){     // reading card
    digitalWrite(zoemerPin, HIGH); // beep
    delay(1000);
    digitalWrite(zoemerPin, LOW);
    digitalWrite(groenPin, LOW); // green light on
    pressCount = 2;
    break;
         
  }else if (button4State == HIGH) { // gate out      
    pressCount = 4;
    break;

This seems a very strange way to manage the logic of the system.

For a start pressCount seem a very meaningless name for a critical variable - nothing seems to be pressing anything. I think pressCount represents different states of the system so why not manage the system that way.

It is not obvious (to me, anyway) what the different pressCount values (or states) represent and they just seem to mixed up higgledy-piggledy with the buttonStates.

If this was my project I would write out in plain language the various steps that the system takes and then I would try to write code to reflect that.

I would also break the code into small functions with meaningful names so that the logic of the system was obvious just by reading through it.

...R

gunske:
hi guys,

My gate project is almost done, it is running perfectly separated, but I want to combine the entrance and outgoing gate at the same time, since you have to wait before the other fsm is done.
How would the following look like when case 1 and case 4 run at the same time?

You can't have two separate states in an FSM running at the same time. You will have to combine them into one state. To do that, you will have to remove the delay()s. Look up the 'Blink without delay' example in the IDE or the examples page of this site. Read, learn, understand and embrace the principle.

Henry_Best:
You can't have two separate states in an FSM running at the same time. You will have to combine them into one state.

I think it is more accurate to say that you need to overlap them.

I agree with the rest of what @Henry_Best says. That's why I linked to the demo several things at a time in an earlier post. It shows several overlapping states.

...R

Robin2:

Henry_Best:
You can't have two separate states in an FSM running at the same time. You will have to combine them into one state.

I think it is more accurate to say that you need to overlap them.

We appear to differ only on terminology. I would say that overlapping states are just one state with different parts operating at different times. But I won't argue the point. Your interpretation is just as valid as mine.

Henry_Best:
We appear to differ only on terminology. I would say that overlapping states are just one state with different parts operating at different times. But I won't argue the point. Your interpretation is just as valid as mine.

I'm just following up this out of curiosity - and perhaps to learn how to align my terminology (I am certainly not an expert and I don't want to confuse others).

What I had in mind by "overlapping" can be illustrated by two LEDS flashing at different and unrelated rates. To my mind they represent two separate state machines - not least because they could easily be separated into two different programs without any effect on either "machine".

Whereas I would consider it a single machine if the two LEDs were an integral part of a sequence such that the behaviour of one depended on or followed from the other. In that case neither could work properly if they were moved into two different programs.

...R

Robin2:

Henry_Best:
We appear to differ only on terminology. I would say that overlapping states are just one state with different parts operating at different times. But I won't argue the point. Your interpretation is just as valid as mine.

I'm just following up this out of curiosity - and perhaps to learn how to align my terminology (I am certainly not an expert and I don't want to confuse others).

Neither am I, but I see it slightly differently to you.

What I had in mind by "overlapping" can be illustrated by two LEDS flashing at different and unrelated rates. To my mind they represent two separate state machines - not least because they could easily be separated into two different programs without any effect on either "machine".

I see those as two sub-states running in one master state. But, as I said above, I don't want to argue semantics. We could be here all year and not reach a conclusion.

Henry_Best:
I see those as two sub-states running in one master state.

Thanks - that helps me to see where you are coming from.

...R