Skip to content

Instantly share code, notes, and snippets.

@JeffersGlass
Created April 5, 2020 19:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JeffersGlass/9025c83d9b3a6fa06c4a400b2b4e5296 to your computer and use it in GitHub Desktop.
Save JeffersGlass/9025c83d9b3a6fa06c4a400b2b4e5296 to your computer and use it in GitHub Desktop.
/*
Stepper Motor Control - one revolution
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
*/
#include <Stepper.h>
const int stepsPerRevolution = 400; // change this to fit the number of steps per revolution
// for your motor
const int BUTTON_CLOCKWISE = 7;
const int BUTTON_COUTERCLOCKWISE = 6;
const int SINGLE_STEP = 1;
long lastButtonTime;
const int TIMEOUT = 1000;
const int PINS[4] = {8, 9, 10, 11};
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, PINS[0], PINS[1], PINS[2], PINS[3]);
void setup() {
// set the speed at 60 rpm:
myStepper.setSpeed(60);
// initialize the serial port:
Serial.begin(9600);
pinMode(BUTTON_CLOCKWISE, INPUT_PULLUP);
pinMode(BUTTON_COUTERCLOCKWISE, INPUT_PULLUP);
lastButtonTime = 0;
}
void loop() {
if (digitalRead(BUTTON_CLOCKWISE) == LOW){
myStepper.step(SINGLE_STEP);
lastButtonTime = millis();
}
else if (digitalRead(BUTTON_COUTERCLOCKWISE) == LOW){
myStepper.step(-1 * SINGLE_STEP);
lastButtonTime = millis();
}
if (millis() - lastButtonTime > TIMEOUT){
for (int i = 0; i < 4; i++){
digitalWrite(PINS[i], LOW);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment