#include <Stepper.h>
const int stepsPerRevolution = 200;
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
const byte pinStartBut = 4;
const byte pinRightBut = 2;
const byte pinLeftBut = 3;
const byte pinPot = A0;
int dir = 1;
bool isOn = false;
int lastStartButton = HIGH;
int currStartButton = HIGH;
int lastRightButton = HIGH;
int currRightButton = HIGH;
int lastLeftButton = HIGH;
int currLeftButton = HIGH;
void setup() {
pinMode(pinStartBut, INPUT_PULLUP);
pinMode(pinRightBut, INPUT_PULLUP);
pinMode(pinLeftBut, INPUT_PULLUP);
pinMode(pinPot, INPUT);
}
void PushStartButton()
{
static unsigned long timer;
if (timer + 50 > millis()) return;
currStartButton = digitalRead(pinStartBut);
if (lastStartButton == HIGH && currStartButton == LOW)
{
isOn = !isOn;
}
lastStartButton = currStartButton;
timer = millis();
}
void PushRightButton()
{
currRightButton = digitalRead(pinRightBut);
if (lastRightButton == HIGH && currRightButton == LOW)
{
dir = 1;
delay(5);
}
lastRightButton = currRightButton;
}
void PushLeftButton()
{
currLeftButton = digitalRead(pinLeftBut);
if (lastLeftButton == HIGH && currLeftButton == LOW)
{
dir = -1;
delay(5);
}
lastLeftButton = currLeftButton;
}
void loop() {
PushStartButton();
PushRightButton();
PushLeftButton();
int speed = analogRead(pinPot);
int motorSpeed = map(speed, 0, 1023, 0, 100);
if (motorSpeed > 0 && isOn)
{
myStepper.setSpeed(motorSpeed);
myStepper.step(dir * stepsPerRevolution / 100);
}
}