#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <Stepper.h>
//#include <AccelStepper.h>
//LCD:
LiquidCrystal_I2C lcd(0x27, 16, 2);
//KEYPAD:
const byte ROWS = 4;
const byte COLS = 4;
byte rowPins[ROWS] = {28, 30, 32, 34};
byte colPins[COLS] = {42, 44, 46, 48};
char keys[ROWS][COLS] = {
{'1', '2', '3', 'U'},
{'4', '5', '6', 'L'},
{'7', '8', '9', 'R'},
{'T', '0', 'S', 'D'}
};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
//TIMER:
char currentTimeValue[4];
int currentStateTime = 1;
int timerSeconds = 0;
int lpcnt = 0;
//RELAY & LED:
#define L1 6
int RLY1 = LOW;
#define L2 7
int RLY2 = LOW;
//STEPPER MOTOR:
enum state {
SPEED,
OPERATION,
SUCTION,
PUMPING,
TIMER
};
state currentState = SPEED;
String speedValue = " ";
int Speed;
int dirPin = 8;
int stepPin = 9;
const int stepsPerRevolution = 800;
Stepper myStepper(stepsPerRevolution, 8, 9);
void setup(){
lcd.init();
lcd.backlight();
displayCodeEntryScreen();
currentTimeValue[0] = '0';
currentTimeValue[1] = '0';
currentTimeValue[2] = '0';
currentTimeValue[3] = '0';
showEnteredTime();
pinMode(L1, OUTPUT);
pinMode(L2, OUTPUT);
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
digitalWrite(dirPin, HIGH);
}
void loop(){
int l;
char tempVal[3];
char key = keypad.getKey();
if (int(key) != 0 and currentStateTime == 1){
switch (key){
case 'T':
relayStatus(false);
relayStatus2(true);
stepper(false);
currentTimeValue[0] = '0';
currentTimeValue[1] = '0';
currentTimeValue[2] = '0';
currentTimeValue[3] = '0';
showEnteredTime();
currentStateTime = 1;
lpcnt = 0;
timerSeconds = 0;
break;
case 'S':
tempVal[0] = currentTimeValue[0];
tempVal[1] = currentTimeValue[1];
tempVal[2] = 0;
timerSeconds = atol(tempVal) * 60;
tempVal[0] = currentTimeValue[2];
tempVal[1] = currentTimeValue[3];
tempVal[2] = 0;
timerSeconds = timerSeconds + atol(tempVal);
currentStateTime = 2;
break;
default:
currentTimeValue[0] = currentTimeValue[1];
currentTimeValue[1] = currentTimeValue[2];
currentTimeValue[2] = currentTimeValue[3];
currentTimeValue[3] = key;
showEnteredTime();
break;
}
}
if (currentStateTime == 2){
if (int(key) != 0){
if(key == 'T'){
digitalWrite(L1, LOW);
clearScreen();
lcd.setCursor(0,0);
lcd.print("MOTOR STOPPED...");
digitalWrite(L2, HIGH);
delay(500);
digitalWrite(L2, LOW);
delay(500);
digitalWrite(L2, HIGH);
delay(500);
digitalWrite(L2, LOW);
delay(500);
digitalWrite(L2, HIGH);
delay(500);
digitalWrite(L2, LOW);
delay(500);
digitalWrite(L2, HIGH);
delay(500);
digitalWrite(L2, LOW);
delay(500);
relayStatus(false);
relayStatus2(true);
stepper(false);
displayCodeEntryScreen();
currentTimeValue[0] = '0';
currentTimeValue[1] = '0';
currentTimeValue[2] = '0';
currentTimeValue[3] = '0';
showEnteredTime();
currentStateTime = 1;
lpcnt = 0;
timerSeconds = 0;
digitalWrite(L2, HIGH);
delay(500);
digitalWrite(L2, LOW);
}
}
else {
if(lpcnt > 9){
lpcnt = 0;
--timerSeconds;
showCountdown();
if (timerSeconds <= 0){
currentStateTime = 1;
relayStatus(false);
relayStatus2(true);
stepper(false);
displayCodeEntryScreen();
showEnteredTime();
}
else {
relayStatus(true);
relayStatus2(false);
stepper(true);
}
}
++lpcnt;
delay(100);
}
}
}
void showEnteredTime(){
lcd.setCursor(5,1);
lcd.print(currentTimeValue[0]);
lcd.print(currentTimeValue[1]);
lcd.print(":");
lcd.print(currentTimeValue[2]);
lcd.print(currentTimeValue[3]);
}
void relayStatus(bool status){
if (status){
digitalWrite(L1, HIGH);
} else{
digitalWrite(L1, LOW);
}
}
void relayStatus2(bool status){
if (status)
digitalWrite(L2, HIGH);
else
digitalWrite(L2, LOW);
}
void stepper(bool status){
if (status){
myStepper.setSpeed(10);
for (int x = 0; stepsPerRevolution <= 800; x++ ){
digitalWrite(dirPin, HIGH);
digitalWrite(stepPin, HIGH);
delay(100);
digitalWrite(stepPin, LOW);
myStepper.step(stepsPerRevolution);
}
}
else {
myStepper.setSpeed(10);
for (int x = 800; stepsPerRevolution >= 0; x-- ){
digitalWrite(dirPin, LOW);
digitalWrite(stepPin, HIGH);
delay(100);
digitalWrite(stepPin, LOW);
myStepper.step(stepsPerRevolution);
}
}
}
void showCountdown(){
char timest[6];
lcd.setCursor(0,0);
lcd.print("MOTOR IS RUNNING");
lcd.setCursor(2,1);
lcd.print("** ");
sprintf(timest, "%d:%.2d" , (timerSeconds/60), (timerSeconds - ((timerSeconds/60)*60)));
lcd.print(timest);
lcd.print(" **");
}
void displayCodeEntryScreen(){
clearScreen();
lcd.setCursor(0,0);
lcd.print("SET THE TIME:");
}
void clearScreen(){
lcd.setCursor(0,0);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print(" ");
}