/*
Stepper Motor Control - Bounce Between Limit Switches
https://wokwi.com/projects/403962641012999169
This program drives a unipolar or bipolar stepper motor.
The motor is attached to digital pins 8 - 11 of the Arduino.
The motor should revolve one revolution in one direction, then
one revolution in the other direction.
Created 11 Mar. 2007
Modified 30 Nov. 2009
by Tom Igoe
Modified to use a simple two-state state machine for
https://forum.arduino.cc/t/stepper-motor-not-running-continuously/1283598
*/
#include <Stepper.h>
const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
// for your motor
// initialize the Stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
int movingCW = true;
void setup() {
// set the speed at 60 rpm:
myStepper.setSpeed(60);
// initialize the serial port:
Serial.begin(9600);
pinMode(A0, INPUT_PULLUP);
pinMode(A1, INPUT_PULLUP);
}
void loop() {
if (movingCW) {
// step one revolution in one direction:
myStepper.step(stepsPerRevolution/100);
if (digitalRead(A0) == LOW) {
movingCW = false;
Serial.println("counterclockwise");
}
} else {
// step one revolution in the other direction:
myStepper.step(-stepsPerRevolution/100);
if (digitalRead(A1) == LOW) {
movingCW = true;
Serial.println("clockwise");
}
}
// delay(500);
}