Created
March 19, 2020 15:55
-
-
Save sashuu69/052677ae341847ece757eefde43f4f0e to your computer and use it in GitHub Desktop.
GPS ALARM CLOCK
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <TinyGPS++.h> | |
| #include <SoftwareSerial.h> | |
| #include <LiquidCrystal_I2C.h> | |
| #include "SD.h" | |
| #define SD_ChipSelectPin 10 | |
| #include "TMRpcm.h" | |
| #include "SPI.h" | |
| LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display | |
| TinyGPSPlus gps; // The TinyGPS++ object | |
| SoftwareSerial ss(4, 5); // The serial connection to the GPS device | |
| TMRpcm tmrpcm; | |
| /* For GPS */ | |
| float latitude , longitude; | |
| int year , month , date, hour , minute , second; | |
| String date_str , time_str , lat_str , lng_str; | |
| /* For Buttons */ | |
| int P1=6; // Button SET MENU' | |
| int P2=7; // Button + | |
| int P3=8; // Button - | |
| int P4=9; // SWITCH Alarm | |
| /* Function definitions */ | |
| void bootScreen(); // Boot screen of device | |
| void loadingScreen(); // Loading screen for GPS | |
| void getInfoFromGPS(); // Get date, time, latitude and longitude from GPS | |
| void displayData(); // Display date, time, latitude and longitude on LCD | |
| void theInitialiser(); // Standard display | |
| void theAlarmActivator(); // Alarm activator UI for LCD | |
| void setHourUI(); // Alarm hour setter | |
| void setMinuteUI(); // Alarm minute setter | |
| void alarmTrigger(); // Alarm trigger | |
| void player(); // Play audio from SDCard | |
| /* For UI */ | |
| bool alarmStatus = false; // Alarm turn on / off | |
| int menu = 0; // menu handler | |
| int tempHr = 0, tempMin = 0; | |
| String alarmTime = ""; | |
| void setup() { | |
| // put your setup code here, to run once: | |
| Serial.begin(115200); | |
| ss.begin(9600); | |
| lcd.init(); | |
| lcd.backlight(); | |
| pinMode(P1,INPUT_PULLUP); | |
| pinMode(P2,INPUT_PULLUP); | |
| pinMode(P3,INPUT_PULLUP); | |
| pinMode(P4,INPUT_PULLUP); | |
| bootScreen(); | |
| } | |
| void loop() { | |
| // put your main code here, to run repeatedly: | |
| if(digitalRead(P1)==LOW) { | |
| menu += 1; | |
| } | |
| if (menu == 0) { | |
| // Default face | |
| theInitialiser(); | |
| } else if (menu == 1) { | |
| theAlarmActivator(); | |
| } else if (menu == 2) { | |
| setHourUI(); | |
| } else if (menu == 3) { | |
| setMinuteUI(); | |
| } else if (menu == 4) { | |
| menu = 0; | |
| } | |
| delay(400); | |
| } | |
| // Function for Boot screen of device | |
| void bootScreen() { | |
| Serial.println("###############################"); | |
| Serial.println("GPS Alarm Clock"); | |
| Serial.println("Project by Asha Mary Kuriakose."); | |
| Serial.println("###############################\n"); | |
| lcd.clear(); | |
| lcd.setCursor(2,0); | |
| lcd.print("GPS Alarm Clock"); | |
| lcd.setCursor(5,1); | |
| lcd.print("---------"); | |
| lcd.setCursor(5,2); | |
| lcd.print("Project By,"); | |
| lcd.setCursor(0,3); | |
| lcd.print("Asha Mary Kuriakose"); | |
| delay(2000); | |
| } | |
| // Function for Standard display | |
| void theInitialiser() { | |
| Serial.println("################################"); | |
| Serial.println("Fetching data from GPS."); | |
| getInfoFromGPS(); | |
| if(lat_str == "") | |
| lat_str = "Fetching.."; | |
| if(lng_str == "") | |
| lng_str = "Fetching.."; | |
| if (date_str == "" || time_str == "") { | |
| loadingScreen(); | |
| } | |
| else { | |
| displayData(); | |
| } | |
| Serial.println("Data:-"); | |
| Serial.print("Time: ");Serial.println(time_str); | |
| Serial.print("Date: ");Serial.println(date_str); | |
| Serial.print("Latitude: ");Serial.println(lat_str); | |
| Serial.print("Longitude: ");Serial.println(lng_str); | |
| Serial.print("Alarm Status: ");Serial.println(alarmStatus); | |
| Serial.print("Alarm Time: ");Serial.println(alarmTime); | |
| Serial.println("################################\n"); | |
| } | |
| // Function for Loading screen for GPS | |
| void loadingScreen() { | |
| lcd.clear(); | |
| lcd.setCursor(5,0); | |
| lcd.print("Loading.."); | |
| lcd.setCursor(3,1); | |
| lcd.print("Searching for"); | |
| lcd.setCursor(6,2); | |
| lcd.print("nearest"); | |
| lcd.setCursor(5,3); | |
| lcd.print("Satellite.."); | |
| } | |
| // Function for Get date, time, latitude and longitude from GPS | |
| void getInfoFromGPS(){ | |
| while (ss.available() > 0) | |
| if (gps.encode(ss.read())) { | |
| if (gps.location.isValid()) { | |
| latitude = gps.location.lat(); | |
| lat_str = String(latitude , 6); | |
| longitude = gps.location.lng(); | |
| lng_str = String(longitude , 6); | |
| } | |
| if (gps.date.isValid()) { | |
| date_str = ""; | |
| date = gps.date.day(); | |
| month = gps.date.month(); | |
| year = gps.date.year(); | |
| if (date < 10) | |
| date_str = '0'; | |
| date_str += String(date); | |
| date_str += "/"; | |
| if (month < 10) | |
| date_str += '0'; | |
| date_str += String(month); | |
| date_str += "/"; | |
| if (year < 10) | |
| date_str += '0'; | |
| date_str += String(year); | |
| } | |
| if (gps.time.isValid()) { | |
| time_str = ""; | |
| hour = gps.time.hour(); | |
| minute = gps.time.minute(); | |
| second = gps.time.second(); | |
| if (hour < 10) | |
| time_str = '0'; | |
| time_str += String(hour); | |
| time_str += ":"; | |
| if (minute < 10) | |
| time_str += '0'; | |
| time_str += String(minute); | |
| time_str += ":"; | |
| if (second < 10) | |
| time_str += '0'; | |
| time_str += String(second); | |
| } | |
| } | |
| } | |
| // Function for Display date, time, latitude and longitude on LCD | |
| void displayData() { | |
| lcd.clear(); | |
| lcd.setCursor(0,0); | |
| lcd.print("DATE: " + date_str); | |
| lcd.setCursor(0,1); | |
| lcd.print("TIME: " + time_str); | |
| lcd.setCursor(0,2); | |
| lcd.print("LATI: " + lat_str); | |
| lcd.setCursor(0,3); | |
| lcd.print("LONG: " + lng_str); | |
| alarmTrigger(); | |
| } | |
| // Alarm activator UI for LCD | |
| void theAlarmActivator() { | |
| lcd.clear(); | |
| lcd.setCursor(0,0); | |
| if (alarmStatus == false) { | |
| lcd.print("Alarm: False"); | |
| } else { | |
| lcd.print("Alarm: True"); | |
| } | |
| if (digitalRead(P2)==LOW) { | |
| alarmStatus = true; | |
| } | |
| if (digitalRead(P3)==LOW) { | |
| alarmStatus = false; | |
| } | |
| if (digitalRead(P3)==LOW) { | |
| player(); | |
| } | |
| } | |
| // Function for Alarm hour setter | |
| void setHourUI() { | |
| lcd.clear(); | |
| lcd.setCursor(0,0); | |
| lcd.print("Set Alarm:-"); | |
| lcd.setCursor(0,1); | |
| lcd.print("***********"); | |
| lcd.setCursor(0,2); | |
| lcd.print("Set Hour"); | |
| lcd.setCursor(0,3); | |
| lcd.print(tempHr); | |
| // Minus | |
| if (digitalRead(P2)==LOW) { | |
| tempHr += 1; | |
| if (tempHr == 24) { | |
| tempHr = 0; | |
| } | |
| } | |
| // Plus | |
| if (digitalRead(P3)==LOW) { | |
| tempHr -= 1; | |
| if (tempHr == -1) { | |
| tempHr = 23; | |
| } | |
| } | |
| // Set Hour | |
| if (digitalRead(P4)==LOW) { | |
| if (tempHr < 10) { | |
| alarmTime += '0'; | |
| } | |
| alarmTime += String(tempHr); | |
| alarmTime += ":"; | |
| tempHr = 0; | |
| } | |
| } | |
| // Function for Alarm minute setter | |
| void setMinuteUI() { | |
| lcd.clear(); | |
| lcd.setCursor(0,0); | |
| lcd.print("Set Alarm:-"); | |
| lcd.setCursor(0,1); | |
| lcd.print("***********"); | |
| lcd.setCursor(0,2); | |
| lcd.print("Set Minute"); | |
| lcd.setCursor(0,3); | |
| lcd.print(tempHr); | |
| // Minus | |
| if (digitalRead(P2)==LOW) { | |
| if (tempMin == 60) { | |
| tempMin = 0; | |
| } | |
| tempMin += 1; | |
| } | |
| // Plus | |
| if (digitalRead(P3)==LOW) { | |
| tempMin = tempMin - 1; | |
| if (tempMin == -1) { | |
| tempMin = 59; | |
| } | |
| } | |
| // Set minute | |
| if (digitalRead(P4)==LOW) { | |
| if (tempMin < 10) { | |
| alarmTime += '0'; | |
| } | |
| alarmTime += String(tempMin); | |
| alarmTime += ":00"; | |
| tempMin = 0; | |
| } | |
| } | |
| void alarmTrigger() { | |
| if (time_str.compareTo(alarmTime)) { | |
| Serial.println("Alarm Trigger..."); | |
| player(); | |
| } | |
| } | |
| // Function for Play audio from SDCard | |
| void player() { | |
| tmrpcm.setVolume(6); | |
| tmrpcm.quality(1); | |
| tmrpcm.play("test.wav"); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment