// include the Stepper and LiquidCrystal_I2C libraries
#include <Stepper.h>
#include <LiquidCrystal_I2C.h>
// define the step and direction pins
#define stepPin 5
#define dirPin 4
// create a Stepper object
Stepper myStepper(200, stepPin, dirPin);
// define the push button pins
#define buttonPin1 2
#define buttonPin2 3
// define the potentiometer pin
#define potPin A0
// create a LiquidCrystal_I2C object
LiquidCrystal_I2C lcd(0x27, 16, 2); // change the address and size of your LCD
// define the current state of the push buttons
int buttonState1 = LOW;
int buttonState2 = LOW;
// define the speed variable
int speed = 0;
void setup() {
// set the step and direction pins as outputs
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
// set the push button pins as inputs
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
// set the potentiometer pin as an input
pinMode(potPin, INPUT);
// initialize the LCD
lcd.init();
lcd.backlight();
// print the initial message on the LCD
lcd.setCursor(0, 0);
lcd.print("Motor stopped");
}
void loop() {
// read the state of the push buttons
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
// read the value of the potentiometer
speed = analogRead(potPin);
// map the potentiometer value to the motor speed range
speed = map(speed, 0, 1023, 0, 100);
// if button 1 is pressed, rotate the motor clockwise
if (buttonState1 == HIGH) {
digitalWrite(dirPin, HIGH); // set the motor direction
myStepper.setSpeed(speed); // set the motor speed
myStepper.step(200); // rotate the motor one full revolution
lcd.clear(); // clear the LCD screen
lcd.setCursor(0, 0); // set the cursor at the first row
lcd.print("Clockwise"); // display the direction of rotation
}
// if button 2 is pressed, rotate the motor counterclockwise
else if (buttonState2 == HIGH) {
digitalWrite(dirPin, LOW); // set the motor direction
myStepper.setSpeed(speed); // set the motor speed
myStepper.step(200); // rotate the motor one full revolution
lcd.clear(); // clear the LCD screen
lcd.setCursor(0, 0); // set the cursor at the first row
lcd.print("AntiClockwise"); // display the direction of rotation
}
// otherwise, stop the motor
else {
digitalWrite(dirPin, LOW); // set the motor direction to stop
digitalWrite(stepPin, LOW); // set the step pin to low to stop the motor
lcd.clear(); // clear the LCD screen
lcd.setCursor(0, 0); // set the cursor at the first row
lcd.print("Motor stopped"); // display the message for stopped motor
}
// display the motor speed on the second row of the LCD
lcd.setCursor(0, 1); // set the cursor at the second row
lcd.print("Speed: "); // display the text for speed
lcd.print(speed); // display the actual speed value
}