Advertisement
pleasedontcode

"GPS Logging" rev_01

Feb 11th, 2024
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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 Logging"
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2024-02-11 13:59:07
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* log the position every 10 seconds and write to the */
  21.     /* Micro SD card for offline loading into Google maps */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24. /****** DEFINITION OF LIBRARIES *****/
  25. #include <SPI.h>
  26. #include <SdFat.h>
  27.  
  28. /****** FUNCTION PROTOTYPES *****/
  29. void setup(void);
  30. void loop(void);
  31. float getLatitude(); // Function declaration
  32. float getLongitude(); // Function declaration
  33.  
  34. /***** DEFINITION OF SPI PINS *****/
  35. const uint8_t GPS_SDCardModule_SPI_PIN_MOSI_D11 = 11;
  36. const uint8_t GPS_SDCardModule_SPI_PIN_MISO_D12 = 12;
  37. const uint8_t GPS_SDCardModule_SPI_PIN_SCLK_D13 = 13;
  38. const uint8_t GPS_SDCardModule_SPI_PIN_CS_D10 = 10;
  39.  
  40. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  41. SdFat sd;
  42. SdFile file;
  43.  
  44. // Variable to track time
  45. unsigned long lastLogTime = 0;
  46. const unsigned long logInterval = 10000; // Log position every 10 seconds
  47.  
  48. void setup(void)
  49. {
  50.   // put your setup code here, to run once:
  51.   pinMode(GPS_SDCardModule_SPI_PIN_CS_D10, OUTPUT);
  52.  
  53.   // start the SPI library:
  54.   SPI.begin();
  55.  
  56.   // Initialize the SD card
  57.   if (!sd.begin(GPS_SDCardModule_SPI_PIN_CS_D10, SPI_HALF_SPEED))
  58.   {
  59.     // SD card initialization failed
  60.     while (1);
  61.   }
  62.  
  63.   // Create a new file on the SD card
  64.   if (!file.open("position_log.txt", O_WRITE | O_CREAT | O_TRUNC))
  65.   {
  66.     // File creation failed
  67.     while (1);
  68.   }
  69.  
  70.   // Write header to the file
  71.   file.println("Latitude,Longitude");
  72.  
  73.   // Close the file
  74.   file.close();
  75. }
  76.  
  77. void loop(void)
  78. {
  79.   // put your main code here, to run repeatedly:
  80.  
  81.   // Check if it's time to log the position
  82.   if (millis() - lastLogTime >= logInterval)
  83.   {
  84.     // Update the last log time
  85.     lastLogTime = millis();
  86.  
  87.     // Get the GPS position
  88.     float latitude = getLatitude();
  89.     float longitude = getLongitude();
  90.  
  91.     // Open the file in append mode
  92.     if (!file.open("position_log.txt", O_WRITE | O_APPEND))
  93.     {
  94.       // File opening failed
  95.       while (1);
  96.     }
  97.  
  98.     // Write the position to the file
  99.     file.print(latitude);
  100.     file.print(",");
  101.     file.println(longitude);
  102.  
  103.     // Close the file
  104.     file.close();
  105.   }
  106. }
  107.  
  108. // Function definition for getLatitude()
  109. float getLatitude()
  110. {
  111.   // Your code to retrieve the latitude from the GPS module
  112. }
  113.  
  114. // Function definition for getLongitude()
  115. float getLongitude()
  116. {
  117.   // Your code to retrieve the longitude from the GPS module
  118. }
  119.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement