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?