#include <Keypad.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(4, 5, 6, 7, 12, 13);
const int motorPins[4] = {8, 9, 10, 11};
const int steps[4][4] = {
{1, 0, 1, 0},
{0, 1, 1, 0},
{0, 1, 0, 1},
{1, 0, 0, 1}
};
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, 2, 3};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void stepMotor(int direction, int stepsCount) {
for (int i = 0; i < stepsCount; i++) {
for (int step = 0; step < 4; step++) {
int actualStep = direction > 0 ? step : 3 - step;
for (int pin = 0; pin < 4; pin++) {
digitalWrite(motorPins[pin], steps[actualStep][pin]);
}
delay(5);
}
}
}
void displayDirection(const char* direction) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Rotating:");
lcd.setCursor(0, 1);
lcd.print(direction);
}
void setup() {
for (int i = 0; i < 4; i++) {
pinMode(motorPins[i], OUTPUT);
}
lcd.begin(16, 2);
lcd.print("Ready");
Serial.begin(9600);
}
void loop() {
char key = keypad.getKey();
if (key) {
Serial.print("Key Pressed: ");
Serial.println(key);
if (key == 'A') {
displayDirection("5s Roatation");
stepMotor(1, 3* 200);
} else if (key == 'B') {
displayDirection("Clockwise");
stepMotor(1, 100);
} else if (key == 'C') {
displayDirection("Anticlockwise");
stepMotor(-1, 100);
} else if (key == 'D') {
displayDirection("Clock and Anti");
stepMotor(1, 100);
delay(200);
stepMotor(-1, 100);
}
}
}