#include <Wire.h>
#include <AccelStepper.h>
#include <LiquidCrystal_I2C.h>
// Define the motor interface type
#define motorInterfaceType 1
// Define the step and direction pins
#define stepPin 3
#define dirPin 4
// Define the button pins
#define button1Pin 5
#define button2Pin 6
// Define the I2C address of the LCD display
#define LCD_ADDRESS 0x27
// Define the number of columns and rows of the LCD display
#define LCD_COLUMNS 16
#define LCD_ROWS 2
// Create a new instance of the AccelStepper class
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);
// Create a new instance of the LiquidCrystal_I2C class
LiquidCrystal_I2C lcd(LCD_ADDRESS, LCD_COLUMNS, LCD_ROWS);
void setup() {
// Set the maximum speed and acceleration
stepper.setMaxSpeed(1000);
stepper.setAcceleration(500);
// Set the button pins as input
pinMode(button1Pin, INPUT);
pinMode(button2Pin, INPUT);
// Initialize the LCD display
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Speed: ");
lcd.setCursor(0, 1);
lcd.print("Position: ");
}
void loop() {
// Check if button 1 is pressed
if (digitalRead(button1Pin) == HIGH) {
// Move forward 400 steps
stepper.move(400);
stepper.runToPosition();
delay(100);
}
// Check if button 2 is pressed
if (digitalRead(button2Pin) == HIGH) {
// Move backward 1200 steps
stepper.move(-1200);
stepper.runToPosition();
delay(100);
}
// Update the LCD display with the current speed and position of the stepper motor
lcd.setCursor(7, 0);
lcd.print(stepper.speed());
lcd.setCursor(10, 1);
lcd.print(stepper.currentPosition());
}