#include <Wire.h>
#include <AccelStepper.h>
#include <LiquidCrystal_I2C.h>
// Define the step and direction pins
#define STEP_PIN 2
#define DIR_PIN 3
#define movePin 13 // Built-in LED on most Arduino boards
// 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::DRIVER, STEP_PIN, DIR_PIN);
// Create a new instance of the LiquidCrystal_I2C class
LiquidCrystal_I2C lcd(LCD_ADDRESS, LCD_COLUMNS, LCD_ROWS);
// Define the button pins
const int buttonPins[] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; // Use digital pins 14 and 15 for buttons
// Define the LED pins
const int ledPins[] = {A0, A1, A2, A3, A4, A5,A6 , A7, A8, A9, A10, A11}; // Use digital pins 16 and 17 for LEDs
// Define the positions
const long positions[] = {0, 400, 800, 100, 120, 1400, 160, 180, 200, 1570};
// Define the set points for each positio
const long setPoints[] = {1, 2, 3, 4,5 , 6, 7, 8, 9, 10};
// Define the button pin for resetting the stepper motor count
#define RESET_PIN 16
void setup() {
// Set the maximum speed and acceleration
stepper.setMaxSpeed(2000.0); // Adjust as needed
stepper.setAcceleration(600.0); // Adjust as needed
// Set the button pins as input
for (int i = 0; i < 12; i++) {
pinMode(buttonPins[i], INPUT_PULLUP);
pinMode(ledPins[i], OUTPUT); // Set LED pins as output
// Set the LED pin as output
pinMode(movePin, OUTPUT);
// Initialize the LCD display
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(" ");
}
}
void loop() {
// Check for button presses
for (int i = 0; i < 10; i++) {
if (digitalRead(buttonPins[i]) == LOW) {
// Turn on the corresponding LED and move to the specified position
digitalWrite(ledPins[i], HIGH); // Turn on the LED
stepper.moveTo(positions[i]);
digitalWrite(movePin, HIGH); // Turn on the LED
stepper.runToPosition();
delay(100);
// Turn off all other LEDs
for (int j = 0; j < 10; j++) {
if (j != i) {
digitalWrite(ledPins[j], LOW); // Turn off the LED
digitalWrite(movePin, LOW); // Turn off the LED
}
// Update the LCD display with the current speed and position of the stepper motor
lcd.setCursor(7, 0); // Set cursor for motor steps
lcd.print(stepper.currentPosition()); // Print the current position
lcd.setCursor(7, 0); // Set cursor for motor steps
lcd.print(stepper.currentPosition()); // Print the current position
}
// Check for button 14 press to control LED on pin 14
if (digitalRead(buttonPins[10]) == LOW) {
digitalWrite(ledPins[10], HIGH); // Turn on the LED connected to pin 14
digitalWrite(movePin, HIGH); // Turn on the LED if motor is running
} else {
digitalWrite(ledPins[10], LOW); // Turn off the LED connected to pin 14
digitalWrite(movePin, LOW); // Turn off the LED if motor is not running
}
// Check for button 15 press to control LED on pin 15
if (digitalRead(buttonPins[11]) == LOW) {
digitalWrite(ledPins[11], HIGH); // Turn on the LED connected to pin 15
digitalWrite(movePin, HIGH); // Turn on the LED if motor is running
} else {
digitalWrite(ledPins[11], LOW); // Turn off the LED connected to pin 15
digitalWrite(movePin, LOW); // Turn off the LED if motor is not running
}
// Check for button 14 press to move motor forward
if (digitalRead(buttonPins[10]) == LOW) {
stepper.setSpeed(2000); // Set the speed for forward movement
stepper.runSpeed();
} else {
stepper.stop(); // Stop the motor if the button is not pressed
}
// Check for button 15 press to move motor in reverse
if (digitalRead(buttonPins[11]) == LOW) {
stepper.setSpeed(-200); // Set the speed for reverse movement
stepper.runSpeed();
} else {
stepper.stop(); // Stop the motor if the button is not pressed
}
// Check for reset button press to reset the stepper motor count
if (digitalRead(RESET_PIN) == LOW) {
stepper.setCurrentPosition(0); // Reset the stepper motor count to zero
stepper.stop(); // Stop the motor if the button is pressed
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(" ");
}
// When the endstop is triggered, set the current position as home (0)
stepper.setCurrentPosition(0);
}