#include <LiquidCrystal.h>
#include <Keypad.h>
#include <AccelStepper.h>
// LCD Pins
const int rs = 8, en = 9, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
// Keypad setup
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {A0, A1, A2, A3}; // Connect to the row pinouts of the keypad
byte colPins[COLS] = {A4, A5, 10, 11}; // Connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Stepper motor setup
#define DIR_PIN 2
#define STEP_PIN 3
#define ENABLE_PIN 12
AccelStepper stepper(AccelStepper::DRIVER, STEP_PIN, DIR_PIN);
int retractSteps = 500; // Default retract steps
int extendSpeed = 100; // Default extend speed
int retractSpeed = 100; // Default retract speed
// Button pins
const int button1Pin = 12;
const int button2Pin = 13;
void setup() {
// Set up the LCD
lcd.begin(16, 2);
lcd.print("Stepper Control");
// Set up the buttons
pinMode(button1Pin, INPUT_PULLUP);
pinMode(button2Pin, INPUT_PULLUP);
// Set up the stepper motor
pinMode(ENABLE_PIN, OUTPUT);
digitalWrite(ENABLE_PIN, LOW); // Enable the stepper driver
stepper.setMaxSpeed(1000);
stepper.setAcceleration(200);
}
void loop() {
char key = keypad.getKey();
if (key) {
switch (key) {
case 'A': // Increase retract steps
retractSteps += 50;
lcd.clear();
lcd.print("Retract Steps:");
lcd.setCursor(0, 1);
lcd.print(retractSteps);
break;
case 'B': // Decrease retract steps
retractSteps -= 50;
lcd.clear();
lcd.print("Retract Steps:");
lcd.setCursor(0, 1);
lcd.print(retractSteps);
break;
case 'C': // Increase extend speed
extendSpeed += 50;
lcd.clear();
lcd.print("Extend Speed:");
lcd.setCursor(0, 1);
lcd.print(extendSpeed);
break;
case 'D': // Decrease extend speed
extendSpeed -= 50;
lcd.clear();
lcd.print("Extend Speed:");
lcd.setCursor(0, 1);
lcd.print(extendSpeed);
break;
case '*': // Increase retract speed
retractSpeed += 50;
lcd.clear();
lcd.print("Retract Speed:");
lcd.setCursor(0, 1);
lcd.print(retractSpeed);
break;
case '#': // Decrease retract speed
retractSpeed -= 50;
lcd.clear();
lcd.print("Retract Speed:");
lcd.setCursor(0, 1);
lcd.print(retractSpeed);
break;
}
}
// Check button 1 to extend
if (digitalRead(button1Pin) == LOW) {
stepper.setMaxSpeed(extendSpeed);
stepper.moveTo(1000); // or any large number to keep extending
stepper.run();
}
// Check button 2 to retract
if (digitalRead(button2Pin) == LOW) {
stepper.setMaxSpeed(retractSpeed);
stepper.moveTo(-retractSteps);
stepper.run();
}
}