Estructura de un proceso secuencial / Structure of a sequencial process

Hola colegas espero estén bien, hoy me dirijo a ustedes con la finalidad de pedirle su opinión respecto a un nuevo proyecto que estoy emprendiendo y me gustaría saber su opinión sobre la estructura del sketch.

El proyecto se basa en un conjunto de etapas secuenciales ( se cumple la etapa 1, luego pasa la etapa 2, etc.) las cuales necesito accionar de manera individual o todas en secuencia, dependiendo del caso. La pregunta concretamente sería sobre como recomiendan que sea la estructura de dicho sketch para que sea más óptimo y facil de editar de ser necesario? Muchas gracias y espero sus valiosos apartes!

Funciones de uso.

``````const int etapas = 3;
void loop () {
static int etapa=1;

etapa=min(etapa,etapas);

switch(etapa) {
case 1:
procesarEtapa1();
etapa++;
break;

case 2:
procesarEtapa2();
etapa ++;
break;
…
}
}``````
2 Likes

Please write in English or post in the section of Your desired language.

1 Like

I'm very sorry I didn't noticed. What I meant is that I need to do a sequencial process which consist on several stages, they will go in order (first stage, then the second one, and so on and so forth) or separated, it has to work both ways. My question is rewarding the best way to structure such sketch in order to make it easy to modify and work well. Thank you in advance.

Google "Finite State Machine". You've basically given the definition so precisely that it almost sounds like I am answering a homework problem.

1 Like

if I wish to represent it on sort of a diagram I'd rather use it on GRAFCET which I already did, but thanks for your concern !

I am not talking about a diagram. A finite state machine is a programming pattern. Google it and read more than just the pictures and you will find your answer.

1 Like

@mancera's example in #3 is a common pattern for executing code sequentially. To go both ways, you might add some conditions at the end of each case to specifiy the transitions between states:

``````  switch(etapa) {
case 1:
procesarEtapa1();
if (forward){
etapa++;
} else {
etapa = 99;
}
break;
case 2:
...
``````
1 Like

Use functions.

``````unsigned long timer, interval = 1000;
int counter = 0; // a counter

void setup() {
Serial.begin(115200);
}

void loop() {
if (millis() - timer > interval) { // program running longer than "interval"
timer = millis(); // store new "time zero"

if (counter > 3) counter = 0; // check loop counter for overflow

switch (counter) { // use counter to select the function
case (0): { // if counter is 0
function_0(); // call this function
break; // exit this case
}
case (1): {
function_1();
break;
}
case (2): {
function_2();
break;
}
case (3): {
function_3();
break;
}
}
counter++; // increment function counter
}
}

void function_0() {
Serial.print("f0 ");
}
void function_1() {
Serial.print("f1 ");
}
void function_2() {
Serial.print("f2 ");
}
void function_3() {
Serial.println("f3");
}
``````

Hello andresgg

What is the task of the sketch in real life?

The zoo of the ‘Finite State Machine’ offers a lot of suitable solutions.
Starting with a traffic light control up to a microkernel.

El foro tiene una sección dedicada para aquellos que prefieren escribir en español. Tu tema ha sido movido allí.

Hello mate, is it a process for a machine, it has to activate motors and valves, but the process itself is kinda complex that is why I left it like "stages". But in sums it have to have a manual/automatic switch, as well as start, stop and emergency stop. In manual the stages will execute individually, and in automatic they will execute sequencially.

This is very close to what I am looking for mate

Then I want to think you can take it from here. Feel free to share your code if you have any further problems or questions.

En #12 @sterretje avisó que el tema se movió al foro en español.
Si no es mucho pedir, ¿Pueden escribir en español, por favor?
Gracias.

@andresgg Si tienes preguntas o problemas con tu código publícalo aquí para ayudarte a sacarlo adelante

1 Like

Si, muchas gracias, definitivamente mi punto de partida será el switch con la declaración de funciones, muchas gracias y las dudas y avances las ire compartiendo por este medio !