#include <Stepper.h>
const int S = 3;
const int R = 2;
const int L = 4;
const int speed = A0;
bool start = false;
bool clockwise = true;
int currLeft = LOW, lastLeft = LOW;
int currRight = LOW, lastRight = LOW;
int currStart = LOW, lastStart = LOW;
const int stepsPerRevolution = 200;
Stepper myStepper(stepsPerRevolution, 10, 11, 12, 13);
int Debouncer(int last, int button){
int current = digitalRead(button);
if(last != current){
delay(5);
current = digitalRead(button);
}
return current;
}
void StartStop(){
currStart = Debouncer(lastStart, S);
if(lastStart == HIGH && currStart == LOW){
if(start){
Serial.println("Start");
}
else{
Serial.println("Stop");
}
start = !start;
}
lastStart = currStart;
}
void Left(){
currLeft = Debouncer(lastLeft, L);
if(lastLeft == HIGH && currLeft == LOW){
clockwise = false;
}
lastLeft = currLeft;
}
void Right(){
currRight = Debouncer(lastRight, R);
if(lastRight == HIGH && currRight == LOW){
clockwise = true;
}
lastRight = currRight;
}
int TurnSide(){
if(clockwise){
return 1;
}
else if(!clockwise){
return -1;
}
return 0;
}
void setup() {
Serial.begin(9600);
pinMode(S, INPUT);
digitalWrite(S, HIGH);
pinMode(L, INPUT);
digitalWrite(L, HIGH);
pinMode(R, INPUT);
digitalWrite(R, HIGH);
}
void loop()
{
int sensor = analogRead(A0);
int motorSpeed = map(sensor, 0, 1023, 0, 100);
StartStop();
Right();
Left();
if (motorSpeed > 0 && start)
{
myStepper.setSpeed(motorSpeed);
myStepper.step(stepsPerRevolution / 100 * TurnSide());
}
}