I am trying to run the stepper motor for 5 sec then, wait for 2 secs, and reverse and run the motor again for 5 secs. But I am getting confused in the time difference and the delay logic something is going wrong.
Here is the code:
#include <Stepper.h>
#define STEPS 2038 // the number of steps in one revolution of your motor (28BYJ-48)
#define MAX_SPEED 16 // can't go faster than this. Keep within range
#define ALARMPIN 13 // to show action
Stepper stepper(STEPS, 9, 11, 10 ,12);
int previousTime = 0;
long eventTimer = 5000; // Run for duration in ms.
int stepDir = STEPS;
void setup() {
// nothing to do
Serial.begin(9600);
pinMode(ALARMPIN, OUTPUT);
}
void loop() {
long currentTime = millis();
if( (currentTime - previousTime) < eventTimer) {
setMotorInMotion();
previousTime = currentTime;
} else {
delay(2000);
eventTimer = eventTimer + eventTimer;
stopMotorMotion();
if(stepDir == STEPS) {
stepDir = -STEPS;
} else {
stepDir = STEPS;
}
}
}
void setMotorInMotion() {
digitalWrite(ALARMPIN, HIGH);
stepper.setSpeed(MAX_SPEED);
stepper.step(stepDir);
Serial.println("Motor is running...");
}
void stopMotorMotion() {
digitalWrite(ALARMPIN, LOW);
stepper.setSpeed(0);
stepper.step(0);
Serial.println("Motor Stopped");
}
Can someone suggest what the issue is?