const int stepPin = 9;
const int dirPin = 8;
const int switchPin = 2;
const int sensorPin = 3;
int customDelay, customDelayMapped;
void setup() {
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(switchPin, INPUT_PULLUP); // Set the switch pin as an input with a pullup resistor
pinMode(sensorPin, INPUT_PULLUP);
digitalWrite(dirPin, HIGH); // Set the initial direction to clockwise
}
void loop() {
if (digitalRead(sensorPin) == LOW) {
digitalWrite(stepPin, LOW);
delayMicroseconds(0);
}
else
// Check if the switch is pressed and update the direction accordingly
if (digitalRead(switchPin) == LOW) {
digitalWrite(dirPin, LOW); // Set the direction to counterclockwise
}
else {
digitalWrite(dirPin, HIGH); // Set the direction to clockwise
}
customDelayMapped = speedUp(); // Get the delay value from the speedUp function
// Step the motor with the custom delay and direction
digitalWrite(stepPin, HIGH);
delayMicroseconds(customDelayMapped);
digitalWrite(stepPin, LOW);
delayMicroseconds(customDelayMapped);
}
int speedUp() {
int customDelay = analogRead(A0);
int newCustom = map(customDelay, 0, 1023, 300, 4000);
return newCustom;
}