#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <Stepper.h>
// Define the number of steps per revolution for your stepper motor.
#define STEPS_PER_REVOLUTION 200
// Initialize the stepper motor.
Stepper stepper(STEPS_PER_REVOLUTION, 10, 11, 12, 13);
int interval =5; //mm
int one_revolution =1.25; //mm
int stepCount = 0;
//const int relayPin = A3; // Pin to control the relay module
LiquidCrystal_I2C lcd(0x27, 20, 4); // Set the I2C LCD address
// Define the keypad layout and pins.
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] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup() {
stepper.setSpeed(300); // Set the stepper motor speed (adjust as needed)
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the backlight
lcd.setCursor(0, 0);
lcd.print("Position: 0mm");
// Initialize your other setup code here
}
void loop() {
char key = keypad.getKey();
if (key != NO_KEY) {
// Handle keypad input, e.g., start the motor or other actions
}
delay(2000);
// Move the stepper motor in 5mm increments and pause for 1 second
for (int i = 0; i < 500 / 5; i++) {
stepper.step(STEPS_PER_REVOLUTION * (interval /one_revolution)); // 5mm movement
triggerRelay();
delay(500);
lcd.setCursor(0, 1);
lcd.print("Position: " + String(i * 5) + "mm ");
lcd.setCursor(0, 3);
lcd.print("Total Point: " + String(stepCount) + "mm ");
stepCount++;
}
}
void triggerRelay() {
pinMode(A0,OUTPUT);
digitalWrite(A0, HIGH); // Turn on the relay
delay(200); // Wait for 1 second
digitalWrite(A0, LOW); // Turn off the relay
}