My code throwing up the 'command' was not declared in this scope.

So I am pretty new to coding with Arduino, and I created a program to run a bluetooth HC-05 CNC to specific points on a board. But, my code keeps throwing up an error. Here is my program...

#define EN 8

//Direction pin
#define X_DIR 5
#define Y_DIR 6
#define Z_DIR 7

//Step pin
#define X_STP 2
#define Y_STP 3
#define Z_STP 4

//DRV88252
int delayTime=500; //Delay between each pause (uS)
int stps=2000;// Steps to move

void step(boolean dir, byte dirPin, byte stepperPin, int steps)

{

digitalWrite(dirPin, dir);

delay(100);

for (int i = 0; i < steps; i++) {

digitalWrite(stepperPin, HIGH);

delayMicroseconds(delayTime);

digitalWrite(stepperPin, LOW);

delayMicroseconds(delayTime);

}

}

void setup()
{
pinMode(X_DIR, OUTPUT); pinMode(X_STP, OUTPUT);

pinMode(Y_DIR, OUTPUT); pinMode(Y_STP, OUTPUT);

pinMode(Z_DIR, OUTPUT); pinMode(Z_STP, OUTPUT);

pinMode(EN, OUTPUT);

digitalWrite(EN, LOW);

Serial.begin(9600); //Set the baud rate to your Bluetooth module.
}

void loop(){
while (Serial.available() > 0)
{
command = Serial.read();
Serial.println(command);
}
switch(command){
case '1':
step(false, X_DIR, X_STP, 2000);
step(true, Y_DIR, Y_STP, 0);
delay(10);
step(true,X_DIR,X_STP,3000);
step(true,Y_DIR, Y_STP, 0);
break;
case '2':
step(false, X_DIR, X_STP, 4000);
step(true, Y_DIR, Y_STP, 0);
delay(10);
step(true, X_DIR, X_STP, 4000);
step(true, Y_DIR, Y_STP, 0);
break;
case '3':
step(true, X_DIR, X_STP, 0);
step(false, Y_DIR, Y_STP, 2000);
delay(10);
step(true, X_DIR, X_STP, 0);
step(true, Y_DIR, Y_STP, 2000);
break;
case '4':
step(false, X_DIR, X_STP, 2000);
step(false, Y_DIR, Y_STP, 2000);
delay(10);
step(true, X_DIR, X_STP, 2000);
step(true, Y_DIR, Y_STP, 2000);
break;
case '5':
step(false, X_DIR, X_STP, 4000);
step(false, Y_DIR, Y_STP, 2000);
delay(10);
step(true, X_DIR, X_STP, 4000); //X2, Counterclockwise
step(true, Y_DIR, Y_STP, 2000);;
break;
}
}
}

The code keeps on throwing up the 'command' was not declared in this scope for, the line...

command = Serial.read();

Any tips that could help me solve this issue?

The error message didn't lie. You never declared the command variable in your sketch. You have to declare all your variables.

This is what a variable declaration looks like:

char command;

That line tells the compiler to create a variable named command of the char type.
You can declare and define a variable on the same line if you like:

char command = Serial.read();

Thanks a bunch!

Just changingcommand = Serial.read();
tochar command = Serial.read();
won't get rid of the error. it will just move it down to your switch statement which is in a different 'scope'. When you declare the 'command' variable inside the body of the while loop, it only exists inside that body. To fix both problems, declare "char command;" inside the loop() body, before the while. DON'T declare it in both places! If you put "char command;" before the while loop and "char command = Serial.read(); inside the while loop you will have two DIFFERENT variables named 'command': one that gets set in the while loop and one that the switch uses that never gets set. Your sketch will compile but it won't do what you want.

Speaking of what you want... The while loop is going to read and print every character in the buffer but only when the buffer is empty and the while loop exits does the switch statement get to act on the last character that was in the buffer. That doesn't seem like something you'd want.

Also, since the switch statement is outside the while loop it is going to act on 'command' even when it has not been set (like if the buffer is empty). That is probably not what you want since the 'command' variable is local and gets some random value each time loop() starts.

To act on each character as it arrives:

void loop(){
   char command;
   if (Serial.available() > 0)
  {
  command = Serial.read();
  Serial.println(command);
 
  // Put the switch statement INSIDE the 'if' so it only gets used when a character is read.
    switch(command){
    case '1':  
...