'motorDir' was not declared in this scope

I am getting the error stated in the title. I have tried two different ways of writing the code where I assign the value to the defined int motorDir and still continue to get this error. I followed the same syntax as the website's example for the first instance and a different method of my own in instance 2 and I get the same error for each instance.

The first instance is where the ints for motorDir and motorIDout are defined in "ManMotorMove();" and is currently uncommented. The preferable method would be to define them in "recData();" (And currently commented out) because in the final code set there will be a serial read and I would like to have a section dedicated entirely to reading incoming data and then setting the appropriate voltages to the output pins prior to the rest of the code running. Currently the numerical control that is moved via the serial is commented out/not included in this code set as it is not relevant to the current error so please disregard those parts of the code. Again, either way gives me the same error.

Code:

// Other constants
const byte numChars = 32;
char receivedChars[numChars];   // an array to store the received data
boolean userinput = false;
int dataNumber = 0;             // new for this version
float recerveddec;
const int maxstepsize = 32700;
const int minstepsize = 1;
const int sethalfwavelength = 1000; // Set half wavelength to determine speed. Range of available speeds without abnormal vibrations: [1000,3000]. In units of (micro-secnds [10^-6])

// Connections to A4988
const int motor1dirPin = 2;  // Motor 1 direction
const int motor1stepPin = 3; // Motor 1 movement
const int motor2dirPin = 4;  // Motor 2 direction
const int motor2stepPin = 5; // Motor 2 movement

// Connections to User Interface
const int buttonPress = 10; //Pin for manual control
const int motorIDin = 11; // ID pin for motors
const int controlDir = 12; // Rotation direction definition pin
const int emergencystop = 13;  // Emergency stop pin

// const int motorsetrpm = A0; // Set RPM for Pk266-01A and Pk266-01B [RPM/Hz range: 0 to 4500/750] Recomends ~150 RPM or 25 Hz

void setup() {
  
  // Designate Output Pins
  pinMode(motor1stepPin,OUTPUT);
  pinMode(motor1dirPin,OUTPUT);
  pinMode(motor2stepPin,OUTPUT);
  pinMode(motor2dirPin,OUTPUT);
  // Designate Input Pins
  pinMode(buttonPress,INPUT_PULLUP);
  pinMode(motorIDin,INPUT_PULLUP);
  pinMode(controlDir,INPUT_PULLUP);
  pinMode(emergencystop,INPUT_PULLUP);
  
  {
    // Initialize serial and wait for port to open:
    Serial.begin(9600);
    Serial.println("<Arduino is ready>");
  }
  delay(5000);
}


// Stepper Motor Program Begins and Loops
void loop()
{
   recData();
   displayData();
   //NumMoveMotor();
   ManMoveMotor();
}

void recData()
{
   static byte ndx = 0;
   char endMarker = '\n';
   char c;
   if (Serial.available() > 0)   // If there is a serial available, read it
   {
      // read the incoming byte:
      c = Serial.read();      
      if (c != endMarker)
      {
         receivedChars[ndx] = c;
         ndx++;
         if (ndx >= numChars)
         {
            ndx = numChars - 1;
         }
      }
      else
      {
         receivedChars[ndx] = '\0'; // terminate the string
         Serial.println(receivedChars);
         ndx = 0;
         userinput = true;
      }
   }
/*
   if (digitalRead(motorIDin) == LOW) {
    int motorIDout = 1;
   }
   else {
    int motorIDout = 2;
   }
   if (digitalRead(controlDir) == LOW) {
    int motorDir = 0;
   }
   else {
    int motorDir = 1;
   }
*/
}

void displayData()
{
   if (userinput == true)
   {
      recerveddec = atoi(receivedChars);
   }
}

// Stepper Motor Program Begins and Loops
void loop()
{
   ManMoveMotor();
}

