#include <Stepper.h>
Stepper myStepper(200, 4, 5, 6, 7);
#define POT A14
#define RESET A0
#define STEP A1
#define ccw A6
#define cw A7
int rotation = 1; //These two variables is used for the stepper motor rotating motion
int stepCount = 1;
void setup() {
// put your setup code here, to run once:
pinMode(POT, INPUT);
pinMode(RESET, INPUT);
pinMode(STEP, INPUT);
pinMode(ccw, INPUT);
pinMode(cw, INPUT);
int a = 0;
do{
int a = analogRead(POT);
if(a > 0){ //This part of the is for to avoid a bug where the
loop(); //stepper motor doesn't reset properly
}
}while(a == 0);
/*
Yellow Button is for Reset
Blue Button is for Step
Left Green Button is for Counter-Clockwise
Right Green Button is for Clockwise
*/
}
void loop() {
// put your main code here, to run repeatedly:
int motorSpeed = analogRead(POT);
int resetButton = digitalRead(RESET);
int stepButton = digitalRead(STEP);
int cwValue = digitalRead(cw);
int ccwValue = digitalRead(ccw);
if(motorSpeed != 0){
myStepper.setSpeed(motorSpeed); //This part of the code is for to avoid a bug
myStepper.step(rotation); //where the stepper motor doesn't work properly.
}
if(ccwValue == 0 && cwValue == 1){
if(rotation != -1){//This "if" statement is used to avoid a bug where the stepper motor
//doesn't work properly.
rotation = -1; //counter-clockwise
stepCount = 200 - stepCount;//To get the value for the new stepCount.
}
}else if(ccwValue == 1 & cwValue == 0){
if(rotation != 1){//This "if" statement is used to avoid a bug where the stepper motor
//doesn't work properly.
rotation = 1; //clockwise
stepCount = 200 - stepCount;//To get the value for the new stepCount.
}
}
if(resetButton == 0){
if(rotation == 1){
myStepper.setSpeed(1000);
myStepper.step(-stepCount);
stepCount = 0;
delay(500);
}else{
myStepper.setSpeed(1000);
myStepper.step(stepCount);
stepCount = 0;
delay(500);
}
}
if(stepButton == 0){
if(rotation == 1){
myStepper.setSpeed(500); //This part of the code is for the stepper to motor to reset.
myStepper.step(-stepCount);
myStepper.step(353);
stepCount = 153; //This part of the code is to set the specific angle.
delay(500);
}else{
myStepper.setSpeed(500); //This part of the code is for the stepper to motor to reset.
myStepper.step(stepCount);
myStepper.step(-385);
stepCount = 185; //This part of the code is to set the specific angle.
delay(500);
}
}
stepCount++;
if(stepCount > 200){
stepCount = 1;
}
}