Greetings for all,
First let me wish you a good health, for you and your families. I'm Dokleat, from Kosovo, I'm in the beginning of programming. I need your help, so I decided to address you through this post.
I have a Nema 17 stepper motor, and an Arduino Nano plate. I want to do a program that first accepts the commander's order to move in front or back, and then again receives from the user the number of rotations that the motor stepper should do.
I have created a script but I have a problem with its execution. Specifically, during the execution of the second order, Arduino does not accept the number of rotations provided by the user.
This is the source code of the program:
const int stepPin = 3;
const int dirPin = 4;
int x = 0;
long i = 0;
long p = 0;
char serialData = 0;
void setup()
{
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
if(Serial.available() > 0)
{
serialData = Serial.read();
Serial.print(serialData);
if(serialData == '1')
{
i = Serial.parseInt();
digitalWrite(dirPin, HIGH);
for(x = 0; x <=i*200; x++)
{
digitalWrite(stepPin, HIGH);
delayMicroseconds(1500);
digitalWrite(stepPin, LOW);
delayMicroseconds(1500);
}
}else if(serialData == '0')
{
p = Serial.parseInt();
digitalWrite(dirPin, LOW);
for(x=0; x <= p*200; x++)
{
digitalWrite(stepPin, HIGH);
delayMicroseconds(1500);
digitalWrite(stepPin, LOW);
delayMicroseconds(1500);
}
}
}
}
Can you help me, how to solve this problem?
Best regards,
Dokleat