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;
}
}