// Define pin connections & motor's steps per revolution
const int dirPin = 6;
const int stepPin = 7;
const int stepsPerRevolution = 200;
void setup()
{
Serial.begin(9600);
// Declare pins as Outputs
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
Serial.println("start");
}
int speed = 1000;
void loop()
{
if (Serial.available() > 0)
{
int inByte = Serial.read();
switch (inByte)
{
case 'f': {
if (speed>0){
speed -= 100;
}
}
break;
case 's': {
if (speed<2000){
speed += 100;
}
}
break;
case 'd': {
digitalWrite(dirPin, LOW);
}
break;
case 'e': {
digitalWrite(dirPin, HIGH);
}
break;
}
}
for(int x = 0; x < stepsPerRevolution; x++)
{
digitalWrite(stepPin, HIGH);
delayMicroseconds(speed);
digitalWrite(stepPin, LOW);
delayMicroseconds(speed);
}
}