#define potentiometer 34 // Analog pin for the potentiometer
#define bt_F 35 // Clockwise Button
#define bt_S 12 // Stop Button
#define bt_B 13 // Anticlockwise Butto
#define dirPin 16 // GPIO pin for Direction of stepper motor driver
#define stepPin 17 // GPIO pin for Step of stepper motor driver
#define enPin 21 // GPIO pin for Enabled of stepper motor driver
int read_ADC;
int Speed_Delay;
int Mode=1, flag=0;
void setup() {
pinMode(potentiometer, INPUT); // declare potentiometer as input
pinMode(bt_F, INPUT_PULLUP); // declare bt_F as input
pinMode(bt_S, INPUT_PULLUP); // declare bt_S as input
pinMode(bt_B, INPUT_PULLUP); // declare bt_B as input
pinMode(dirPin, OUTPUT); // declare as output for Direction of stepper motor driver
pinMode(stepPin, OUTPUT); // declare as output for Step of stepper motor driver
pinMode(enPin, OUTPUT); // declare as output for Enabled of stepper motor driver
}
void loop() {
read_ADC = analogRead(potentiometer); // read analogue to digital value 0 to 4095 (12-bit ADC)
Speed_Delay = map(read_ADC, 0, 4095, 5000, 10); // value map for Microstep resolution Delay
if(digitalRead (bt_F) == 0) { // For Clockwise
Mode = 2;
digitalWrite(enPin, LOW);
}
if(digitalRead (bt_S) == 0) { // For Stop
if(flag == 0) {
flag = 1;
if(Mode > 1) Mode = 1;
else {
Mode = !Mode;
if(Mode == 0) digitalWrite(enPin, HIGH);
else digitalWrite(enPin, LOW);
}
delay(100);
}
} else {
flag = 0;
}
if(digitalRead (bt_B) == 0) { // For Anticlockwise
Mode = 3;
digitalWrite(enPin, LOW);
}
if(Speed_Delay > 0 && Mode > 1) {
if(Mode == 2) digitalWrite(dirPin, LOW); // Stepper motor rotates CW (Clockwise)
else digitalWrite(dirPin, HIGH); // Stepper motor rotates CCW (Anticlockwise)
digitalWrite(stepPin, HIGH);
delayMicroseconds(Speed_Delay);
digitalWrite(stepPin, LOW);
delayMicroseconds(Speed_Delay);
}
}