#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
//#include "TinyGPS.h"
#include <time.h>
#define RXD2 16
#define TXD2 17
#define LED_PIN 23
#define BUZZER_PIN 25
// The TinyGPSPlus object
//TinyGPSPlus gps;
LiquidCrystal_I2C lcd(0x27,16, 2); // I2C address 0x27, 16 column and 2 rows
#define ROWS 4
#define COLS 3
char keyMap[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
uint8_t rowPins[ROWS] = {33, 32, 35, 34}; // R1,R2,R3,R4
uint8_t colPins[COLS] = {14, 27, 26}; //C1,C2,C3
Keypad keypad = Keypad(makeKeymap(keyMap), rowPins, colPins, ROWS, COLS );
double minLat = 3.147973;
double maxLat = 3.148027;
double minLon = 101.712973;
double maxLon = 101.713027;
bool alertTriggered = false;
// Simulated data for gps_tx_data
double Lat=3.148003,Lon= 101.712999;
double Lat1=3.14999,Lon1= 101.713127;
String InputStr = "";
char key;
int cc=0;
int tt=0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
lcd.init(); // initialize the lcd
lcd.backlight();
lcd.setCursor(0, 0);
pinMode(LED_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
// Clear The Display
//lcd.clear();
}
void loop() {
// put your main code here, to run repeatedly:
double lans,lons;
if (!alertTriggered) {
if(cc>2)
{
lans=Lat1 ;
lons=Lon1;
}
else
{
lans=Lat;
lons=Lon;
}
lcd.setCursor(0, 0);
lcd.print("Lat: ");
lcd.print(lans, 6);
Serial.println("Lat:");
Serial.print(lans, 6);
Serial.print(" , Lng:");
Serial.println(lons, 6);
lcd.setCursor(0, 1);
lcd.print("Lng: ");
lcd.print(lons, 6);
// Check if alert condition is met
if (lans < minLat || lans > maxLat || lons < minLon || lons > maxLon) {
if (!alertTriggered) {
triggerAlert();
alertTriggered = true;
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("OUT OF RANGE!");
}
}
cc++;
delay(1000);
}
else if(alertTriggered)
{
key = keypad.getKey();
if(key=='7' ||key=='*')
Keypad keypad = Keypad(makeKeymap(keyMap), rowPins, colPins, ROWS, COLS );
else if (key !=NO_KEY) {
Serial.println(key);
if(key=='#')
{
if(InputStr=="1234")
{
digitalWrite(LED_PIN, LOW);
digitalWrite(BUZZER_PIN, LOW);
noTone(BUZZER_PIN);
alertTriggered = false;
InputStr="";
cc=0;
}
else
InputStr="";
}
else{
InputStr.concat(key);
Serial.println(InputStr);}
}
}
/*
while (Serial.available() > 0)
if (gps.encode(Serial.read()))
displayInfo();
*/
// Process each character of gps_tx_data
}
void triggerAlert() {
// Activate alert (e.g., sound buzzer, blink LED)
digitalWrite(LED_PIN, HIGH);
//digitalWrite(BUZZER_PIN, HIGH);
tone(BUZZER_PIN, 1000);
/*
delay(2000);
digitalWrite(LED_PIN, LOW);
noTone(BUZZER_PIN);
*/
}