#define stepPin 2
#define dirPin 3
#define msi1 9
#define msi2 10
#define msi3 11
const int steps = 200;
enum State {
INIT,
MOVE_FORWARD,
MOVE_BACKWARD,
DELAY
};
State currentState = INIT;
unsigned long previousMillis = 0;
const long interval = 1000; // 1 second
void setup() {
// Sets the pins as Outputs
Serial.begin(115200);
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode(msi1,OUTPUT);
pinMode(msi2,OUTPUT);
pinMode(msi3,OUTPUT);
digitalWrite(msi1,HIGH);
digitalWrite(msi2,HIGH);
digitalWrite(msi3,LOW);
digitalWrite(dirPin, LOW);
digitalWrite(stepPin, LOW);
}
void loop() {
unsigned long currentMillis = millis();
switch (currentState) {
case INIT:
digitalWrite(dirPin, LOW); // Enables the motor to move in a particular direction
currentState = MOVE_BACKWARD;
break;
// In order to change the angle of rotation
// Microstepping 1/8 th == 0.225 degree per revolution
// if 200 steps/rev == 1.8 degree per revolution
// 0.225 * 44.4444 = 9.9999 degrees
case MOVE_FORWARD:
for(float x = 0; x < 44.44444; x++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(50); // Changing the time delay between steps changes the rotation speed
digitalWrite(stepPin, LOW);
delayMicroseconds(50);
if (44.00 == x){
Serial.println("1");
// Serial.println(0.225*x);
}
}
currentState = DELAY;
previousMillis = currentMillis; // reset the delay timer
break;
case DELAY:
if (currentMillis - previousMillis >= interval) {
currentState = MOVE_BACKWARD;
}
break;
case MOVE_BACKWARD:
// digitalWrite(dirPin, LOW); // Enables the motor to move in the opposite direction
for(float x = 0; x < 44.44444; x++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(50); // Changing the time delay between steps changes the rotation speed
digitalWrite(stepPin, LOW);
delayMicroseconds(50);
if (44.00 == x){
Serial.println("1");
}
}
currentState = DELAY;
previousMillis = currentMillis; // reset the delay timer
break;
}
}