Code: Select all
#include <Adafruit_GPS.h>
#define GPSSerial Serial1 //Hardware serial port
#include <LiquidCrystal_I2C.h>
Adafruit_GPS GPS(&GPSSerial); //Connect to GPS unit
//initalise time and date verables
int hour;
int minute;
byte day;
byte month;
byte year;
// set the LCD number of columns and rows
int lcdColumns = 16;
int lcdRows = 2;
// set LCD address, number of columns and rows
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);
// Set GPSECHO to 'false' to turn off echoing the GPS data to the Serial console
// Set to 'true' if you want to debug and listen to the raw GPS sentences
#define GPSECHO false
uint32_t timer = millis();
//print out the current GPS location
void print_GPS(){
lcd.clear();//Clear display
lcd.setCursor(0,0);//set cursor to position 0, line 0
lcd.print("GPS:");//print GPS
lcd.setCursor(5,0);//Move cursor
lcd.print(GPS.latitude);//print Latitude
lcd.setCursor(12,0);//move cursor
lcd.print(GPS.lat);//print north or south
lcd.setCursor(5, 1);//move cursor
lcd.print(GPS.longitude);//print longitude
lcd.setCursor(12, 1);//move cursor
lcd.print(GPS.lon);//print east or west
delay(5000);//keep location displayed for 5 seconds
lcd.clear();//clear display
}
//print the last detection time and date
void print_date(){
lcd.clear();//clear display
lcd.setCursor(0,0);//set cursor
lcd.print("Date: ");// print DATE:
lcd.setCursor(6, 0);//move cursor
lcd.print(month);//print month in 00 format
lcd.setCursor(8, 0);//move cursor
lcd.print("/");//print /
lcd.setCursor(9, 0);//move cursor
lcd.print(day);//print day in 00 format
lcd.setCursor(11, 0);//move cursor
lcd.print("/");//print /
lcd.setCursor(12, 0);//move cursor
lcd.print(year);//print year in 0000 format
lcd.setCursor(0, 1);//move cursor
lcd.print("Time:");//print Time:
lcd.setCursor(6, 1);//move cursor
lcd.print(hour);//print hour
lcd.setCursor(8, 1);//move cursor
lcd.print(":");//print :
lcd.setCursor(9, 1);//move cursor
lcd.print(minute);//print minute
}
//setup code
void setup() {
Serial.begin(9600);//
pinMode(A4, INPUT_PULLDOWN); // RF ADC
pinMode(A1, INPUT_PULLDOWN); //Light Detector Input
pinMode(A2, INPUT_PULLDOWN); //Trigger for GPS Location
pinMode(13, OUTPUT);//All Clear LED (Green)
pinMode(12, OUTPUT);//RF detection LED (Yellow)
pinMode(11, OUTPUT);//Lightning flash detection LED(Blue)
pinMode(10, OUTPUT);//Detection confermed LED(Red)
GPS.begin(9600);//// 9600 NMEA is the default baud rate for Adafruit MTK GPS
GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);// turn on only the "minimum recommended" data
GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); // 1 Hz update rate
GPS.sendCommand(PGCMD_ANTENNA);// Request updates on antenna status
// initialize LCD
lcd.init();
// turn on LCD backlight
lcd.backlight();
//Print Boot Message
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Lightning");
lcd.setCursor(0,1);
lcd.print("Detection");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("System");
delay(2000);
lcd.clear();
delay(1000);//keep message displayed
}
void loop() {
// put your main code here, to run repeatedly:
int x = analogRead(A4);//ADC for RF detection
double v = (x*3.3)/4095;//Voltage at ADC conversion
bool b = digitalRead(A1);//read pin 15 for light detection
double vThres = 1.0; //V theshold for RF Detector
//manual trigger read GPS location
if (digitalRead(A2)){
char c = GPS.read();//read data frin GPS main loop
if (GPSECHO){
if (c) Serial.print(c);
}
if (GPS.newNMEAreceived()) // print the NMEA sentence, or data, cannot listen and catching new sentences
{
Serial.print(GPS.lastNMEA()); // this also sets the newNMEAreceived() flag to false
if (!GPS.parse(GPS.lastNMEA())) // this also sets the newNMEAreceived() flag to false
return; // we can fail to parse a sentence in which case we should just wait for another
}
//Print GPS location data to lcd
if (GPS.fix) {
Serial.print("Location: ");
Serial.print(GPS.latitude, 4); Serial.print(GPS.lat);
Serial.print(", ");
Serial.print(GPS.longitude, 4); Serial.println(GPS.lon);
Serial.print("Speed (knots): "); Serial.println(GPS.speed);
Serial.print("Angle: "); Serial.println(GPS.angle);
Serial.print("Altitude: "); Serial.println(GPS.altitude);
Serial.print("Satellites: "); Serial.println((int)GPS.satellites);
Serial.print("Antenna status: "); Serial.println((int)GPS.antenna);
print_GPS();
}
print_date();//Reprint last confermed detection
Serial.println(GPS.milliseconds);
Serial.print("Date: ");
Serial.print(GPS.day, DEC); Serial.print('/');
Serial.print(GPS.month, DEC); Serial.print("/20");
Serial.println(GPS.year, DEC);
Serial.print("Fix: "); Serial.print((int)GPS.fix);
Serial.print(" quality: "); Serial.println((int)GPS.fixquality);
}
//LED and detection logic
if (v > vThres && digitalRead(A1) == HIGH){//RF detected but no flash
digitalWrite(13, LOW);//Turn off Green LED
digitalWrite(12, HIGH);//Turn on Yellow LED
Serial.println("RF Detection. Possible Lightning in the Area");
//lcd display message
lcd.clear();
lcd.setCursor(0,0);
lcd.print("RF DETECTION IN");
lcd.setCursor(0,1);
lcd.print("AREA.");
delay(3000);
print_GPS();
digitalWrite(12, LOW);//Turn off RF detection LED (Yellow)
digitalWrite(13, HIGH);//All clear LED (Green)
lcd.clear();
print_date();//Reprint last confermed detection
}
if (digitalRead(A1) == LOW && v < vThres){//Flash but no RF
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Flash DETECTION");
digitalWrite(13, LOW);//Turn off Green LED
digitalWrite(11, HIGH);//Turn on flash detection LED (Blue)
Serial.println("Flash Detection");
delay(3000);//Delay to reduce detecting the same signal more than once
print_GPS();
digitalWrite(11, LOW);//FLash detection LED (Blue)
digitalWrite(13, HIGH);//All clear LED (Green)
lcd.clear();
print_date();//Reprint last confermed detection
}
if (v > vThres && digitalRead(A1) == LOW){//Both flash and RF detected
minute = GPS.minute;
hour = GPS.hour-4;
day = GPS.day;
month = GPS.month;
year = GPS.year;
digitalWrite(13, LOW);//Turn off Green LED
digitalWrite(10, HIGH);//Turn on lightning confermed LED (Red)
Serial.println("Lighting is in the Area, Seek Shelter!");
delay(3000);//Delay to reduce detecting the same signal more than once
print_GPS();
digitalWrite(10, LOW);//Turn off lightning confermed LED (Red)
digitalWrite(13, HIGH);//All clear LED (Green)
print_date();
}
}