PROBLEM USING SWICTH (state)

Hello
I have a problem in creating a control system using Arduino programming.

I use the SWITCH (state) command for a repetition process as follows:

SWITCH (state)

case A:
if (condition 1);
state B

else if (condition 2);
state C

else if (condition 3);
state D

case B:
contains processes with many commands involving delay timers;
state A;

case C:
contains processes with many commands involving delay timers;
state A;

case D:
command to stop all process B or process C;
state A;

The problem is that if at the beginning we give the command for condition 1 or condition 2 and during the process condition 1 or condition 2 is running then we give the command condition 3 then the program will not carry it out and will still carry out condition 1 or condition 2.

Even though each case has been returned to state A.

The question is how to ensure that the condition 3 command can be executed when the program is executing condition 1 or condition 2.

Thank You.

Please show your real rather than pseudo code, because the code below is non-sense:

Use the flag variable. Set the flag when condition 1 or condition 2 are meet. Test the flag and execute command 3 only if flag is set.

why don't you check for conditions 1 and 2 while in states B & C?

did you add also the break ?

SWITCH (state)

case A:
  if (condition 1) state B
  else if (condition 2) state C
  else if (condition 3) state D
  break;

case B:
  contains processes with many commands involving delay timers;
  state A;
  break;

case C:
  contains processes with many commands involving delay timers;
  state A;
  break;

case D:
  command to stop all process B or process C;
  state A;
  break;

mind the semicolon after the conditions in your if and don't use local variables in the cases unless you add a compound statement around them with {....}

Was given a break but still failed

post the real code then... we can't make guesses on pseudo code

You seem to have not yet understood how switch-case
and
switch-case-break; works

program starts in state "A"

state "A"

the one and only thing that state "A" is doing
it checks if condition_1 is fullfilled
when condition_1 IS fullfilled change to state "B"

state "B"

state "B" does the B-processes (with many commands involving delaytimer)
and
state "B" checks if condition_2 is fullfilled
when condition_2 IS fullfilled change to state "C"

state "C"

state "C" does the C-processes (with many commands involving delaytimer)
and
state "C" checks if condition_3 is fullfilled
when condition_3 IS fullfilled change to state "D"

this makes no sense:
state "D" stop the processes that were executed in state "B"
and
stop the processes that were executed in state "C"

You should either post the REAL code or

As there might be a misconception about how the code works
You should write a normal worded
do you understand normal worded means
avoid each and every programming-term and avoid each and every C++-language element

and only use normal words to describe the wanted functionality

best regards Stefan

I too had problems with switch and if block. At that moment I solved choosing a different path... hehe! no time to go that way...

without details your message seems useless

Below I provide code written in normally worded.
Please check the AUTO case, where the last ELSE IF was not implemented by the program when I pressed the STOP button

SWITCH (state)

case AUTO:
if relay_auto active,
state AA.

Else if PB_auto is pressed,
-activate relay_auto
-print lcd (0,1) "AUTO"
-state AA.

Else if PB_stop_auto is pressed,
-deactivate relay_forward,
-deactivate relay_reverse,
-activate relay_forward
-print lcd (12,1) "MoveUp "
-state STOP

case STOP:
if PB_ls1 is pressed
-deactivate relay_forward
-print lcd (12,1) "Top "
-deactivate relay_bvo
-activate relay_bvc
-print lcd (0,4) "BV:C"
-deactivate relay_auto
-state AUTO

case AA:
-deactivate relay_bvc
-activate relay_bvo
-print lcd (0,4) "BV:O"
-delay(2000) //open

  • activate relay_reverse
    -print lcd (12,1)"MoveDown"
  • delay (7000) //down
  • deactivate relay_reverse
    -print lcd (12,1) "Bottom "
    -activate relay_filling
    -print lcd (12,1) "Filling "
  • delay (2000) //filling
    -deactivate_relay_filling
  • activate relay_forward
    -print lcd (12,1) "MoveUp "
  • deactivate relay_empty
  • state AB

case AB:
if relay_empty inactive
-state AB1
else if relay_empty active
-state AB2

case AB1: //naik2
if PB_ls2 is pressed
-deactivate relay_bvo
-activate relay_bvc
-print lcd (0,4) "BV:C"
-delay (2000) //bv close
-state AC

case AB2:
-if PB_ls2 is pressed
-deactivate relay_reverse
-activate relay_emptying
-print lcd (12,1) "Emptying"
-delay (2000) //emptying
-deactivate relay_emptying
-activate relay_forward
-print lcd (12,1) "MoveUp "
-state AC

case AC:
if relay_empty inactive
-state AC1
else if relay_empty active
-state AC2

case AC1:
if PB_ls1 is pressed
-deactivate relay_forward
-print lcd (12,1) "Top "
-state AD

case AC2:
if PB_ls1 is pressed
-deactivate relay_forward
-print lcd (12,1) "Top "
-deactivate relay_bvc
-activate relay_bvo
-print lcd (0,4) "BV:O"
-state AD

case AD:
if PB_ls3 is pressed
-activate relay_reverse
-print lcd (12,1) "MoveDown"
-activate realy_empty
-state AB

else if PB_ls4 is pressed
-state AUTO

that's totally useless.

As we say around here, our crystal ball is in the dishwasher...

1 Like

if that's the code

then the C++ norm states that the MCU won't even evaluate condition2 and condition3 if condition1 is true

if you want them to be still evaluated then do

if (condition1) state B
if (condition2) state C
if (condition3) state D

with the risk of course that if two conditions are true then the last state set wins

not code normally worded

normally worded

functionality

by

avoiding

each and every similarity to code

if you do not understand how to do that
the simpelst way is to post your

complete sketch

using this method

best regards Stefan

after simulated but not work or fail

Ok this turned into nonsense-amusement.

If you tripple-couple the flux-compensator into a for-loop while praying 42 - 42 - 42
everything will work

1 Like

mine as well :grinning:

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