-2

I need to achieve that in every loop my Arduino do other thing. Say in first loop do one thing, in second do second thing and in third loop do third thing. And again. And while doing one thing, other things are dead.

Not sure what approach to use. This is just my 50 cents:

for(int x=0; x<2; x++)
  {
  switch (x) {
    case 0:
      //do something
      break;
    case 1:   
      //do something
      break;
    case 2:   
      //do something
      break;
  }
  delay(1);
}

I tried this from above but it stuck at case 0

Any point or link to explanation would be nice.

closed as off-topic by per1234, Juraj, jfpoilpret, Chris Stratton, MatsK Mar 5 '18 at 10:38

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question does not appear to be about Arduino, within the scope defined in the help center." – per1234, Juraj, MatsK
If this question can be reworded to fit the rules in the help center, please edit the question.

  • how do you know, "it is stuck in case 0"? – Juraj Mar 4 '18 at 18:22
  • It don't do case 1. And it should. – dekip Mar 4 '18 at 18:27
  • The question was how you know that. This question will probably not be answerable until you post actual code which causes the problem. – Chris Stratton Mar 4 '18 at 18:50
  • In your code the for loop is wrong: if you want to reach case 2, you need x <= 2; or x < 3 in the for. Then this code should in general work. You can also make a global variabe for the "step" you're currently in only write the switch part in the loop followed by step++; – Maximilian Gerhardt Mar 4 '18 at 19:05
  • 1
    Either the case 0 code is stuck in a loop or it exits abnormally. – AJD Mar 4 '18 at 19:13
2

Your implementation is in principle correct. However, tradionally, you don't incrementally loop over all states from 0 to end using a for loop. The implementation is more flexible if it allows the state to go to any other state if it needs to. This would be a normal finite state machine (FSM).

#include <Arduino.h>
/* How many states are possible? */
#define MAX_STATES 2
/* global state variable */
int currentState = 0;

/* prototypes */
void Action1();
void Action2();

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

void AdvanceStateByOne() {
    currentState = (currentState + 1) % MAX_STATES;
}

void loop() {
    //Execute the action dictated by the current state
    switch(currentState) {
        case 0:
            Action1();
            break;
        case 1:
            Action2();
            break;
        default: //Catch for un-implemented states
            break;
    }
    //Go to next state, incrementally.
    //This could also be modified in one of the functions above
    //Loop back to zero state after MAX_STATES using modulo operator
    AdvanceStateByOne();

    //for testing, introduce some delay
    delay(1000);
}

void Action1() {
    Serial.println("Action 1");
}

void Action2() {
    Serial.println("Action 2");
}

This prints out "Action 1" and "Action 2" in a loop forever.

Now you could take this further and instead of using pure numbers for your states, you can use an enum representation of those to make the code clearer:

enum state_t {
    STATE_DO_ACTION_1 = 0,
    STATE_DO_ACTION_2,
    STATES_END
} ;

And then act on those accordingly. You can also use the C++ feature of a enum class to give you a nice notation like State::SomeState1.

-1
for(int x=0; x<2; x++)
  {
  switch (x) {
    case 0:
      //do something
      break;
    case 1:   
      //do something
      break;
    case 2:   
      //do something
      break;
  }
  delay(1);
}

You will never do case 2, that's for sure, as you are incrementing to less than 2.

Not the answer you're looking for? Browse other questions tagged or ask your own question.