#include <LiquidCrystal_I2C.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal_I2C lcd(0x27, 16, 2);
#include <Keypad.h>//header for keypad commands enabling
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Three columns
int interruptPin = 2;
String spd = ""; // To get the speed sting value from the keypad
int Speed; // Variable to convert string to integer
int j = 13;
int i = 0;
// Define the Keymap for Keypad
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = {28, 30, 32, 34};
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = {42, 44, 46, 48};
// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup()
{
// Set the outputs for Stepper motor
for (int k = 13; k >= 10; k--)
{
pinMode(k, OUTPUT); //pins 8-14 are enabled as output
}
lcd.begin(16, 2);//initializing LCD
}
void loop()
{
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Direction");
// Main subroutine
start();
}
void motor_right()
{
while (1)
{
for (int k = 10; k <= 13; k++)
{
digitalWrite(k, HIGH);
delay(Speed);
digitalWrite(k, LOW);
}
char key4 = kpd.getKey();
if (key4 == '*')
return loop();
}
}
// Function for left rotation of the motor
void motor_left()
{
while (1)
{
for (int k = 13; k >= 10; k--)
{
digitalWrite(k, HIGH);
delay(Speed);
digitalWrite(k, LOW);
}
char key3 = kpd.getKey();
if (key3 == '*')
{
return loop();
}
}
}
// Function for right rotation of the motor
// Main function
void start()
{
spd = "";
Speed = 0;
while (1)
{
char key = kpd.getKey(); //storing pressed key value in a char
if (key != NO_KEY)
{
//For Right rotation
if (key == 'B')
{
key = NO_KEY;
while (1)
{
lcd.setCursor(12, 0);
lcd.print("Right");
delay(500);
lcd.setCursor(0, 1);
lcd.print("Speed");
while (1)
{
char key2 = kpd.getKey();
if (key2 != NO_KEY)
{
lcd.setCursor(j, 1);
lcd.print(key2);
spd += (char)key2; // Get the speed string value to "spd" variable
Speed = spd.toInt(); // Convert the stirng value to an integer & sorte into the "Speed" variable
j++;
if (key2 == '#') // Condition for left roatation
{
key2 = NO_KEY;
motor_right();
}
}
}
}
}
//For Left rotation
if (key == 'A')
{
key = NO_KEY; // Clear the pressed key data
while (1)
{
lcd.setCursor(12, 0);
lcd.print("Left");
delay(500);
lcd.setCursor(0, 1);
lcd.print("Speed");
while (1)
{
char key2 = kpd.getKey();
if (key2 != NO_KEY)
{
lcd.setCursor(j, 1);
lcd.print(key2);
spd += (char)key2; // Get the speed string value to "spd" variable
Speed = spd.toInt(); // Convert the stirng value to an integer & sorte into the "Speed" variable
j++;
if (key2 == '#') // Condition for left roatation
{
key2 = NO_KEY;
motor_left();
}
}
}
}
}
}
}
}