#include <Keypad.h>
#include <LiquidCrystal.h>
// Define LCD pins (non-I2C)
const int lcdRs = 8, lcdEn = 9, lcdD4 = 10, lcdD5 = 11, lcdD6 = 12, lcdD7 = 13;
LiquidCrystal lcd(lcdRs, lcdEn, lcdD4, lcdD5, lcdD6, lcdD7);
// Define stepper motor pins
const int motorPinAPlus = 2;
const int motorPinAMinus = 3;
const int motorPinBPlus = 4;
const int motorPinBMinus = 5;
// Define keypad configuration
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] = {6, 7, A0, A1}; // Connect to the row pinouts of the keypad
byte colPins[COLS] = {A2, A3, A4, A5}; // Connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
// Variables for user input
int degree = 0;
int minute = 0;
int second = 0;
bool inputCompleted = false;
void setup() {
// Initialize LCD
lcd.begin(20, 4);
lcd.print("Enter Degrees:");
lcd.setCursor(0, 1);
// Set motor pins as output
pinMode(motorPinAPlus, OUTPUT);
pinMode(motorPinAMinus, OUTPUT);
pinMode(motorPinBPlus, OUTPUT);
pinMode(motorPinBMinus, OUTPUT);
}
void loop() {
static String input = "";
char key = keypad.getKey();
if (key) {
if (key == '#') { // '#' key indicates end of input
parseInput(input);
input = "";
} else if (key == '*') { // '*' key clears the input
input = "";
lcd.setCursor(0, 1);
lcd.print(" "); // Clear line
lcd.setCursor(0, 1);
} else {
input += key;
lcd.print(key);
}
}
if (inputCompleted) {
rotateStepper(degree);
inputCompleted = false; // Reset for next input
}
}
void parseInput(String input) {
int delimiter1 = input.indexOf(',');
int delimiter2 = input.lastIndexOf(',');
if (delimiter1 > 0 && delimiter2 > delimiter1) {
degree = input.substring(0, delimiter1).toInt();
minute = input.substring(delimiter1 + 1, delimiter2).toInt();
second = input.substring(delimiter2 + 1).toInt();
lcd.setCursor(0, 2);
lcd.print("Degree: ");
lcd.print(degree);
lcd.print(" Min: ");
lcd.print(minute);
lcd.print(" Sec: ");
lcd.print(second);
inputCompleted = true;
} else {
lcd.setCursor(0, 2);
lcd.print("Invalid Input");
}
}
void rotateStepper(int degrees) {
int steps = map(degrees, 0, 360, 0, 200); // Example mapping (200 steps per rev)
for (int i = 0; i < steps; i++) {
// Step sequence for bipolar motor (full stepping)
digitalWrite(motorPinAPlus, HIGH);
digitalWrite(motorPinAMinus, LOW);
digitalWrite(motorPinBPlus, HIGH);
digitalWrite(motorPinBMinus, LOW);
delay(10);
digitalWrite(motorPinAPlus, LOW);
digitalWrite(motorPinAMinus, HIGH);
digitalWrite(motorPinBPlus, HIGH);
digitalWrite(motorPinBMinus, LOW);
delay(10);
digitalWrite(motorPinAPlus, LOW);
digitalWrite(motorPinAMinus, HIGH);
digitalWrite(motorPinBPlus, LOW);
digitalWrite(motorPinBMinus, HIGH);
delay(10);
digitalWrite(motorPinAPlus, HIGH);
digitalWrite(motorPinAMinus, LOW);
digitalWrite(motorPinBPlus, LOW);
digitalWrite(motorPinBMinus, HIGH);
delay(10);
}
}