Advertisement
LeventeDaradici

GPS Clock Arduino - Arduino for beginners

Jan 3rd, 2022
2,533
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.88 KB | None | 0 0
  1.  
  2. #include <Wire.h>
  3. #include <LiquidCrystal_I2C.h>
  4. LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display > 0x3F in my case for a 2004 lcd display
  5. #include <Adafruit_GPS.h>
  6. #include <SoftwareSerial.h>
  7. // Connect the GPS Power pin to 5V
  8. // Connect the GPS Ground pin to ground
  9. // Connect the GPS TX (transmit) pin to Digital 4
  10. // Connect the GPS RX (receive) pin to Digital 3
  11. SoftwareSerial mySerial(4, 3);
  12. Adafruit_GPS GPS(&mySerial);
  13. #define GPSECHO  true
  14.  
  15. int timezone = +2; // select your time zone
  16.  
  17. void setup()
  18. {
  19.   lcd.init();                      
  20.   lcd.backlight();
  21.   lcd.setCursor(4,0);
  22.   lcd.print("GPS clock");
  23.   lcd.setCursor(0,1);
  24.   lcd.print("Levente Daradici");
  25.   delay(5000);
  26.   lcd.clear();
  27.   GPS.begin(9600);
  28.   GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
  29.   GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);  
  30.   GPS.sendCommand(PGCMD_ANTENNA);
  31.   delay(1000);
  32. }
  33.  
  34. uint32_t timer = millis();
  35. void loop()                    
  36. {
  37.   char c = GPS.read();
  38.   if ((c) && (GPSECHO))
  39.   if (GPS.newNMEAreceived())
  40.       {
  41.         if (!GPS.parse(GPS.lastNMEA()))
  42.       return;  
  43.       }
  44.  
  45.   if (timer > millis())  timer = millis();
  46.  
  47.   if (millis() - timer > 1000) {
  48.     timer = millis();
  49.  
  50.     lcd.setCursor(4,0);
  51.     int hour = (GPS.hour) + timezone;
  52.         if (hour < 10) { lcd.print('0'); }
  53.         lcd.print(hour, DEC); lcd.print(':');
  54.         if (GPS.minute < 10) { lcd.print('0'); }
  55.         lcd.print(GPS.minute, DEC); lcd.print(':');
  56.         if (GPS.seconds < 10) { lcd.print('0'); }
  57.         lcd.print(GPS.seconds, DEC);
  58.  
  59.     lcd.setCursor(3,1);
  60.         if (GPS.day < 10) { lcd.print('0'); }
  61.         lcd.print(GPS.day, DEC); lcd.print('.');
  62.         if (GPS.month < 10) { lcd.print('0'); }
  63.         lcd.print(GPS.month, DEC); lcd.print('.');
  64.         lcd.print("20");
  65.         lcd.print(GPS.year, DEC);
  66.    
  67.   }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement