const int stepPin = 3;
const int dirPin = 2;
const int sleepPin = 4;
int customDelay,customDelayMapped; // Defines variables
const int FORWbuttonPin = 6;
int FORWbuttonState = 0;
const int ONbuttonPin = 7;
int ONbuttonState = 0;
const int STOPbuttonPin = 8;
int STOPbuttonState = 0;
void setup() {
// Sets the two pins as Outputs
pinMode(FORWbuttonPin, INPUT);
pinMode(ONbuttonPin, INPUT);
pinMode(STOPbuttonPin, INPUT);
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode(sleepPin,OUTPUT);
digitalWrite(dirPin,HIGH); //Enables the motor to move in a particular direction
}
void loop() {
customDelayMapped = speedUp(); // Gets custom delay values from the custom speedUp function
// Makes pules with custom delay, depending on the Potentiometer, from which the speed of the motor depends
FORWbuttonState = digitalRead(FORWbuttonPin);
if (FORWbuttonState == HIGH) {
digitalWrite(dirPin, HIGH);
} else {
digitalWrite(dirPin, LOW);
}
STOPbuttonState = digitalRead(STOPbuttonPin);
if (STOPbuttonState == HIGH) {
digitalWrite(sleepPin, LOW);
} else {
digitalWrite(sleepPin, HIGH);
}
ONbuttonState = digitalRead(ONbuttonPin);
if (ONbuttonState == HIGH) {
digitalWrite(sleepPin, HIGH);
} else {
digitalWrite(sleepPin, LOW);
}
if (ONbuttonState == HIGH && STOPbuttonState == HIGH) {
digitalWrite(sleepPin, LOW);
}
digitalWrite(stepPin, HIGH);
delayMicroseconds(customDelayMapped);
digitalWrite(stepPin, LOW);
delayMicroseconds(customDelayMapped);
}
// Function for reading the Potentiometer
int speedUp() {
int customDelay = analogRead(A0); // Reads the potentiometer
int newCustom = map(customDelay, 0, 1023, 5,300); // Convrests the read values of the potentiometer from 0 to 1023 into desireded delay values (300 to 4000)
return newCustom;
}