Hello I need your help with my project.
my project has an option to set alarm clock. I'm using Arduino mega 2560, matrix keypad 4x4, lcd 16x2 and rtc ds1302.
My question is: how can I recive input from the keypad to set the alarm time?
this is my code:
Please ignore the other functions, help me after the state is 3 (after I'm pressing C).
#include <DS1302.h>
DS1302 rtc(11, 12, 13); // (CE, I/O, CLK)
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7); // (rs, e, d4, d5, d6, d7)
#include <Keypad.h>
char keys[4][4]={
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}};
byte colPin[4]={38,39,40,41};
byte rowPin[4]={42,43,44,45};
Keypad keypad=Keypad(makeKeymap(keys),rowPin,colPin,4,4);
int enA=22,enB=23,in1=24,in2=25,in3=26,in4=27;
int h=0, m=0; //hour and minute
void setup()
{
rtc.halt(false); // Set the clock to run-mode, and disable the write protection
rtc.writeProtect(false);
//rtc.setTime(12, 30, 0); // Set the time
//rtc.setDate(15, 5, 2018); // Set the date
lcd.begin(16, 2); // Setup LCD to 16x2 characters
lcd.clear();
lcd.setCursor(4, 0);
lcd.print("Welcome!");
pinMode(enA, OUTPUT); //ena
pinMode(enB, OUTPUT); //enb
pinMode(in1, OUTPUT); //in1
pinMode(in2, OUTPUT); //in2
pinMode(in3, OUTPUT); //in3
pinMode(in4, OUTPUT); //in4
digitalWrite(enA, HIGH);
digitalWrite(enB, HIGH);
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
delay(1500);
digitalWrite(enA, LOW);
digitalWrite(enB, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
}
void loop()
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(rtc.getTimeStr()); //print time
lcd.print(" Press A");
lcd.setCursor(0, 1);
lcd.print(rtc.getDateStr()); //print date
delay (1000);
char pressed = keypad.getKey(); //Read the keypad
int state=0, act=0, b=0;
if(pressed == 'A')
{
state = 1;
func1();
while (state == 1)
{
char keyB = keypad.getKey();
if(keyB == 'B'){
state = 2;
}
if(keyB == 'C'){
state = 3;
}
if(keyB == '#')
state = 0;
}
if(state == 2)
feed();
if(state == 3)
{
act= 1;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Set Time: __:__");
lcd.setCursor(0, 1);
lcd.print("*.Ok #.Cancel");
while (act == 1)
{
char keyC = keypad.getKey();
if(keyC == '#')
act = 0;
}
}
}
}
void func1(void)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("B. Feed Now");
lcd.setCursor(0, 1);
lcd.print("C. Set Time");
return;
}
void feed(void)
{
lcd.clear();
lcd.setCursor(3,0);
lcd.print("Feeding...");
digitalWrite(enA, HIGH); //motor a
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
delay(2000);
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
delay(2000);
digitalWrite(enA, LOW);
digitalWrite(in2, LOW);
digitalWrite(enB, HIGH); // motor b
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
delay(2000);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
delay(2000);
digitalWrite(enB, LOW);
digitalWrite(in3, LOW);
return;
}