Switch case function not working

Good morning everyone,

I'm getting crazy with some issues about the code on my Arduino Uno. I'm a newbie and to me, it looks fine, I don't see any problem, but now I explain everything.
I need to manage a 24V motor through some 5V relays, just to move a platform forward/ backward. I have also 2 micros, one for the "maximum forward position reached" and one for the "maximum backward position reached".
The cycle starts when DI3 is HIGH and to manage the cycle I choose to use the logic by step, using the "switch...case" function.
Basically, when I perform a case, at the end of each one I increase the step.
My problem is that when I force the DI3 making a bridge with the Arduino 5V, nothing happens. Through the monitoring the value of STEP is always 0.

Am I doing something wrong?

Thank you.


#define ON LOW
#define OFF HIGH

// I/O
int Start = 3;
int MaxFwd = 5;
int MaxBkw = 6;
int MoveFwd = 8;
int MoveBkw = 9;

// VARIABLES
const int Step_0 = 0;
const int Step_10 = 10;
const int Step_20 = 20;
const int Step_30 = 30;
const int Step_999 = 999;

int STEP = 0;


void setup()  //DEFINITION ---------------------------------------------------------------------------------------------------------------------------

{
  //start serial connection
  Serial.begin(9600);

  //configure pins as inputs and outputs
  pinMode(Start, INPUT);
  pinMode(MaxFwd, INPUT);
  pinMode(MaxBkw, INPUT);
  pinMode(MoveFwd, OUTPUT);
  pinMode(MoveBkw, OUTPUT);

}

void loop() // CYCLE -----------------------------------------------------------------------------------------------------------------------------------
{

  Serial.print(STEP, DEC); //To show actual value of the STEP

  delay(1000); //Only for test checking

  switch (STEP)
  {
    case Step_0: // Start cycle --------------------------------------------------------------------------------------

      if (digitalRead(Start) == HIGH) int STEP = 10; //Increase step
      break;

    case Step_10: // Forward --------------------------------------------------------------------------------------------

      digitalWrite(MoveFwd, ON); //Cmd forward ON

      if (digitalRead(MaxFwd) == HIGH)  //Max forward reached
      {
        digitalWrite(MoveFwd, OFF); //Cmd forward OFF
        int STEP = 20; //Increase step
      }
      break;

    case Step_20: // Delay ---------------------------------------------------------------------------------------------
      {
        delay(5000);  //Delay move
        int STEP = 30; //Increase step
      }
      break;

    case Step_30: // Backward ------------------------------------------------------------------------------------------

      digitalWrite(MoveBkw, ON); //Cmd back ON

      if (digitalRead(MaxBkw) == HIGH) //Max backward reached
      {
        digitalWrite(MoveBkw, OFF); //Cmd back OFF
        int STEP = 999; //Increase step
      }
      break;

    case Step_999: // End cycle ---------------------------------------------------------------------------------------------
      {
        delay(500);  //Delay reset
        int STEP = 0; //Reset step
      }
      break;

    default:
      int STEP = 0; //Reset step
      break;
  }

}

Problem is you keep re-declaring STEP.

Remove int from these statements and it will work... just declare once at the start of the program.

1 Like

Fortunately, there are people like you in this world.
You saved my mental health! It is working perfectly fine!!

Thank you very much!

1 Like

So you get stressed when you copy paste code without understanding how it works and then it doesn’t work for some reason?

Yes exactly.
As I said I'm a newbie, this is the first time I program with Arduino and I wrote the sketch just yesterday.
Unfortunately I do not have yet your experience to find mistakes in an instant :slightly_smiling_face:

I was born with experience in C++, wasn’t everyone?

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