#include <AccelStepper.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
const byte ROW_NUM = 4;
const byte COLUMN_NUM = 4;
char keys[ROW_NUM][COLUMN_NUM] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
byte pin_rows[ROW_NUM] = { 4, 5, 18, 19 };
byte pin_column[COLUMN_NUM] = { 13, 12, 14, 27 };
Keypad keypad = Keypad(makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM);
LiquidCrystal_I2C lcd(0x27, 16, 2);
AccelStepper stepper1(1, 15, 2);
unsigned long st = 0;
unsigned long count_millis = 0;
bool state = false;
bool st1 = true;
bool waitForInput = false;
bool waitForInput_for_steps = false;
int steps_motor = 0;
long intervel = 0;
int count_time = 0;
void setup() {
Serial.begin(9600);
Wire.begin();
lcd.begin(16, 2);
lcd.setBacklight(255);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("LCD Initialized");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("System ready");
stepper1.setMaxSpeed(500);
stepper1.setAcceleration(500);
stepper1.setCurrentPosition(0);
}
void loop() {
char key = keypad.getKey();
if (key) {
if (key == 'A') {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Steps:");
waitForInput_for_steps = true;
} else if (key == 'B') {
stepper1.stop();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Time in sec:");
intervel = 0;
waitForInput = true;
} else if (key == '#') {
lcd.setCursor(0, 1);
lcd.print("Done");
delay(1000);
waitForInput = false;
waitForInput_for_steps = false;
} else if (key == '*') {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Start");
delay(1000);
st1 = false;
}
}
if (waitForInput && isDigit(key)) {
intervel = intervel * 10 + (key - '0');
lcd.print(key);
}
if (waitForInput_for_steps && isDigit(key)) {
steps_motor = steps_motor * 10 + (key - '0');
lcd.print(key);
}
while (st1 != true) {
stepper1.moveTo(steps_motor);
stepper1.runToPosition();
if (stepper1.currentPosition() >= steps_motor && !state) {
stepper1.setSpeed(500);
state = true;
st = millis();
lcd.clear();
}
if (millis() - count_millis > 1000 && state == true) {
count_millis = millis();
count_time++;
lcd.setCursor(0, 0);
lcd.print("Time:");
lcd.print(count_time);
}
if (millis() - st > (1000 * intervel) && state == true) {
stepper1.moveTo(0);
stepper1.runToPosition();
if (stepper1.currentPosition() == 0 && !st1) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("System ready");
st1 = true;
state = false;
intervel=0;
steps_motor=0;
count_time=0;
}
}
}
}