#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <AccelStepper.h>
// Define constants for stepper motor pins
#define X_STEP_PIN 2
#define X_DIR_PIN 3
#define Y_STEP_PIN 4
#define Y_DIR_PIN 5
// Define constants for LCD
#define LCD_ADDRESS 0x27 // I2C address of the LCD
#define LCD_COLUMNS 16
#define LCD_ROWS 2
// Define constants for keypad pins
const byte ROWS = 4;
const byte COLS = 4;
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};
byte colPins[COLS] = {A4, A5, 6, 7};
// Define stepper motor parameters
AccelStepper xStepper(AccelStepper::DRIVER, X_STEP_PIN, X_DIR_PIN);
AccelStepper yStepper(AccelStepper::DRIVER, Y_STEP_PIN, Y_DIR_PIN);
// Define LCD object
LiquidCrystal_I2C lcd(LCD_ADDRESS, LCD_COLUMNS, LCD_ROWS);
// Define keypad object
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Define variables for current position
int currentX = 0;
int currentY = 0;
void setup() {
// Set up motor speed and LCD
xStepper.setMaxSpeed(1000); // Adjust speed as needed
yStepper.setMaxSpeed(1000); // Adjust speed as needed
lcd.init();
lcd.backlight();
}
void loop() {
// Wait for user input
char key = keypad.getKey();
// If a key is pressed
if (key != NO_KEY) {
int roomNumber = key - '0'; // Convert char to int
// Calculate target coordinates based on room number
int targetX = calculateXCoordinate(roomNumber);
int targetY = calculateYCoordinate(roomNumber);
// Move to target coordinates
moveTo(targetX, targetY);
// Display confirmation on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Moved to Room ");
lcd.print(roomNumber);
delay(2000); // Display message for 2 seconds
}
}
// Function to calculate X coordinate based on room number
int calculateXCoordinate(int roomNumber) {
// Implement your calculation logic here
}
// Function to calculate Y coordinate based on room number
int calculateYCoordinate(int roomNumber) {
// Implement your calculation logic here
}
// Function to move to target coordinates
void moveTo(int targetX, int targetY) {
// Move along X axis
xStepper.moveTo(targetX);
xStepper.runToPosition();
// Move along Y axis
yStepper.moveTo(targetY);
yStepper.runToPosition();
// Update current position
currentX = targetX;
currentY = targetY;
}