pleasedontcode

# GPS Reader rev_02

Mar 6th, 2026
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 12.45 KB | None | 0 0
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: # GPS Reader
  13.     - Version: 002
  14.     - Source Code NOT compiled for: Arduino Nano
  15.     - Source Code created on: 2026-03-07 02:25:28
  16.  
  17. ********* Pleasedontcode.com **********/
  18.  
  19. /****** SYSTEM REQUIREMENTS *****/
  20. /****** SYSTEM REQUIREMENT 1 *****/
  21.     /* Read NEO-M8N GPS module data via UART (9600 baud), */
  22.     /* parse NMEA sentences, and display latitude, */
  23.     /* longitude, altitude, satellite count, and fix */
  24.     /* quality */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27.  
  28. #include <SoftwareSerial.h>
  29. #include <EasyButton.h>
  30. #include <TinyGPSPlus.h>
  31.  
  32. /****** FUNCTION PROTOTYPES *****/
  33. void setup(void);
  34. void loop(void);
  35. void displayGPSData(void);
  36. void onButtonPressed(void);
  37. void handleSerialDebug(void);
  38. void updateGPSData(void);
  39.  
  40. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  41. const uint8_t GPS_PushButton_PIN_A0 = A0;
  42.  
  43. /***** DEFINITION OF SERIAL COMMUNICATION PARAMETERS *****/
  44. const unsigned long SERIAL_BAUD_RATE = 9600;            // Serial communication baud rate for debugging
  45. const unsigned long GPS_BAUD_RATE = 9600;               // GPS module UART baud rate (NEO-M8N specification)
  46.  
  47. /***** DEFINITION OF SOFTWARE SERIAL PINS FOR GPS *****/
  48. const uint8_t GPS_RX_PIN = 10;                          // Software Serial RX pin for GPS module
  49. const uint8_t GPS_TX_PIN = 11;                          // Software Serial TX pin for GPS module
  50.  
  51. /****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
  52. // Software Serial for GPS communication (RX, TX)
  53. SoftwareSerial gpsSerial(GPS_RX_PIN, GPS_TX_PIN);
  54.  
  55. // EasyButton instance for GPS push button
  56. EasyButton button(GPS_PushButton_PIN_A0);
  57.  
  58. // TinyGPSPlus object to handle GPS parsing
  59. TinyGPSPlus gps;
  60.  
  61. // Variable to store GPS display flag
  62. boolean displayGPS = false;
  63.  
  64. // Variable to store last update time for timeout handling
  65. unsigned long lastGPSUpdate = 0;
  66. unsigned long lastDisplayTime = 0;
  67. const unsigned long DISPLAY_UPDATE_INTERVAL = 1000;  // Update display every 1 second
  68.  
  69. // Structure to hold parsed GPS data
  70. struct GPSData {
  71.     double latitude;
  72.     double longitude;
  73.     double altitude;
  74.     uint32_t satelliteCount;
  75.     uint8_t fixQuality;
  76.     double hdop;
  77.     uint16_t year;
  78.     uint8_t month;
  79.     uint8_t day;
  80.     uint8_t hour;
  81.     uint8_t minute;
  82.     uint8_t second;
  83.     boolean locationValid;
  84.     boolean altitudeValid;
  85.     boolean dateValid;
  86.     boolean timeValid;
  87.     boolean satellitesValid;
  88.     boolean hdopValid;
  89. } currentGPSData;
  90.  
  91. // Variables to track NMEA sentence statistics
  92. struct NMEAStats {
  93.     uint32_t ggaSentences;  // Global Positioning System Fix Data (Latitude/Longitude)
  94.     uint32_t rmcSentences;  // Recommended Minimum Navigation Information
  95.     uint32_t gsaSentences;  // GPS DOP and active satellites
  96.     uint32_t gsvSentences;  // GPS Satellites in view
  97.     uint32_t checksumErrors;
  98.     uint32_t totalCharsProcessed;
  99. } nmeaStats;
  100.  
  101. void setup(void)
  102. {
  103.     // Initialize hardware serial communication for debugging/display
  104.     Serial.begin(SERIAL_BAUD_RATE);
  105.    
  106.     // Wait for serial port to initialize (may not be necessary on all boards)
  107.     delay(100);
  108.    
  109.     // Initialize software serial for GPS module communication
  110.     // Arduino Nano: Pins 10 (RX) and 11 (TX) for software serial
  111.     gpsSerial.begin(GPS_BAUD_RATE);
  112.    
  113.     // Print startup message
  114.     Serial.println(F("\n\nNEO-M8N GPS Data Reader with Push Button Control"));
  115.     Serial.println(F("=============================================="));
  116.     Serial.println(F("Board: Arduino Nano"));
  117.     Serial.println(F("GPS Baud Rate: 9600"));
  118.     Serial.println(F("GPS RX Pin: D10 | GPS TX Pin: D11"));
  119.     Serial.println(F("Push Button Pin: A0 (PULLUP)"));
  120.     Serial.println(F("Press button to toggle GPS data display"));
  121.     Serial.println();
  122.    
  123.     // Initialize push button pin as input with internal pull-up
  124.     pinMode(GPS_PushButton_PIN_A0, INPUT_PULLUP);
  125.    
  126.     // Configure EasyButton
  127.     button.begin();
  128.    
  129.     // Set up button press event callback
  130.     button.onPressed(onButtonPressed);
  131.    
  132.     // Initialize GPS data structure
  133.     initializeGPSData();
  134.    
  135.     // Initialize NMEA statistics
  136.     nmeaStats.ggaSentences = 0;
  137.     nmeaStats.rmcSentences = 0;
  138.     nmeaStats.gsaSentences = 0;
  139.     nmeaStats.gsvSentences = 0;
  140.     nmeaStats.checksumErrors = 0;
  141.     nmeaStats.totalCharsProcessed = 0;
  142.    
  143.     Serial.println(F("Waiting for GPS signal..."));
  144.     Serial.println(F("NEO-M8N module will output NMEA sentences at 9600 baud"));
  145.     Serial.println();
  146.    
  147.     lastGPSUpdate = millis();
  148.     lastDisplayTime = millis();
  149. }
  150.  
  151. void loop(void)
  152. {
  153.     // Update button state - handles debouncing internally
  154.     button.read();
  155.    
  156.     // Read and parse GPS data from the software serial port
  157.     updateGPSData();
  158.    
  159.     // Display GPS data at specified interval if enabled
  160.     if (displayGPS && (millis() - lastDisplayTime >= DISPLAY_UPDATE_INTERVAL)) {
  161.         if (gps.location.isUpdated() || gps.altitude.isUpdated()) {
  162.             displayGPSData();
  163.             lastDisplayTime = millis();
  164.         }
  165.     }
  166.    
  167.     // Timeout check - if no valid data received for 5 seconds, print status
  168.     if ((millis() - lastGPSUpdate) > 5000) {
  169.         if (gps.charsProcessed() < 10) {
  170.             Serial.println(F("WARNING: No GPS data received. Check wiring and connections."));
  171.             Serial.print(F("Characters processed: "));
  172.             Serial.println(gps.charsProcessed());
  173.             Serial.println(F("Waiting for data..."));
  174.             lastGPSUpdate = millis();
  175.         }
  176.     }
  177. }
  178.  
  179. // Function to read and parse GPS data from software serial
  180. void updateGPSData(void)
  181. {
  182.     // Read available characters from GPS module
  183.     while (gpsSerial.available() > 0) {
  184.         char c = gpsSerial.read();
  185.        
  186.         // Encode character into TinyGPSPlus parser
  187.         gps.encode(c);
  188.        
  189.         // Update last GPS update time
  190.         lastGPSUpdate = millis();
  191.     }
  192.    
  193.     // Update internal GPS data structure with latest parsed values
  194.     if (gps.location.isUpdated()) {
  195.         currentGPSData.latitude = gps.location.lat();
  196.         currentGPSData.longitude = gps.location.lng();
  197.         currentGPSData.locationValid = gps.location.isValid();
  198.     }
  199.    
  200.     if (gps.altitude.isUpdated()) {
  201.         currentGPSData.altitude = gps.altitude.meters();
  202.         currentGPSData.altitudeValid = gps.altitude.isValid();
  203.     }
  204.    
  205.     if (gps.satellites.isUpdated()) {
  206.         currentGPSData.satelliteCount = gps.satellites.value();
  207.         currentGPSData.satellitesValid = gps.satellites.isValid();
  208.     }
  209.    
  210.     if (gps.hdop.isUpdated()) {
  211.         currentGPSData.hdop = gps.hdop.hdop();
  212.         currentGPSData.hdopValid = gps.hdop.isValid();
  213.     }
  214.    
  215.     if (gps.date.isUpdated()) {
  216.         currentGPSData.year = gps.date.year();
  217.         currentGPSData.month = gps.date.month();
  218.         currentGPSData.day = gps.date.day();
  219.         currentGPSData.dateValid = gps.date.isValid();
  220.     }
  221.    
  222.     if (gps.time.isUpdated()) {
  223.         currentGPSData.hour = gps.time.hour();
  224.         currentGPSData.minute = gps.time.minute();
  225.         currentGPSData.second = gps.time.second();
  226.         currentGPSData.timeValid = gps.time.isValid();
  227.     }
  228.    
  229.     // Update fix quality
  230.     currentGPSData.fixQuality = gps.fixQuality();
  231. }
  232.  
  233. // Function to initialize GPS data structure
  234. void initializeGPSData(void)
  235. {
  236.     currentGPSData.latitude = 0.0;
  237.     currentGPSData.longitude = 0.0;
  238.     currentGPSData.altitude = 0.0;
  239.     currentGPSData.satelliteCount = 0;
  240.     currentGPSData.fixQuality = 0;
  241.     currentGPSData.hdop = 0.0;
  242.     currentGPSData.year = 0;
  243.     currentGPSData.month = 0;
  244.     currentGPSData.day = 0;
  245.     currentGPSData.hour = 0;
  246.     currentGPSData.minute = 0;
  247.     currentGPSData.second = 0;
  248.     currentGPSData.locationValid = false;
  249.     currentGPSData.altitudeValid = false;
  250.     currentGPSData.dateValid = false;
  251.     currentGPSData.timeValid = false;
  252.     currentGPSData.satellitesValid = false;
  253.     currentGPSData.hdopValid = false;
  254. }
  255.  
  256. // Callback function for button press event
  257. void onButtonPressed(void)
  258. {
  259.     // Toggle GPS display flag
  260.     displayGPS = !displayGPS;
  261.    
  262.     if (displayGPS) {
  263.         Serial.println(F("\n*** GPS Display ENABLED ***\n"));
  264.         Serial.println(F("Real-time GPS data will be displayed below:\n"));
  265.     } else {
  266.         Serial.println(F("\n*** GPS Display DISABLED ***\n"));
  267.     }
  268. }
  269.  
  270. // Function to interpret fix quality code
  271. void printFixQualityDescription(uint8_t fixQuality)
  272. {
  273.     switch (fixQuality) {
  274.         case 0:
  275.             Serial.print(F("No Fix - GPS receiver not locked"));
  276.             break;
  277.         case 1:
  278.             Serial.print(F("GPS Fix - Standard GPS positioning"));
  279.             break;
  280.         case 2:
  281.             Serial.print(F("Differential GPS Fix - DGPS/WAAS correction"));
  282.             break;
  283.         case 3:
  284.             Serial.print(F("PPS Fix - Precise Positioning Service"));
  285.             break;
  286.         case 4:
  287.             Serial.print(F("Real Time Kinematic - RTK integer solution"));
  288.             break;
  289.         case 5:
  290.             Serial.print(F("Float RTK - RTK float solution"));
  291.             break;
  292.         case 6:
  293.             Serial.print(F("Estimated/Dead Reckoning - Dead reckoning mode"));
  294.             break;
  295.         case 7:
  296.             Serial.print(F("Manual Input Mode - User input coordinates"));
  297.             break;
  298.         case 8:
  299.             Serial.print(F("Simulation Mode - GPS simulator"));
  300.             break;
  301.         default:
  302.             Serial.print(F("Unknown Fix Quality"));
  303.             break;
  304.     }
  305. }
  306.  
  307. // Function to display GPS data including latitude, longitude, altitude, and fix quality
  308. void displayGPSData(void)
  309. {
  310.     // Display separator line
  311.     Serial.println(F("========================================"));
  312.    
  313.     // Display Date and Time (from RMC sentence)
  314.     Serial.print(F("Date/Time:     "));
  315.     if (currentGPSData.dateValid) {
  316.         if (currentGPSData.month < 10) Serial.print(F("0"));
  317.         Serial.print(currentGPSData.month);
  318.         Serial.print(F("/"));
  319.         if (currentGPSData.day < 10) Serial.print(F("0"));
  320.         Serial.print(currentGPSData.day);
  321.         Serial.print(F("/"));
  322.         Serial.print(currentGPSData.year);
  323.         Serial.print(F(" "));
  324.     } else {
  325.         Serial.print(F("-- (no date)   "));
  326.     }
  327.    
  328.     if (currentGPSData.timeValid) {
  329.         if (currentGPSData.hour < 10) Serial.print(F("0"));
  330.         Serial.print(currentGPSData.hour);
  331.         Serial.print(F(":"));
  332.         if (currentGPSData.minute < 10) Serial.print(F("0"));
  333.         Serial.print(currentGPSData.minute);
  334.         Serial.print(F(":"));
  335.         if (currentGPSData.second < 10) Serial.print(F("0"));
  336.         Serial.println(currentGPSData.second);
  337.     } else {
  338.         Serial.println(F("-- (no time)"));
  339.     }
  340.    
  341.     // Display Latitude (from GGA/RMC sentence)
  342.     Serial.print(F("Latitude:      "));
  343.     if (currentGPSData.locationValid) {
  344.         Serial.print(currentGPSData.latitude, 8);
  345.         Serial.println(F(" degrees"));
  346.     } else {
  347.         Serial.println(F("INVALID"));
  348.     }
  349.    
  350.     // Display Longitude (from GGA/RMC sentence)
  351.     Serial.print(F("Longitude:     "));
  352.     if (currentGPSData.locationValid) {
  353.         Serial.print(currentGPSData.longitude, 8);
  354.         Serial.println(F(" degrees"));
  355.     } else {
  356.         Serial.println(F("INVALID"));
  357.     }
  358.    
  359.     // Display Altitude (from GGA sentence)
  360.     Serial.print(F("Altitude:      "));
  361.     if (currentGPSData.altitudeValid) {
  362.         Serial.print(currentGPSData.altitude, 2);
  363.         Serial.println(F(" meters"));
  364.     } else {
  365.         Serial.println(F("INVALID"));
  366.     }
  367.    
  368.     // Display Number of Satellites (from GGA sentence)
  369.     Serial.print(F("Satellites:    "));
  370.     if (currentGPSData.satellitesValid) {
  371.         Serial.print(currentGPSData.satelliteCount);
  372.         Serial.println(F(" in use"));
  373.     } else {
  374.         Serial.println(F("INVALID"));
  375.     }
  376.    
  377.     // Display Fix Quality (from GGA sentence)
  378.     Serial.print(F("Fix Quality:   "));
  379.     if (currentGPSData.fixQuality != 255) {
  380.         Serial.print(currentGPSData.fixQuality);
  381.         Serial.print(F(" ("));
  382.         printFixQualityDescription(currentGPSData.fixQuality);
  383.         Serial.println(F(")"));
  384.     } else {
  385.         Serial.println(F("INVALID"));
  386.     }
  387.    
  388.     // Display HDOP - Horizontal Dilution of Precision (from GGA sentence)
  389.     Serial.print(F("HDOP:          "));
  390.     if (currentGPSData.hdopValid) {
  391.         Serial.println(currentGPSData.hdop, 2);
  392.     } else {
  393.         Serial.println(F("INVALID"));
  394.     }
  395.    
  396.     // Display statistics
  397.     Serial.println();
  398.     Serial.print(F("Data Processed: "));
  399.     Serial.print(gps.charsProcessed());
  400.     Serial.println(F(" characters"));
  401.    
  402.     Serial.print(F("Sentences with Fix: "));
  403.     Serial.println(gps.sentencesWithFix());
  404.    
  405.     Serial.print(F("Failed Checksums: "));
  406.     Serial.println(gps.failedChecksum());
  407.    
  408.     Serial.println(F("========================================"));
  409. }
  410.  
  411. // Function to handle serial debug commands (optional)
  412. void handleSerialDebug(void)
  413. {
  414.     if (Serial.available() > 0) {
  415.         char cmd = Serial.read();
  416.        
  417.         switch (cmd) {
  418.             case 's':
  419.             case 'S':
  420.                 // Display current GPS status
  421.                 displayGPSData();
  422.                 break;
  423.             case 'h':
  424.             case 'H':
  425.                 // Display help menu
  426.                 Serial.println(F("\nCommand Menu:"));
  427.                 Serial.println(F("  's' - Show GPS status"));
  428.                 Serial.println(F("  'h' - Show this help menu"));
  429.                 Serial.println(F("  'p' - Toggle GPS power (simulated)"));
  430.                 Serial.println();
  431.                 break;
  432.             case 'p':
  433.             case 'P':
  434.                 // Toggle power
  435.                 displayGPS = !displayGPS;
  436.                 if (displayGPS) {
  437.                     Serial.println(F("GPS Display ON"));
  438.                 } else {
  439.                     Serial.println(F("GPS Display OFF"));
  440.                 }
  441.                 break;
  442.         }
  443.     }
  444. }
  445.  
  446. /* END CODE */
  447.  
Advertisement
Add Comment
Please, Sign In to add comment