'State' was not declared in this scope

Please help !
I have state machine programmed but the switch is not declared.
When i set it to zero. it didn't do anything.

enum State_enum {IDLE, FORWARD, BACK};
 
void motors_idle();
void motors_forward();
void motors_back();

int Sensor_Home = 10;
int Sensor_Button = 3;
int Distance = 0;
int Stepping = false;
int flag = 0;

void setup(){
  Serial.begin(9600);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  digitalWrite(8, HIGH);
  digitalWrite(9, LOW);
  pinMode(Sensor_Home, INPUT);
  pinMode(Sensor_Button,INPUT);
}
 
void loop()
{
  switch(state)
  {
    case IDLE:
      if(Sensor_Home == LOW){
        motors_idle();
        state = FORWARD;
      }
      else{
        state = BACK;
      }
      break;
         
    case FORWARD:
      if(Sensor_Button == LOW && Sensor_Home == LOW){
        motors_forward();
        state = BACK;
        }
      break;
   
    case BACK:
      if(Sensor_Home == HIGH){
        motors_back();
        state = IDLE;
      }
      break;
  }    
}
 
void motors_idle()
{
  //code for idle motor
  if (Sensor_Home == LOW)
  {
    digitalWrite(8,LOW);
  }
}
 
void motors_forward()
{
  //when SENSOR_PRESS trigger
  //motor move back til SENSOR_HOME pressed & motor forward until SENSOR_HOME unpressed
  //drive motor forward to X-distance & return (LOOP CYCLE)

  while (flag = 0)
  {
    
    if (Sensor_Button == LOW && Stepping == false) //SENSOR_PRESS triggered
    {
      Stepping = true;
    }
    while (Stepping == true)
    {
      //code for motor go to SENSOR_HOME
      //then before code foor motor forward x-distance and return
      digitalWrite(9, HIGH);
      delayMicroseconds(100);
      digitalWrite(9, LOW);
      delayMicroseconds(100);
      Distance = Distance + 1;   // record this step

      // Check to see if we are at the end of our move
      if (Distance == 9600) //Input Distance
      {
        // We are! Reverse direction (invert DIR signal)
        if (digitalRead(8) == HIGH)
        {
          digitalWrite(8, LOW);
        }
        else
        {
          digitalWrite(8, HIGH);
          flag = 1;
          break;
        }
        delay(500);
      }
    }
  }
}
 
void motors_back()
{
  while(flag == 0)
  {
    if (Sensor_Home == HIGH && Stepping == false)
    {
      Stepping == true;
    }
    while(Stepping == true)
    {
      digitalWrite(8, HIGH);
      digitalWrite(9, HIGH);
      delayMicroseconds(100);
      digitalWrite(9, LOW);
      delayMicroseconds(100);
      if (Sensor_Home == LOW) //LIMIT SWITCH ACTIVATED
      {
        // RUN MOTOR BACK TOWARD LIMIT SWITCH & PRESSED
        // REVERSE DIRECTION & MOTOR FORWARD UNTIL LIMIT SWITCH UNPRESSED & STOP
        digitalWrite(8,LOW);
        Stepping == false;
        flag = 1;
        break;
      }
    }
  }
}

int state = IDLE; at global scope?

Hi, welcome to the forum! Karma for using code tags! But two things you missed from "How to use the forum":

  • Tell us what you want to happen:
  • Place the whole error message. Really, it does contain more info :wink:

But to the problem, 'state' (not 'State', the IDE is case sensitive) is indeed not defined. You only try to use it but you never told the compiler to make the actual variable. You do so by placing the type in front of the variable name. You may also assign a value to it at that moment. So add:

State_enum state = IDLE;

At the top of the sketch where you declared other variables.

After you did that, note some other problems:

while (flag = 0)
//did you mean
  while (flag == 0)
  if (Sensor_Home == HIGH && Stepping == false)
    {
      Stepping == true;
    }
//did you mean:
    if (Sensor_Home == HIGH && Stepping == false)
    {
      Stepping = true;
    }
      Stepping == false;
        flag = 1;
        break;
//did you mean:
        Stepping = false;
        flag = 1;
        break; //NOTE: no need to break, you already set the flag false
pinMode(Sensor_Home, INPUT);
//...
      if(Sensor_Home == LOW){
//did you mean
      if(digitalRead(Sensor_Home) == LOW){

This is where good variable names come into play. Probably would have noticed this if you would have called it 'Sensor_Home_Pin' (or 'SensorHomePin'). Note that you made this mistake multiple times.

And some tips:

  • Make a variable for all pin numbers as well, makes the code easier to understand.
digitalWrite(8, LOW);
//vs
digitalWrite(MotorDirectionPin, LOW);
  • Default way of writing variable names is likeThisOnArduino. Although nothing wrong with a different style but try to stick to that style.
   if(Sensor_Home == HIGH){
        motors_back();

Sensor_Home has the value 10.
It is never, ever going to equal HIGH, which has the value 1.

You're missing a digitalRead.

It's not clear from the code what you want your sketch to do.

It looks like maybe you want to home the axis by first going backward until the HOME switch closes and then go forward until the HOME switch opens. Then you want to go forward some amount? What does the button do?

I think that, instead of having the motors_forward() function doing the homing, you should add more states to do the homing. There shouldn't be a 'flag' and you shouldn't need to read the output pins to decide what to do. In each state you are only doing one thing.
HOME: Go backward one step. If the HOME switch is closed, go to state CLEAR_HOME.
CLEAR_HOME: Go forward one step. If the HOME switch is open, go to state FORWARD
FORWARD: Go forward one step. Increment Distance. If Distance matches the goal, go to state HOME

If you want to add a delay, put in a state for it. For example, if you want to pause half a second after reaching the goal and before going back to home:
FORWARD: Go forward one step. Increment Distance. If Distance matches the goal, set Timer to millis() and go to state PAUSE.
PAUSE: If millis() - Timer >= 500, got to state HOME