Control stepper with serial command

Hi

I made a program where I'd like to control my stepper with serial commands. When I send a 1 it should do 1 step in clockwise (CW) direction when I send a 2 it should do 1 step in counterclockwise (CCW) direction. To simulate this I use the serial monitor. To test my code I insert a 1 serveral times in the serial monitor and I noticed that 8/10 my motor did turn in CW. The other 2 times it turns in CCW. The same counts when I insert a 2 serveral times.

here is my code

    #include <Stepper.h>

    #define number_of_steps 48 // value of steps on your motor
    Stepper myStepper(number_of_steps,8,9,10,11); // init stepper library

    int received_data[0]; // allocate memory for data over serial

    void setup()
    {
    Serial.begin(9600); // init serial port
    myStepper.setSpeed(10);
    }

    void loop()
    {
    while(Serial.available() < 1) {} // wait until 1 byte is available on serial port

    for(int i=0;i<1;i++) {
    received_data[i] = Serial.read(); // read 1 byte
    Serial.println(received_data[i]);
    }
    
    
    if(received_data[0] == 49) // clockwise revolution
    {
    myStepper.step(2); // step in clockwise direction
    delay(5);
    Serial.print("DONE stepping in CW direction for ");
    Serial.print(1);
    Serial.print(" steps!"); // output status message
    }

    else if(received_data[0] == 50) // counter clockwise revolution
    {
    myStepper.step(-2); // step in counter clockwise direction
    delay(5);
    Serial.print("DONE stepping in CCW direction for ");
    Serial.print(1);
    Serial.print(" steps!"); // output status message  
    }
    else Serial.print("ERROR!"); // output error message
    }

I do not understand why it does sometimes something else while I insert the same command.

while(Serial.available() < 1) {} // wait until 1 byte is available on serial port

    for(int i=0;i<1;i++) {
    received_data[i] = Serial.read(); // read 1 byte
    Serial.println(received_data[i]);
}

This is all confused.

Serial.available() < 1 is the same as Serial.available == 0 which is hardly what you want?

What does for(int i=0;i<1;i++) do? Especially as you only want to read a single character.

...R

I have been discussing my code with someone else and he did also did point on this code. It is indeed unnessecary code which I changed in my new code. I did remove the 'for(int i=0;i<1;i++)' part and did change 'while(Serial.available() < 1) {}' by ' if(Serial.available() > 0)'.

    #include <Stepper.h>

    #define number_of_steps 48 // value of steps on your motor
    Stepper myStepper(number_of_steps,8,9,10,11); // init stepper library

    int received_data[0]; // allocate memory for data over serial

    void setup()
    {
    Serial.begin(9600); // init serial port
    }

    void loop()
    {
    if(Serial.available() > 0) // wait until 1 byte is available on serial port
    {
    received_data[0] = Serial.read(); // read 1 byte
    Serial.println(received_data[0]);
    
    if(received_data[0] == 49) // clockwise revolution
    {
    myStepper.step(1); // step in clockwise direction
    delay(5);
    Serial.print("DONE stepping in CW direction for ");
    Serial.print(1);
    Serial.print(" steps!"); // output status message
    }

    else if(received_data[0] == 50) // counter clockwise revolution
    {
    myStepper.step(-1); // step in counter clockwise direction
    delay(5);
    Serial.print("DONE stepping in CCW direction for ");
    Serial.print(1);
    Serial.print(" steps!"); // output status message  
    }
    else Serial.print("ERROR!"); // output error message
    }
    }

Still with these changes when I send a 1 = 49 in ASCII it doesn't always run CW loop, but sometimes it runs the CCW loop instead.

EDIT: I also did leave the ' if(Serial.available() > 0)' part out of the code so it constantly does something or gives the error message. And I'm facing the same problems here. How could I ever come in the loop from received date == 50 if I only send 49's?

int received_data[0]; // allocate memory for data over serial

This is an unusual statement, and I'm not sure what it actually does -- it allocates an array of length 0.

Try changing "received_data[0]" to "received_data" everywhere and see if that helps.

int received_data[0]

Yes. I suspect this is the problem. It's one of the GOTCHAs in C that you define arrays with the actual number of elements but you address them with the first element as [ 0]. So that line should be int received_data[1]

The likely affect of your error is that when you reference received_data[0] later in the program it is actually using the data that is in the next address location (whatever that may be). If you want to explore the phenomenon add another variable (say char testChar = 'a'; ) immediately after the array definition and see what happens when you try to print testChar.

Of course, as @jremington has said, it doesn't make a lot of sense to create an array with a single element.

...R