Stepper Motor not moving accurately

Hello everyone,
I was working on one project in which I am using Stepper Motors(Nema 17). I got one program from this forum only which I modified and is attached with this post. I am using serial monitor to give input to the board.
Problem is that when i give multiple commands at once, example if I type ‘f’ 4 times at once and then press enter then the motor accuracy is very bad (i.e. it should have rotated full 360 degrees but it moved around 300 degrees only). On the other hand if i type ‘f’ one time and press enter, then the accuracy is very high (i.e. it rotates almost 90 degrees) and if I do this 4 times then the final accuracy for full 360 degree rotation is high.
I really have no clue what is wrong. I don’t want to make the program longer with more cases(like providing cases with 180, 360 degrees rotation).
Thanks in advance.

Stepper_Motor_Switch_Case_Full.ino (12.2 KB)

You should only call Serial.begin() once, during setup(), instead of after receiving each character. Also, since the Serial I/O uses pins 0 and 1 on most boards, and you’re also trying to use pin1 for directionPin1, it’s possible that the serial I/O is causing the direction to flip while the stepper is moving, so that not all the steps you execute are in the direction you intended. If you’re using the hardware Serial port, you should not use the serial Rx and Tx pins for anything else.

If you look closely in my program, Serial.begin() is not called every time after receiving each character (it is outside the for loop).
and to answer your second query, i just wrote that pins 1 and 2 are used, but i am actually using 5 motors which are connected to I/O pins 3 through 12. So i don't think that should be a problem.
Thing that i am concerned of is that there is accuracy difference of single 90 degree rotation and 360 degree rotation(when 4 output signals are sent to the board at a time).

palashrc0945:
If you look closely in my program, Serial.begin() is not called every time after receiving each character (it is outside the for loop).
and to answer your second query, i just wrote that pins 1 and 2 are used, but i am actually using 5 motors which are connected to I/O pins 3 through 12. So i don't think that should be a problem.

I disagree on both counts.

You ARE calling Serial.begin() after each received character. Look at lines 37, 64 etc.

You ARE using directionPin1 in the code that handles the 'f' and 'F' characters that you specified in your original post, and you ARE doing serial output in those cases which will cause the hardware Serial port to change the output state of the Tx pin (1). If you are using pin1 as a motor direction pin as implied by your code then this is wrong and will not work correctly, leading to exactly the sort of symptoms you have reported.

Also note that you only need to call pinMode() once per pin, so it is pointless (but harmless) to call this repeatedly within loop().

I see it now, sorry for that :cold_sweat: .
I stated my question in such a way that, anyone could understand it by just looking at the first case.
Actually i used the 5th motor for my testing, which was connected to pin 9 and pin 10. I didn't connect anything to pin 1 and pin 2. I connected this motor to pin 9 and pin 10 and sent the alphabet 'u'(using the serial monitor), motor rotated 90 degrees, then i did the same 4 times and motor rotated 360 degrees. now just for testing purposes I sent 'uuuu' and pressed enter. After that the problem came out. The serial monitor showed that the 'u case is called 4 times, but the actual rotation of the shaft was about 300 degrees only (it was in between 270 and 360 degree).
Now I have told you everything.

I wonder if when you send 4 u's that your program runs too quickly and the second or third u is called before the stepper has finished moving. To test this add an extra delay() at the end of loop();

You should really replace all the delays with the technique in the Blink Without Delay example sketch.

You don't need to repeat Serial.begin(). That is only needed once in setup().

Why, Why, WHY ....

... have you repeated what is virtually the same code in each case statement

Put that code in a function and call it from the case statements with an appropriate parameter.

Repeating yards (metres) of code like that is an invitation for typing errors and it's a nightmare when something needs to be changed.

...R

I suggest you fix the problems that have already been pointed out, eliminate the code that is not involved in demonstrating the problem, confirm that your updated code does still demonstrate the problem and then post the code here.

Sorry for replying this late but i completed my project and have done a lot of changes in the mean time. And now the motors are working just fine. It was the problem of the power supply which i had. After i have changed it, it's working just perfectly.

I want to thank everyone for your help and your support.

Really happy for my project. Here is the link for the video.