/* Simple Stepper Motor Control Exaple Code
by Dejan Nedelkovski, www.HowToMechatronics.com
*/
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
int startPin = 12;
int ledPin = 13;
bool startStatus = HIGH ;
// Defines pins numbers
const int stepPin = 4;
const int dirPin = 3;
const int selectDirPin = 9;
int customDelay, customDelayMapped, oldcustomDelayMapped; // Defines variables
bool dirState = LOW;
bool lastButtonState;
bool currentButtonState;
void setup()
{
Serial.begin(115200);
// Sets the two pins as Outputs
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(selectDirPin, INPUT_PULLUP);
digitalWrite(dirPin, HIGH); //Enables the motor to move in a particular direction
pinMode(startPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
lcd.init();
lcd.clear();
lcd.backlight(); // Make sure backlight is on
// Print a message on both lines of the LCD.
lcd.setCursor(2, 0); //Set cursor to character 2 on line 0
lcd.print("Tom George");
lcd.setCursor(2, 1); //Move cursor to character 2 on line 1
lcd.print("Stepper Control");
delay(500);
lcd.setCursor(0, 0);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(" ");
LCDDisplay();
}
void loop()
{
ReadButton1();
while (startStatus)
{
ReadButton1();
}
dirState = digitalRead(selectDirPin);
if (dirState == LOW)
{
digitalWrite(dirPin, LOW);
}
else
{
digitalWrite(dirPin, HIGH);
}
LCDDisplay();
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(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, 1023, 0, 1, 30000); // Convrests the read values of the potentiometer from 0 to 1023 into desireded delay values (300 to 4000)
return newCustom;
}
void LCDDisplay()
{
lcd.setCursor(0, 0);
lcd.print("Step ms ");
if (customDelayMapped != oldcustomDelayMapped)
{
lcd.setCursor(8, 0);
lcd.print(" ");
lcd.setCursor(8, 0);
lcd.print(customDelayMapped);
oldcustomDelayMapped = customDelayMapped;
}
lcd.setCursor(0, 1);
lcd.print("dir ");
if (dirState == HIGH)
{
lcd.print("FWD");
}
else
{
lcd.print("REV");
}
lcd.setCursor(8, 1);
if (startStatus)
{
lcd.print("STOP");
}
else
{
lcd.print("RUN ");
}
}
void ReadButton1()
{
currentButtonState = digitalRead(startPin); // read new state
if (lastButtonState == HIGH && currentButtonState == LOW) {
// Serial.println("The button is pressed");
// toggle state of LED
startStatus = !startStatus;
// control LED arccoding to the toggled state
digitalWrite(ledPin, startStatus);
}
lastButtonState = currentButtonState; // save the last state
delay(10);
dirState = digitalRead(selectDirPin);
if (dirState == LOW)
{
digitalWrite(dirPin, LOW);
}
else
{
digitalWrite(dirPin, HIGH);
}
LCDDisplay();
// Serial.println(dirState);
}