/* This example assumes a step/direction driver with Step on pin 9, Direction on pin 8
* And an input switch on pin 3. The switch is a switch to ground, with pin 3 pulled
* high with a pullup resistor. When the switch is turned on (closed, i.e. goes low)
* the the stepper motor steps at the rate specified (50 RPM in this code, with
* 1/8th microstepping of a 200 steps/rev motor)
* each 'step' from the code's perspective is 1/8th of the motor's full step,
* because we are assuming the use of the EasyDriver in 1/8th microstep mode.
* If you use the Big Easy Driver, it's default is 1/16 microstep, so adjust your expectations for motor motion accordingly
* https://www.schmalzhaus.com/EasyDriver/Examples/EasyDriverExamples.html - example 1.7
* 2= DOWN, 3= UP, 9= STEP, 8=DIR
*/
#define DISTANCE 1.0
#define RPMS 8.0
#define STEP_PIN 9 // Tells motor to move
#define DIRECTION_PIN 8 // Tells Board/ Motor Direction using HiGH (clockwise)/LOW (Anticlockwise)
#define UP_PIN 3 // Up Pin = Up Momentary Switch
#define Down_PIN 2 // Down Pin = Down Momentary Switch
#define STEPS_PER_REV 200
#define MICROSTEPS_PER_STEP 8
#define MICROSECONDS_PER_MICROSTEP (1000000/(STEPS_PER_REV * MICROSTEPS_PER_STEP)/(RPMS / 60))
int StepCounter = 0;
int Stepping = false;
uint32_t LastStepTime = 0;
uint32_t CurrentTime = 0;
void setup() {
pinMode(STEP_PIN, OUTPUT);
pinMode(DIRECTION_PIN, OUTPUT);
digitalWrite(STEP_PIN, LOW);
digitalWrite(DIRECTION_PIN, LOW);
pinMode(UP_PIN,INPUT);
pinMode(Down_PIN,INPUT);
}
void loop() {
if (digitalRead(UP_PIN) == LOW && Stepping == false)
{
digitalWrite(DIRECTION_PIN, LOW); // Tells board Up on direction Pin using LOW
Stepping = true; // Applies above only if true
}
if (digitalRead(Down_PIN) == LOW && Stepping == false)
{
digitalWrite(DIRECTION_PIN, HIGH); // Tells board Down on direction Pin using HIGH
Stepping = true; // Applies above only if true
}
if (Stepping == true)
{
CurrentTime = micros();
if ((CurrentTime - LastStepTime) > MICROSECONDS_PER_MICROSTEP)
{
LastStepTime = CurrentTime;
digitalWrite(STEP_PIN, HIGH); // When stepping True - Step Pin set HIGH Motor moves at Set Speed in Down Direction
delayMicroseconds((MICROSECONDS_PER_MICROSTEP * 0.9)/2);
digitalWrite(STEP_PIN, LOW); // When stepping True - Step Pin set LOW Motor moves at Set Speed in Down Direction
delayMicroseconds((MICROSECONDS_PER_MICROSTEP * 0.9)/2);
StepCounter = StepCounter + 1;
if (StepCounter == DISTANCE)
{
StepCounter = 0;
Stepping = false;
}
}
}
}