Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: # GPS Reader
- - Version: 002
- - Source Code NOT compiled for: Arduino Nano
- - Source Code created on: 2026-03-07 02:25:28
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Read NEO-M8N GPS module data via UART (9600 baud), */
- /* parse NMEA sentences, and display latitude, */
- /* longitude, altitude, satellite count, and fix */
- /* quality */
- /****** END SYSTEM REQUIREMENTS *****/
- #include <SoftwareSerial.h>
- #include <EasyButton.h>
- #include <TinyGPSPlus.h>
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void displayGPSData(void);
- void onButtonPressed(void);
- void handleSerialDebug(void);
- void updateGPSData(void);
- /***** DEFINITION OF DIGITAL INPUT PINS *****/
- const uint8_t GPS_PushButton_PIN_A0 = A0;
- /***** DEFINITION OF SERIAL COMMUNICATION PARAMETERS *****/
- const unsigned long SERIAL_BAUD_RATE = 9600; // Serial communication baud rate for debugging
- const unsigned long GPS_BAUD_RATE = 9600; // GPS module UART baud rate (NEO-M8N specification)
- /***** DEFINITION OF SOFTWARE SERIAL PINS FOR GPS *****/
- const uint8_t GPS_RX_PIN = 10; // Software Serial RX pin for GPS module
- const uint8_t GPS_TX_PIN = 11; // Software Serial TX pin for GPS module
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
- // Software Serial for GPS communication (RX, TX)
- SoftwareSerial gpsSerial(GPS_RX_PIN, GPS_TX_PIN);
- // EasyButton instance for GPS push button
- EasyButton button(GPS_PushButton_PIN_A0);
- // TinyGPSPlus object to handle GPS parsing
- TinyGPSPlus gps;
- // Variable to store GPS display flag
- boolean displayGPS = false;
- // Variable to store last update time for timeout handling
- unsigned long lastGPSUpdate = 0;
- unsigned long lastDisplayTime = 0;
- const unsigned long DISPLAY_UPDATE_INTERVAL = 1000; // Update display every 1 second
- // Structure to hold parsed GPS data
- struct GPSData {
- double latitude;
- double longitude;
- double altitude;
- uint32_t satelliteCount;
- uint8_t fixQuality;
- double hdop;
- uint16_t year;
- uint8_t month;
- uint8_t day;
- uint8_t hour;
- uint8_t minute;
- uint8_t second;
- boolean locationValid;
- boolean altitudeValid;
- boolean dateValid;
- boolean timeValid;
- boolean satellitesValid;
- boolean hdopValid;
- } currentGPSData;
- // Variables to track NMEA sentence statistics
- struct NMEAStats {
- uint32_t ggaSentences; // Global Positioning System Fix Data (Latitude/Longitude)
- uint32_t rmcSentences; // Recommended Minimum Navigation Information
- uint32_t gsaSentences; // GPS DOP and active satellites
- uint32_t gsvSentences; // GPS Satellites in view
- uint32_t checksumErrors;
- uint32_t totalCharsProcessed;
- } nmeaStats;
- void setup(void)
- {
- // Initialize hardware serial communication for debugging/display
- Serial.begin(SERIAL_BAUD_RATE);
- // Wait for serial port to initialize (may not be necessary on all boards)
- delay(100);
- // Initialize software serial for GPS module communication
- // Arduino Nano: Pins 10 (RX) and 11 (TX) for software serial
- gpsSerial.begin(GPS_BAUD_RATE);
- // Print startup message
- Serial.println(F("\n\nNEO-M8N GPS Data Reader with Push Button Control"));
- Serial.println(F("=============================================="));
- Serial.println(F("Board: Arduino Nano"));
- Serial.println(F("GPS Baud Rate: 9600"));
- Serial.println(F("GPS RX Pin: D10 | GPS TX Pin: D11"));
- Serial.println(F("Push Button Pin: A0 (PULLUP)"));
- Serial.println(F("Press button to toggle GPS data display"));
- Serial.println();
- // Initialize push button pin as input with internal pull-up
- pinMode(GPS_PushButton_PIN_A0, INPUT_PULLUP);
- // Configure EasyButton
- button.begin();
- // Set up button press event callback
- button.onPressed(onButtonPressed);
- // Initialize GPS data structure
- initializeGPSData();
- // Initialize NMEA statistics
- nmeaStats.ggaSentences = 0;
- nmeaStats.rmcSentences = 0;
- nmeaStats.gsaSentences = 0;
- nmeaStats.gsvSentences = 0;
- nmeaStats.checksumErrors = 0;
- nmeaStats.totalCharsProcessed = 0;
- Serial.println(F("Waiting for GPS signal..."));
- Serial.println(F("NEO-M8N module will output NMEA sentences at 9600 baud"));
- Serial.println();
- lastGPSUpdate = millis();
- lastDisplayTime = millis();
- }
- void loop(void)
- {
- // Update button state - handles debouncing internally
- button.read();
- // Read and parse GPS data from the software serial port
- updateGPSData();
- // Display GPS data at specified interval if enabled
- if (displayGPS && (millis() - lastDisplayTime >= DISPLAY_UPDATE_INTERVAL)) {
- if (gps.location.isUpdated() || gps.altitude.isUpdated()) {
- displayGPSData();
- lastDisplayTime = millis();
- }
- }
- // Timeout check - if no valid data received for 5 seconds, print status
- if ((millis() - lastGPSUpdate) > 5000) {
- if (gps.charsProcessed() < 10) {
- Serial.println(F("WARNING: No GPS data received. Check wiring and connections."));
- Serial.print(F("Characters processed: "));
- Serial.println(gps.charsProcessed());
- Serial.println(F("Waiting for data..."));
- lastGPSUpdate = millis();
- }
- }
- }
- // Function to read and parse GPS data from software serial
- void updateGPSData(void)
- {
- // Read available characters from GPS module
- while (gpsSerial.available() > 0) {
- char c = gpsSerial.read();
- // Encode character into TinyGPSPlus parser
- gps.encode(c);
- // Update last GPS update time
- lastGPSUpdate = millis();
- }
- // Update internal GPS data structure with latest parsed values
- if (gps.location.isUpdated()) {
- currentGPSData.latitude = gps.location.lat();
- currentGPSData.longitude = gps.location.lng();
- currentGPSData.locationValid = gps.location.isValid();
- }
- if (gps.altitude.isUpdated()) {
- currentGPSData.altitude = gps.altitude.meters();
- currentGPSData.altitudeValid = gps.altitude.isValid();
- }
- if (gps.satellites.isUpdated()) {
- currentGPSData.satelliteCount = gps.satellites.value();
- currentGPSData.satellitesValid = gps.satellites.isValid();
- }
- if (gps.hdop.isUpdated()) {
- currentGPSData.hdop = gps.hdop.hdop();
- currentGPSData.hdopValid = gps.hdop.isValid();
- }
- if (gps.date.isUpdated()) {
- currentGPSData.year = gps.date.year();
- currentGPSData.month = gps.date.month();
- currentGPSData.day = gps.date.day();
- currentGPSData.dateValid = gps.date.isValid();
- }
- if (gps.time.isUpdated()) {
- currentGPSData.hour = gps.time.hour();
- currentGPSData.minute = gps.time.minute();
- currentGPSData.second = gps.time.second();
- currentGPSData.timeValid = gps.time.isValid();
- }
- // Update fix quality
- currentGPSData.fixQuality = gps.fixQuality();
- }
- // Function to initialize GPS data structure
- void initializeGPSData(void)
- {
- currentGPSData.latitude = 0.0;
- currentGPSData.longitude = 0.0;
- currentGPSData.altitude = 0.0;
- currentGPSData.satelliteCount = 0;
- currentGPSData.fixQuality = 0;
- currentGPSData.hdop = 0.0;
- currentGPSData.year = 0;
- currentGPSData.month = 0;
- currentGPSData.day = 0;
- currentGPSData.hour = 0;
- currentGPSData.minute = 0;
- currentGPSData.second = 0;
- currentGPSData.locationValid = false;
- currentGPSData.altitudeValid = false;
- currentGPSData.dateValid = false;
- currentGPSData.timeValid = false;
- currentGPSData.satellitesValid = false;
- currentGPSData.hdopValid = false;
- }
- // Callback function for button press event
- void onButtonPressed(void)
- {
- // Toggle GPS display flag
- displayGPS = !displayGPS;
- if (displayGPS) {
- Serial.println(F("\n*** GPS Display ENABLED ***\n"));
- Serial.println(F("Real-time GPS data will be displayed below:\n"));
- } else {
- Serial.println(F("\n*** GPS Display DISABLED ***\n"));
- }
- }
- // Function to interpret fix quality code
- void printFixQualityDescription(uint8_t fixQuality)
- {
- switch (fixQuality) {
- case 0:
- Serial.print(F("No Fix - GPS receiver not locked"));
- break;
- case 1:
- Serial.print(F("GPS Fix - Standard GPS positioning"));
- break;
- case 2:
- Serial.print(F("Differential GPS Fix - DGPS/WAAS correction"));
- break;
- case 3:
- Serial.print(F("PPS Fix - Precise Positioning Service"));
- break;
- case 4:
- Serial.print(F("Real Time Kinematic - RTK integer solution"));
- break;
- case 5:
- Serial.print(F("Float RTK - RTK float solution"));
- break;
- case 6:
- Serial.print(F("Estimated/Dead Reckoning - Dead reckoning mode"));
- break;
- case 7:
- Serial.print(F("Manual Input Mode - User input coordinates"));
- break;
- case 8:
- Serial.print(F("Simulation Mode - GPS simulator"));
- break;
- default:
- Serial.print(F("Unknown Fix Quality"));
- break;
- }
- }
- // Function to display GPS data including latitude, longitude, altitude, and fix quality
- void displayGPSData(void)
- {
- // Display separator line
- Serial.println(F("========================================"));
- // Display Date and Time (from RMC sentence)
- Serial.print(F("Date/Time: "));
- if (currentGPSData.dateValid) {
- if (currentGPSData.month < 10) Serial.print(F("0"));
- Serial.print(currentGPSData.month);
- Serial.print(F("/"));
- if (currentGPSData.day < 10) Serial.print(F("0"));
- Serial.print(currentGPSData.day);
- Serial.print(F("/"));
- Serial.print(currentGPSData.year);
- Serial.print(F(" "));
- } else {
- Serial.print(F("-- (no date) "));
- }
- if (currentGPSData.timeValid) {
- if (currentGPSData.hour < 10) Serial.print(F("0"));
- Serial.print(currentGPSData.hour);
- Serial.print(F(":"));
- if (currentGPSData.minute < 10) Serial.print(F("0"));
- Serial.print(currentGPSData.minute);
- Serial.print(F(":"));
- if (currentGPSData.second < 10) Serial.print(F("0"));
- Serial.println(currentGPSData.second);
- } else {
- Serial.println(F("-- (no time)"));
- }
- // Display Latitude (from GGA/RMC sentence)
- Serial.print(F("Latitude: "));
- if (currentGPSData.locationValid) {
- Serial.print(currentGPSData.latitude, 8);
- Serial.println(F(" degrees"));
- } else {
- Serial.println(F("INVALID"));
- }
- // Display Longitude (from GGA/RMC sentence)
- Serial.print(F("Longitude: "));
- if (currentGPSData.locationValid) {
- Serial.print(currentGPSData.longitude, 8);
- Serial.println(F(" degrees"));
- } else {
- Serial.println(F("INVALID"));
- }
- // Display Altitude (from GGA sentence)
- Serial.print(F("Altitude: "));
- if (currentGPSData.altitudeValid) {
- Serial.print(currentGPSData.altitude, 2);
- Serial.println(F(" meters"));
- } else {
- Serial.println(F("INVALID"));
- }
- // Display Number of Satellites (from GGA sentence)
- Serial.print(F("Satellites: "));
- if (currentGPSData.satellitesValid) {
- Serial.print(currentGPSData.satelliteCount);
- Serial.println(F(" in use"));
- } else {
- Serial.println(F("INVALID"));
- }
- // Display Fix Quality (from GGA sentence)
- Serial.print(F("Fix Quality: "));
- if (currentGPSData.fixQuality != 255) {
- Serial.print(currentGPSData.fixQuality);
- Serial.print(F(" ("));
- printFixQualityDescription(currentGPSData.fixQuality);
- Serial.println(F(")"));
- } else {
- Serial.println(F("INVALID"));
- }
- // Display HDOP - Horizontal Dilution of Precision (from GGA sentence)
- Serial.print(F("HDOP: "));
- if (currentGPSData.hdopValid) {
- Serial.println(currentGPSData.hdop, 2);
- } else {
- Serial.println(F("INVALID"));
- }
- // Display statistics
- Serial.println();
- Serial.print(F("Data Processed: "));
- Serial.print(gps.charsProcessed());
- Serial.println(F(" characters"));
- Serial.print(F("Sentences with Fix: "));
- Serial.println(gps.sentencesWithFix());
- Serial.print(F("Failed Checksums: "));
- Serial.println(gps.failedChecksum());
- Serial.println(F("========================================"));
- }
- // Function to handle serial debug commands (optional)
- void handleSerialDebug(void)
- {
- if (Serial.available() > 0) {
- char cmd = Serial.read();
- switch (cmd) {
- case 's':
- case 'S':
- // Display current GPS status
- displayGPSData();
- break;
- case 'h':
- case 'H':
- // Display help menu
- Serial.println(F("\nCommand Menu:"));
- Serial.println(F(" 's' - Show GPS status"));
- Serial.println(F(" 'h' - Show this help menu"));
- Serial.println(F(" 'p' - Toggle GPS power (simulated)"));
- Serial.println();
- break;
- case 'p':
- case 'P':
- // Toggle power
- displayGPS = !displayGPS;
- if (displayGPS) {
- Serial.println(F("GPS Display ON"));
- } else {
- Serial.println(F("GPS Display OFF"));
- }
- break;
- }
- }
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment