/*
Stepper Motor Control using a stepper driver with Stepper.h - one revolution
Simulation https://wokwi.com/projects/360724869269076993
code from
https://github.com/arduino-libraries/Stepper/blob/master/examples/stepper_oneRevolution/stepper_oneRevolution.ino
Modified to a two-wire/quadrature setup for a stepper driver
for https://forum.arduino.cc/t/stepper-driver-with-quadrature-from-stepper-h-or-encoder/1121041
and https://forum.arduino.cc/t/questions-about-my-hx711-stepper-project/1102568/29
and https://github.com/arduino-libraries/Stepper/issues/40
This program drives a stepper motor using a driver and Stepper.h
The motor driver STEP input is attached to digital pin 8 of the Arduino.
The motor driver DIR is attached to digitalPin 7
The digital pin 9 is attached to a DIR status led.
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 2023-03-31 by drf5n
*/
#include <Stepper.h>
const int stepsPerRevolution = 200*4; // change this to fit the number of steps per revolution
// for your motor. The *4 factor is due to the 4 steps/cycle change in the
// Stepper.h two-wire quadrature output setup
// note that the DIR pin toggles in step with the STEP pin, but
// with the level HIGH or LOW depending on direction
//This scheme works per the table at
// https://github.com/arduino-libraries/Stepper/blob/master/src/Stepper.h#L64-L72
/* The sequence of control signals for 2 control wires is as follows
* (columns C1 and C2 from above):
*
* Step C0 C1
* 1 0 1
* 2 1 1
* 3 1 0
* 4 0 0
* If C0 = STEP, and C1 = DIR
* and the step 2 is the leading edge of STEP, then DIR is high
* and in the opposite direction, if 3 is the leading edge of STEP,
* then DIR is low.
*/
// Per https://forum.arduino.cc/t/change-stepper-dir-according-to-predifined-analog-input-value/1118716/25
// and
// initialize the Stepper library on pins 8 through 11:
//Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
// Use 1 pin as STEP and other for DIR
Stepper myStepper(stepsPerRevolution, 8, 7);
const int delayMS = 0;
void setup() {
// set the speed at 60 rpm:
myStepper.setSpeed(60);
// initialize the serial port:
Serial.begin(9600);
}
void loop() {
// step one revolution in one direction:
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
delay(delayMS);
// step one revolution in the other direction:
Serial.println("counterclockwise");
myStepper.step(-stepsPerRevolution);
delay(delayMS);
}