state machine function (two relay with two push buttons)

Hello,
Here is my project:
With an arduino nano 33 IOT, I would like to have a temperature reading, control the hydraulic pressure at the outlet of a pump and be able to control two relays with two push buttons.
all this info will be available for consultation with the blynk app.

I'm having a problem programming my push buttons and taking over. After several tries and research I saw that I must use the technique of "finite automaton" / "state machine function"

For me it's a little complicated this technique. I need your help.
You will find my code below. I was inspired by this video: Youtube

When I press my push button, the relay starts blinking very quickly. As soon as I release him, he doesn't stay in one position. What I would like is that if I press once and I release the relay closes and if I press and release a second time the relay opens. An on-off system.

Do you have a solution ?

int state_s1 = 0;
int state_prev_s1 = 0;
int pin_s1 = 2;
int val_s1=0;
unsigned long t_s1 = 0;
unsigned long t_0_s1 = 0;
unsigned long bounce_delay_s1=5;

const int RELAIS1=4;

boolean etatRelais1=1;


void setup() {
  // put your setup code here, to run once:
  pinMode(pin_s1,INPUT_PULLUP);
  pinMode(RELAIS1, OUTPUT);
  Serial.begin(115200);
}

void loop() {
  // put your main code here, to run repeatedly:
  SM_relaiA();
  if(state_s1 == 4){
    Serial.println("triggered!!!");
      etatRelais1=!etatRelais1; // inverse l'état du relais
   digitalWrite(RELAIS1,etatRelais1);

       
  }
  if (state_s1 != state_prev_s1){
  Serial.print("state= ");
  Serial.println(state_s1);
  }
}

 void SM_relaiA (){
  state_prev_s1 = state_s1;
  
  switch(state_s1){
    case 0 :
    state_s1 = 1;
    break;
    
    case 1 :
      val_s1 = digitalRead(pin_s1);

      if ( val_s1 == LOW) {state_s1 = 2;}
    break;
    
    case 2 :
      t_0_s1 = millis();
      state_s1 = 3;
    break;
    
    case 3 :
      val_s1 = digitalRead(pin_s1);
      t_s1 = millis();

      if (val_s1 == HIGH) {state_s1;}
      if (t_s1 - t_0_s1 > bounce_delay_s1){
        state_s1 = 4;
      }
    break;

    case 4 :
      state_s1 = 0;
    break;

    case 5 :
      val_s1 = digitalRead(pin_s1);
      if(val_s1 == HIGH)(state_s1 = 4);
    break;
  }

  
 }

Why not just keep track of how many button presses have happened. The first time, set a variable "relay_closed = true" and the second time set it equal to false. Act accordingly.

What’s this supposed to be doing?

if (val_s1 == HIGH) {state_s1;}

As far as I can see state 5 never gets set? Anywhere.
What is it for? Is it supposed to be set?

  1. Put comments in each of your states so you, and we, can tell what they are meant to be doing.
    Ideally things like: waiting for button to be pressed, waiting for debounce, waiting for button to be released, etc.

  2. This code in loop is probably wrong...

if(state_s1 == 4){

It constantly toggles the relay when in state 4.

If changing of the relay state is linked with changes of state of the state machine, then the code that performs the relay toggle should be INSIDE function SM_relaiA in one of the switch cases, not outside it.

What's this supposed to be doing?

no idea, i saw it in the video.
I'm a beginner

@gui21540, rather than use generic names like state_s1 use meaningful names such as buttonState (or whatever is appropriate) and your code almost becomes self-documenting and a lot easier to follow when you come back to it after a 3 week break.

...R

gui21540:
no idea, i saw it in the video.
I'm a beginner

What video?

Link to the video with a time stamp of where someone just writes a variable name (e.g. “state_s1”) as a statement on its own and expects it to do something.

If they’ve actually done that, and not explained what they were TRYING to do, I would suggest using a tutorial different video.