#define stepPin 1
#define dirPin 3
const int switchOne = 8;
const int switchTwo = 9;
int p1buttonState = 1;
int p2buttonState = 1;
int customDelay, customDelayMapped;
void setup() {
// Sets the two pins as Outputs
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
// Sets Switches pins as Input Pullup
pinMode( switchOne, INPUT_PULLUP);
pinMode( switchTwo, INPUT_PULLUP);
}
void loop() {
p1buttonState = digitalRead(switchOne);
p2buttonState = digitalRead(switchTwo);
if (p1buttonState == 0) {
//Set clockwise rotation direction
digitalWrite(dirPin, HIGH);
rotation();
} else
if (p2buttonState == 0) {
//Set counterclockwise rotation direction
digitalWrite(dirPin, LOW);
rotation();
}
}
void rotation () {
speedControl();
// Makes pules with custom delay, depending on the Potentiometer, from which the speed of the motor depends
digitalWrite(stepPin, HIGH);
delayMicroseconds(customDelayMapped);
digitalWrite(stepPin, LOW);
delayMicroseconds(customDelayMapped);
}
// Custom function for reading the potentiometer and mapping its value from 300 to 3000, suitable for the custom delay value in microseconds
void speedControl() {
customDelay = analogRead(A0); // Read the potentiometer value
customDelayMapped = map(customDelay, 0, 1023, 300, 3000); // Convert the analog input from 0 to 1024, to 300 to 3000
}