#include <LiquidCrystal.h>
#include <Keypad.h>
//used pind - digital 3,4,5,6,7,8,9,22,23,24,26,28,30,32
//Defining Numpad Configuration
const byte KEYPAD_ROWS = 4;
const byte KEYPAD_COLS = 4;
char keys[KEYPAD_ROWS][KEYPAD_COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'.', '0', '#', 'D'}
};
byte rowPins[KEYPAD_ROWS] = {9, 8, 7, 6};
byte colPins[KEYPAD_COLS] = {5, 4, 3, 2};
Keypad customKeypad = Keypad(makeKeymap(keys), rowPins, colPins, KEYPAD_ROWS, KEYPAD_COLS);
// Defining the LCD library with arduino pins for related lcd pins
const int rs = 22, en = 24, d4 = 26, d5 = 28, d6 = 30, d7 = 32;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
//Definign Digital Meter read output pin
int meter_pin = 23;
//Defining domestic circuit pins arduino
int Light_Bulb_Room = 31;
int Fan_Room = 33;
int Meter_output=0;
//PIR sensor
int ledPin = 11; // choose the pin for the LED
int inputPin = 10; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
//ldr
int LDR_PIN=25;
int Outdoor_light_pin=27;
int LDRState = 0; // this is to identify the change of state of the LDR detected light
//other
int lcd_message_type = 0; // this ditermines what should be printed on the lcd at the moment
String keypad_input;
String unit_charge[4]={"42","50","75","10000"};
int bill_amount=0;
void setup() {
Serial.begin(9600);
for (int pin_no=3;pin_no<10;pin_no++){
pinMode(pin_no,OUTPUT);
}
for (int pin_no=22;pin_no<33;pin_no=pin_no+2){
pinMode(pin_no,OUTPUT);
}
pinMode(meter_pin, INPUT);
digitalWrite(meter_pin,LOW);
pinMode(Light_Bulb_Room,OUTPUT);
pinMode(Fan_Room,OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(inputPin, INPUT);
pinMode(LDR_PIN, INPUT);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("Hello");
}
void loop() {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
Meter_output+=digitalRead(meter_pin);
bill_amount_func(Meter_output);
if (bill_amount>unit_charge[3].toInt()){
lcd_message_type = 3;
}
//keypad starts...................
char customKey = customKeypad.getKey();
if (customKey){
tariff(customKey, unit_charge);
}
//The process of PIR sensor in the below codes
val = digitalRead(inputPin); // read input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, HIGH); // turn LED ON
if (pirState == LOW) {
// we have just turned on
digitalWrite(Light_Bulb_Room,HIGH);//turn on the light
digitalWrite(Light_Bulb_Room,LOW);//turn on the Fan
lcd_message_type = 1;
// We only want to print on the output change, not state
pirState = HIGH;
}
} else {
digitalWrite(ledPin, LOW); // turn LED OFF
if (pirState == HIGH) {
// we have just turned of
lcd_message_type = 2;
// We only want to print on the output change, not state
pirState = LOW;
}
}
if (digitalRead(LDR_PIN) == LOW) {
digitalWrite(Outdoor_light_pin, LOW);
if (LDRState==1){
lcd_message_type=5;
}
} else {
digitalWrite(Outdoor_light_pin, HIGH);
if (LDRState==0){
lcd_message_type=4;
}
}
//lcd print
if (lcd_message_type==0){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Meter");
lcd.setCursor(6,0);
lcd.print(Meter_output);
lcd.setCursor(0,1);
lcd.print("Bill");
lcd.setCursor(5,1);
lcd.print(bill_amount);
}
else if(lcd_message_type==1){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Motion detected!");
lcd.setCursor(0, 1);
lcd.print("Light On");
delay(3000);
lcd_message_type = 0;
}
else if(lcd_message_type==2){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Motion ended!");
lcd.setCursor(0, 1);
lcd.print("Light Off");
delay(3000);
lcd_message_type = 0;
}
else if(lcd_message_type==3){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Red Notice");
lcd.setCursor(0, 1);
lcd.print("Bill Max Exceeded");
}
else if(lcd_message_type==4){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("LDR Detected");
lcd.setCursor(0,1);
lcd.print("Light ON");
delay(2000);
LDRState=1;
lcd_message_type=0;
}
else if(lcd_message_type==5){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("LDR Detected");
lcd.setCursor(0,1);
lcd.print("Light OFF");
delay(2000);
LDRState=0;
lcd_message_type=0;
}
delay(100); //to reduce the lcd blinking
}
void tariff(char Letter, String Unit_Charge_Array[]){
if (Letter=='A'){
char key=customKeypad.getKey();
while(key!='#'){
if(key){
keypad_input+=key;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("A");
lcd.setCursor(0, 1);
lcd.print(keypad_input);
Unit_Charge_Array[0]=keypad_input;
}
key=customKeypad.getKey();
}
keypad_input="";
lcd_message_type = 0;
}
else if (Letter=='B'){
char key=customKeypad.getKey();
while(key!='#'){
if(key){
keypad_input+=key;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("B");
lcd.setCursor(0, 1);
lcd.print(keypad_input);
Unit_Charge_Array[1]=keypad_input;
}
key=customKeypad.getKey();
}
keypad_input="";
lcd_message_type = 0;
}
else if (Letter=='C'){
char key=customKeypad.getKey();
while(key!='#'){
if(key){
keypad_input+=key;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print(keypad_input);
Unit_Charge_Array[2]=keypad_input;
}
key=customKeypad.getKey();
}
keypad_input="";
lcd_message_type = 0;
}
else if (Letter=='D'){
char key=customKeypad.getKey();
while(key!='#'){
if(key){
keypad_input+=key;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("D");
lcd.setCursor(0, 1);
lcd.print(keypad_input);
Unit_Charge_Array[3]=keypad_input;
}
key=customKeypad.getKey();
}
keypad_input="";
lcd_message_type = 0;
}
}
void bill_amount_func(int Meter_Reading){
if (Meter_Reading>120){
bill_amount=(Meter_Reading-120)* (unit_charge[2].toInt());
bill_amount+=(120-60)*(unit_charge[1].toInt());
bill_amount+=(60-0)*(unit_charge[0].toInt());
}
else if (Meter_Reading>60){
bill_amount=(Meter_Reading-60)*(unit_charge[1].toInt());
bill_amount+=(60-0)*(unit_charge[0].toInt());
}
else{
bill_amount=(Meter_Reading)*(unit_charge[0].toInt());
}
}