#include <Wire.h>
#include <stdio.h>
#include <Keypad.h>
#include <Stepper.h>
#include <LiquidCrystal_I2C.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 :
int counter = 0;
int attempts = 0;
int max_attempts = 3;
String mymints;
int minutes = 0;
String mysecs;
int seconds = 0;
long int total_seconds = 0;
int secflag = 0;
int timer_started_flag = 0;
unsigned long previousMillis=0;
unsigned long int previoussecs = 0;
unsigned long int currentsecs = 0;
unsigned long currentMillis = 0;
int interval= 1 ;
int tsecs = 0;
//RELAY & LED:
#define L1 10
int RLY1 = LOW;
#define L2 11
int RLY2 = LOW;
//STEPPER MOTOR :
int dirPin = 8;
int stepPin = 9;
void setup() {
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
pinMode(L1, OUTPUT);
pinMode(L2, OUTPUT);
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
digitalWrite(dirPin, HIGH);
lcd.print("COUNTDOWN TIMER");
delay(2000);
lcd.clear();
lcd.print("ENTER MINUTES:");
}
void loop(){
keypadfunction();
}
void keypadfunction(){
char key = keypad.getKey();
if (key){
counter = counter + 1;
lcd.setCursor(counter, 1);
lcd.print(key);
}
if (key == '1') {
mymints = mymints + 1;
}
if (key == '2'){
mymints = mymints + 2;
}
if (key == '3'){
mymints = mymints + 3;
}
if (key == '4'){
mymints = mymints + 4;
}
if (key == '5'){
mymints = mymints + 5;
}
if (key == '6'){
mymints = mymints + 6;
}
if (key == '7'){
mymints = mymints + 7;
}
if (key == '8'){
mymints = mymints + 8;
}
if (key == '9'){
mymints = mymints + 9;
}
if (key == '0'){
mymints = mymints + 0;
}
if (key == 'S'){
counter = 0;
mymints = "";
minutes = 0;
mysecs = "";
seconds = 0;
secflag = 0;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("ENTER MINUTES:");
}
if (key == 'T'){
lcd.clear();
minutes = mymints.toInt();
lcd.clear();
lcd.print(" (SAVED MIN): ");
lcd.setCursor(7,1);
lcd.print(minutes);
mymints = "";
delay(2000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("ENTER SECOUNDS:");
counter = 0;
secflag = 1;
while(secflag == 1){
forSeconds();
}
}
}
void forSeconds(){
char key = keypad.getKey();
if (key){
counter = counter + 1;
lcd.setCursor(counter, 1);
lcd.print(key);
}
if (key == '1'){
mysecs = mysecs + 1;
}
if (key == '2'){
mysecs = mysecs + 2;
}
if (key == '3'){
mysecs = mysecs + 3;
}
if (key == '4'){
mysecs = mysecs + 4;
}
if (key == '5'){
mysecs = mysecs + 5;
}
if (key == '6'){
mysecs = mysecs + 6;
}
if (key == '7'){
mysecs = mysecs + 7;
}
if (key == '8'){
mysecs = mysecs + 8;
}
if (key == '9'){
mysecs = mysecs + 9;
}
if (key == '0'){
mysecs = mysecs + 0;
}
if (key == 'S'){
counter = 0;
mymints = "";
minutes = 0;
mysecs = "";
seconds = 0;
secflag = 0;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("ENTER MINUTES:");
}
if (key == 'T'){
lcd.clear();
seconds = mysecs.toInt();
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" (SAVED SEC): ");
lcd.setCursor(7,1);
lcd.print(seconds);
mysecs = "";
delay(2000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Mins Secs");
lcd.setCursor(1,1);
lcd.print(minutes);
lcd.setCursor(10,1);
lcd.print(seconds);
total_seconds = (minutes * 60) + seconds ;
counter = 0;
secflag = 0;
timer_started_flag = 1;
lcd.clear();
lcd.print("TOTAL SEC:");
lcd.setCursor(11,0);
lcd.print( total_seconds );
delay(3000);
while( timer_started_flag == 1){
char key = keypad.getKey();
if (key){
counter = counter + 1;
lcd.setCursor(counter, 1);
}
if (key == 'S'){
counter = 0;
mymints = "";
minutes = 0;
mysecs = "";
seconds = 0;
secflag = 0;
total_seconds = 0;
timer_started_flag = 0;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("ENTER MINUTES:");
}
//lcd.clear();
lcd.setCursor(0,0);
lcd.print("TOTAL SEC:");
lcd.setCursor(11,0);
lcd.print( total_seconds );
lcd.setCursor(0,1);
if( total_seconds > 0){
digitalWrite(L1, HIGH);
digitalWrite(L2, LOW);
lcd.print("MOTOR IS RUNNING");
stepper();
}
if( total_seconds <= 0){
total_seconds = 0;
digitalWrite(L1, LOW);
digitalWrite(L2, HIGH);
lcd.print("MOTOR STOPPED");
}
currentMillis = millis();
currentsecs = currentMillis / 1000;
if ((unsigned long)(currentsecs - previoussecs) >= interval) {
total_seconds = total_seconds - 1;
lcd.clear();
previoussecs = currentsecs;
}
}
}
}
void stepper(){
digitalWrite(dirPin, LOW);
digitalWrite(stepPin, HIGH);
delay(10);
digitalWrite(stepPin, LOW);
delay(10);
}