Skip to content

Instantly share code, notes, and snippets.

@JeffersGlass

JeffersGlass/buttonDrive Secret

Created Apr 5, 2020
Embed
What would you like to do?
/*
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
You can’t perform that action at this time.