/* Simple Stepper Motor Control Exaple Code
*
* by Dejan Nedelkovski, www.HowToMechatronics.com
*
*/
// Defines pins numbers
const int stepPin = 3;
const int dirPin = 4;
const int btnPin = 7;
const int btnPin2 = 8;
int customDelay,customDelayMapped; // Defines variables
void setup() {
// Sets the two pins as Outputs
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode(btnPin,INPUT);
pinMode(btnPin2,INPUT);
pinMode(13, OUTPUT);
}
void loop() {
if (digitalRead(btnPin) or digitalRead(btnPin2)) {
//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
digitalWrite(13, HIGH);
if(digitalRead(btnPin2)){
digitalWrite(dirPin,HIGH); //Enables the motor to move in a particular direction
customDelayMapped = 2000;
digitalWrite(stepPin, HIGH);
delayMicroseconds(customDelayMapped);
digitalWrite(stepPin, LOW);
delayMicroseconds(customDelayMapped);
}
else{
digitalWrite(dirPin,LOW); //Enables the motor to move in a particular direction
customDelayMapped = 2000;
digitalWrite(stepPin, HIGH);
delayMicroseconds(customDelayMapped);
digitalWrite(stepPin, LOW);
delayMicroseconds(customDelayMapped);
}
}
else{
digitalWrite(13, LOW);
}
}
// Function for reading the Potentiometer
int speedUp() {
int customDelay = analogRead(A0); // Reads the potentiometer
int newCustom = map(customDelay, 0, 1023, 300,4000); // Convrests the read values of the potentiometer from 0 to 1023 into desireded delay values (300 to 4000)
return newCustom;
}