void ManMoveMotor() {
  if (digitalRead(motorIDin) == LOW) {
    int motorIDout = 1;
  }
  else {
    int motorIDout = 2;
  }
  if (digitalRead(controlDir) == LOW) {
    int motorDir = 0;
  }
  else {
    int motorDir = 1;
  }
  
  switch (motorDir) {
    case 0: //Clockwise rotation case
      digitalWrite(motor1dirPin,LOW);
      digitalWrite(motor2dirPin,LOW);
      switch (motorIDout) {
        case 1: //To control motor 1
          while (digitalRead(buttonPress) == LOW) { // If button is being pressed, execute loop
          digitalWrite(motor1stepPin,HIGH);
          delayMicroseconds(sethalfwavelength);
          digitalWrite(motor1stepPin,LOW);
          delayMicroseconds(sethalfwavelength);
          }
          break; //Break for case 1 "motor 1 control"
        case 2: //To control motor 2
          while (digitalRead(buttonPress) == LOW) { // If button is being pressed, execute loop
          digitalWrite(motor1stepPin,HIGH);
          delayMicroseconds(sethalfwavelength);
          digitalWrite(motor1stepPin,LOW);
          delayMicroseconds(sethalfwavelength);
          }
          break; //Break for case 2 "motor 2 control"
      }
      delay(1);
      break; //Break for case 0 "Clockwise rotation"
    case 1: //Counter-Clockwise rotation case
      digitalWrite(motor1dirPin,LOW);
      digitalWrite(motor2dirPin,LOW);
      switch (motorIDout) {
        case 1: //To control motor 1
          while (digitalRead(buttonPress) == LOW) { // If button is being pressed, execute loop
          digitalWrite(motor1stepPin,HIGH);
          delayMicroseconds(sethalfwavelength);
          digitalWrite(motor1stepPin,LOW);
          delayMicroseconds(sethalfwavelength);
          }
          break; //Break for case 1 "motor 1 control"
        case 2: //To control motor 2
          while (digitalRead(buttonPress) == LOW) { // If button is being pressed, execute loop
          digitalWrite(motor1stepPin,HIGH);
          delayMicroseconds(sethalfwavelength);
          digitalWrite(motor1stepPin,LOW);
          delayMicroseconds(sethalfwavelength);
          }
          break; //Break for case 2 "motor 2 control"
      }
      delay(1);
      break; //Break for case 1 "Counter-Clockwise rotation"
  }
  delay(1);
}

Perhaps I'm missing something but if you need to set variable values which are used in more than one different function surely it is simplest to define them at the top of the program in global scope (i.e. not inside any function)?

Steve

I followed the same syntax as the website's example

Link, please.

@slipstick The int definitions will change based on the voltage inputs so they cannot be defined globally, outside the functions.

@TheMemberFormerlyKnownAsAWOL https://www.arduino.cc/en/Tutorial/switchCase
There is a difference between my code and the website in terms of that I am using a double switch case while the website just uses one but I don't think that is what is causing the error.

@Delta_G I see what you mean and I wasn't aware that this was how it worked as I am new to C++/Arduino coding. How can I make it so that these variables continue to be defined when the switch case is implemented? As I said to @slapstick I cannot define them globally since they need to be dynamic. If I move the closing brackets below the switch case then none of the switch case statements will run half the time due to the condition.

tkeaton:
@slipstick The int definitions will change based on the voltage inputs so they cannot be defined globally, outside the functions.

If you really mean the DEFINITIONS will change rather than just the VALUES in those variables then they aren't the same variables. Just give them different names.

But if it is just the values that can change then that's what global scope is for...so that any function can set/change them.

Steve

@slipstick Ok I think I now understand where I was confused. I thought that by defining a variable globally it automatically meant that the definition and value assigned are both fixed so they cannot be altered (aka "read only"). I didn't realize it could also only established the existence of a variable that is alterable. I was only using "const int" up to this time which is "read only" and didn't realize if I just use "int" then the variable's existence becomes acknowledged and alterable. Thank you for the reminder/realization.