#include <Keypad.h> //library keypad
#include <DS3231.h> //library RTC ds3231
#include <Servo.h> //library servo
#include <Wire.h> //library LCD
#include <LiquidCrystal_I2C.h> //LCD with i2c
//keypad
const byte numRows= 4;
const byte numCols= 4;
char keymap[numRows][numCols]=
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
// pin keypad
byte rowPins[numRows]= {13,12,11,10};
byte colPins[numCols]= {9,8,7,6};
Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);
// pin RTC
DS3231 rtc(A0, A1);
// set the LCD address to 0x27 for a 16 chars 2 line display
// A FEW use address 0x3F
// Set the pins on the I2C chip used for LCD connections:
// addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
//servo
Servo servoMain; // Define our Servo
void setup()
{
Serial.begin(9600);
rtc.begin();
rtc.setTime(17,0,0);
lcd.begin(16,2);
servoMain.attach(5); // servo digital pin
}
void loop()
{
char keypressed = myKeypad.getKey();
if (keypressed != NO_KEY)
{
if (keypressed == 'A')
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print(keypressed);
lcd.print(" on : ");
lcd.print("05:00");
lcd.setCursor(1,1);
lcd.print(" off: ");
lcd.print("05:05");
if (rtc.getTimeStr() == '05:00:00')
{
servoMain.write(0);
}
}
if (keypressed == '*')
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print(rtc.getTimeStr());
lcd.setCursor(0,1);
lcd.print("Open Water Gate!!");
servoMain.write(0);
}
if(keypressed == '#')
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print(rtc.getTimeStr());
lcd.setCursor(0,1);
lcd.print("Close Water Gate!!");
servoMain.write(90);
delay(2000);
lcd.clear();
}
}
}