#include <AccelStepper.h>
#include <LiquidCrystal.h>
// Define stepper motor connections and motor interface type
#define DIR_PIN 11
#define STEP_PIN 12
#define MOTOR_INTERFACE_TYPE 1
// Define potentiometer pin
#define POT_PIN A0
// LCD connections
const int rs = 10, en = 9, d4 = 8, d5 = 7, d6 = 6, d7 = 5;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
// Create a new instance of the AccelStepper class
AccelStepper stepper(MOTOR_INTERFACE_TYPE, STEP_PIN, DIR_PIN);
unsigned long lastUpdateTime = 0;
const unsigned long updateInterval = 500; // LCD and serial update interval in ms
void setup() {
// Set the maximum speed and acceleration
stepper.setMaxSpeed(1000); // Adjust as needed
stepper.setAcceleration(500); // Adjust as needed
// Initialize the LCD
lcd.begin(16, 2);
// Start the serial communication
Serial.begin(9600);
// Initial LCD display
lcd.setCursor(0, 0);
lcd.print("Pot: ");
lcd.setCursor(0, 1);
lcd.print("Speed: ");
}
void loop() {
// Read the potentiometer value (0 to 1023)
int potValue = analogRead(POT_PIN);
// Map the potentiometer value to a speed range
float speed = map(potValue, 0, 1023, 0, 1000); // Speed in steps per second
// Set the motor speed
stepper.setSpeed(speed);
// Run the motor at the set speed
stepper.runSpeed(); // Non-blocking function to run the motor at constant speed
// Update LCD and serial output periodically
if (millis() - lastUpdateTime >= updateInterval) {
lastUpdateTime = millis();
// Update LCD display
lcd.setCursor(5, 0);
lcd.print(potValue); // Update potentiometer value on LCD
lcd.setCursor(7, 1);
lcd.print(speed); // Update speed value on LCD
lcd.print(" "); // Clear any leftover characters
// Print to serial monitor
//Serial.print("Potentiometer: ");
//Serial.print(potValue);
// Serial.print(" | Speed: ");
// Serial.println(speed);
}
}