#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 MOVE_PIN 52 // Built-in LED on most Arduino boards
#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};
// Define the LED pins
const int ledPins[] = {A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11};
// Define the positions
const long positions[] = {0, 400, 800, 100, 120, 1400, 160, 180, 200, 1570};
// Define the set points for each position
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);
stepper.setAcceleration(600.0);
// Set the button pins as input and LED pins as output
for (int i = 0; i < 12; i++) {
pinMode(buttonPins[i], INPUT_PULLUP);
pinMode(ledPins[i], OUTPUT);
}
// Set the LED pin for move indication as output
pinMode(MOVE_PIN, OUTPUT);
// Initialize the LCD display
lcd.init();
lcd.backlight();
lcd.clear();
}
void loop() {
// Check for button presses
for (int i = 0; i < 12; i++) {
if (digitalRead(buttonPins[i]) == LOW) {
// Turn on the corresponding LED and move to the specified position
digitalWrite(ledPins[i], HIGH);
stepper.moveTo(positions[i]);
digitalWrite(MOVE_PIN, HIGH);
stepper.runToPosition();
delay(100);
// Turn off all other LEDs
for (int j = 0; j < 12; j++) {
if (j != i) {
digitalWrite(ledPins[j], LOW);
}
}
// Update the LCD display
lcd.setCursor(0, 0);
lcd.print("Position: ");
lcd.setCursor(9, 0);
lcd.print(positions[i]);
lcd.setCursor(0, 1);
lcd.print("Yard: ");
lcd.setCursor(6, 1);
lcd.print(setPoints[i]);
// Check for even or odd button press
if (i % 2 == 0) { // Even button
digitalWrite(50, HIGH); // Turn on LED 50
digitalWrite(51, LOW); // Ensure LED 51 is off
} else { // Odd button
digitalWrite(51, HIGH); // Turn on LED 51
digitalWrite(50, LOW); // Ensure LED 50 is off
}
} else {
// Turn off the corresponding LED if the button is not pressed
digitalWrite(ledPins[i], LOW);
}
}
// Control motor movement with buttons 14 and 15
if (digitalRead(buttonPins[10]) == LOW) {
stepper.setSpeed(100);
stepper.runSpeed();
} else if (digitalRead(buttonPins[11]) == LOW) {
stepper.setSpeed(-100);
stepper.runSpeed();
} else {
stepper.stop();
}
// Check for reset button press
if (digitalRead(RESET_PIN) == LOW) {
stepper.setCurrentPosition(0);
stepper.stop();
lcd.clear();
}
}