#include <Stepper.h>
const int stepsPerRevolution = 200;
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
int stepCount = 0;
const int buttonStart = 3;
int lastButtonStart = LOW;
int curButtonStart = LOW;
const int buttonLeft = 2;
int lastButtonLeft = LOW;
int curButtonLeft = LOW;
const int buttonRight = 4;
int lastButtonRight = LOW;
int curButtonRight = LOW;
bool start = false;
int num = 100;
void setup() {
pinMode(buttonStart, INPUT);
digitalWrite(buttonStart, HIGH);
pinMode(buttonLeft, INPUT);
digitalWrite(buttonLeft, HIGH);
pinMode(buttonRight, INPUT);
digitalWrite(buttonRight, HIGH);
}
int debounce (int last, int button)
{
int current = digitalRead(button);
if (last != current)
{
delay(5);
current = digitalRead(button);
}
return current;
}
void loop()
{
curButtonStart = debounce (lastButtonStart, buttonStart );
if (lastButtonStart == HIGH && curButtonStart == LOW)
{
start = !start;
}
lastButtonStart = curButtonStart;
curButtonLeft = debounce (lastButtonLeft, buttonLeft );
if (lastButtonLeft == HIGH && curButtonLeft == LOW)
{
num = -100;
}
lastButtonLeft = curButtonLeft;
curButtonRight = debounce (lastButtonRight, buttonRight );
if (lastButtonRight == HIGH && curButtonRight == LOW)
{
num = 100;
}
lastButtonRight = curButtonRight;
if (start == true) {
int sensor = analogRead(A0);
int motorSpeed = map(sensor, 0, 1023, 0, 100);
if (motorSpeed > 0)
{
myStepper.setSpeed(motorSpeed);
myStepper.step(stepsPerRevolution / num);
}
}
}