const int dirPin = 8;
const int stepPin = 9;
int up = 10;
void setup()
{
Serial.begin(9600);
Serial.setTimeout(1000);
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
Serial.println("Enter the steps you want to take: ");
}
void loop()
{
if (Serial.available() > 0)
{
int up = Serial.parseInt(); // More reliable method to get integer from Serial
if (up > 0)
{
Serial.print("Moving ");
Serial.print(up);
Serial.println(" steps.");
stepper(up);
Serial.println("Enter the steps you want to take: ");
}
}
}
void stepper(int up)
{
digitalWrite(dirPin, LOW);
for (int x = 0; x < up; x++)
{
digitalWrite(stepPin, HIGH);
delayMicroseconds(10000);
digitalWrite(stepPin, LOW);
delayMicroseconds(10000);
}
delay(100);
digitalWrite(dirPin, HIGH);
for (int x = 0; x < up; x++)
{
digitalWrite(stepPin, HIGH);
delayMicroseconds(10000);
digitalWrite(stepPin, LOW);
delayMicroseconds(10000);
}
}