// Include the Arduino Stepper Library
#include <Stepper.h>
// Number of steps per output rotation
const int stepsPerRevolution = 200;
// make this 1 if you dont have a gear reduction
int gearReduction = 1;
// Current position of the motor
int currentPos = 0;
// number of stepps per layer
int steppsPerLayer = 5;
//Button Pins
// the number of the Button pin
const int buttonPin = 2;
// the pin for the button that moves the motors back to zero
const int resetPin = 7;
// the pin for the button that sets the current position as zero
const int zeroPin = 8;
// Create Instance of Stepper library
Stepper myStepper(stepsPerRevolution, 3, 4, 5, 6);
void setup()
{
// set the speed at 60 rpm:
myStepper.setSpeed(60);
// initialize the button pin as an input:
pinMode(buttonPin, INPUT);
// initialize the serial port:
Serial.begin(9600);
}
void loop()
{
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (digitalRead(buttonPin) == HIGH) {
moveToPos(currentPos + steppsPerLayer);
delay(1000);
}
if (digitalRead(resetPin) == HIGH) {
moveToPos(0);
}
if (digitalRead(zeroPin) == HIGH) {
currentPos = 0;
}
delay(100);
}
void moveToPos(float targetPos){
//calculate how far to move the motor
int stepsToMove = (targetPos - currentPos) * gearReduction;
//move the motor
myStepper.step(stepsToMove);
//set current position
currentPos = targetPos;
}