Components and supplies
MKR GPS Shield
Arduino MKR WiFi 1010
Hammond 1591CTCL clear polycarbonate 4.7x2.6x1.4" project box
SSD1327
Lithium Ion Polymer Battery - 3.7v 2500mAh
Project description
Code
GPS_SSD1327-1
arduino
Arduino language sketch that retrieves and displays GPS data
1/* This sketch uses the Arduino MKR WiFi 1010, MKR GPS shield and Adafruit SSD1327 display. Date/time, GPS latitude, longitude, altitude, ground speed and 2distance are displayed on the SSD1327 along with battery voltage and distance traveled in meters, feet and miles. The real time clock on the MKR WiFi 1010 is 3synchronized with GPS time and displays the date, hour, minute and second.*/ 4 5//Libraries for Adafruit SSD1327 display, Arduino MKR GPS and Arduino Real Time Clock 6#include <Adafruit_SSD1327.h> 7#include <Arduino_MKRGPS.h> 8#include <RTCZero.h> 9 10#define DTHRESH 1.0 //Distance change threshhold to calculate total distance 11#define STHRESH 0.3 //Speed threshhold to calculate total distance 12 13//Instantiate the display and real time clock objects 14Adafruit_SSD1327 display(128, 128, &Wire, -1, 1000000); 15RTCZero rtc; 16 17//Declare the previous lat/long, distance and duration global variables 18float plat; 19float plon; 20float dist; 21float dur; 22uint32_t tm0; 23uint32_t tm1; 24 25//Initial setup 26void setup() { 27 //The following serial monitor display statements are commented out in case they are needed for troubleshooting when the board is connected to a computer 28 //Serial.begin(9600); 29 //while (! Serial) delay(100); 30 31 //Initialize the Adafruit SSD1327 128x128 pixel display 32 if ( ! display.begin(0x3D) ) { 33 //Serial.println("Unable to initialize OLED"); 34 while (1) yield(); 35 } 36 37 //Set the SSD1327 text size, colors and clear the display 38 display.display(); 39 display.setTextSize(1); 40 display.setTextWrap(false); 41 display.setTextColor(SSD1327_WHITE,SSD1327_BLACK); 42 display.clearDisplay(); 43 display.display(); 44 45 //Initialize the real time clock 46 rtc.begin(); 47 48 //Initialize the MKR GPS 49 if (!GPS.begin(GPS_MODE_SHIELD)) { 50 //display.println("Failed to initialize GPS!"); 51 while (1); 52 } 53 54 //Wait for the GPS to become available. This may take up to 15 minutes. 55 while (!GPS.available()) { 56 ; 57 } 58 59 //Declare the global previous latitude, longitude and cumulative distance variables 60 plat = GPS.latitude(); 61 plon = GPS.longitude(); 62 tm0 = GPS.getTime(); 63 dist = 0.0; 64 dur = 0.0; 65 tm1 = 0; 66} 67 68//Main loop 69void loop() { 70 //Wait for the GPS to become available then display GPS data 71 if (GPS.available()) { 72 uint32_t tm = GPS.getTime(); //Get GPS epoch time 73 if (rtc.getEpoch() != tm) { //Sychronize the real time clock with GPS time 74 rtc.setEpoch(tm); 75 } 76 float lat = GPS.latitude(); //Get GPS latitude 77 float lon = GPS.longitude(); //Get GPS longitude 78 float altm = GPS.altitude(); //Get GPS altitude in meters 79 float altf = 3.28084 * altm; //Convert altiude from meters to feet 80 float kph = GPS.speed(); //Get GPS ground speed in kph 81 float mph = 0.621371 * kph; //Convert kph ground speed to mph 82 int satellites = GPS.satellites(); //Get number of GPS satellites received 83 int Batt_int = analogRead(ADC_BATTERY); //Read the external battery voltage 84 float Batt_int_v = Batt_int * (4.27/1023.0); //Convert the digital battery voltage to analog voltage 85 float xkm; //Declare distance variable in km 86 float xmt; //Declare distance variable in meters 87 88 //Check real time clock every second and calculate distance traveled in km since GPS reading using the Haversine formula 89 if (tm > tm1) { 90 xkm = HaverSine(plat, plon, lat, lon); 91 xmt = 1000.0 * xkm; //Convert km to meters 92 /*Reduce false GPS readings by checking for non-numeric Haversine results, distance change greater than threshhold and 93 speed greater than threshhold.*/ 94 if (! isnan(xmt) && xmt > DTHRESH && mph > STHRESH) { 95 dist += xmt; //Add distance reading in meters to cumulative distance 96 } 97 } 98 99 //Calculate distance and duration 100 float distmi = 0.000621371 * dist; //Convert distance in km to miles 101 float distft = 3.28084 * dist; //Convert distance in meters to feet 102 dur = (tm - tm0)/3600.0; //Hours duration 103 104 //Display values on SSD1327 105 display.display(); //Send yield message to SSD1327 display 106 display.clearDisplay(); //Clear the SSD1327 display 107 display.setCursor(0,0); //Position the SSD1327 cursor at top left 108 printZero(rtc.getMonth()); //Display real time clock month with leading zero if needed 109 display.print("-"); 110 printZero(rtc.getDay()); //Display real time clock day with leading zero if needed 111 display.print("-"); 112 printZero(rtc.getYear()); //Display real time clock year with leading zero if needed 113 display.print(" "); 114 printZero(rtc.getHours()-4); //Display real time clock hour with leading zero if needed and correct for time zone 115 display.print(":"); 116 printZero(rtc.getMinutes()); //Display real time clock minute with leading zero if needed 117 display.print(":"); 118 printZero(rtc.getSeconds()); //Display real time clock second with leading zero if needed 119 display.println(); 120 display.print(lat,7); //Display GPS latitude as decimal degrees 121 display.println(" lat"); 122 display.print(lon,7); //Display GPS longitude as decimal degrees 123 display.println(" lon"); 124 display.print(altf); //Display GPS altitude in feet 125 display.println(" alt ft"); 126 display.print(mph); //Display GPS ground speed in mph 127 display.println(" mph"); 128 display.print(satellites); //Display number of GPS satellites received 129 display.println(" satellites"); 130 display.print(Batt_int_v); //Display analog battery voltage 131 display.println(" volts"); 132 display.print(distmi,3); //Display cumulative distance traveled in miles 133 display.println(" dist miles"); 134 display.print(distft,1); //Display cumulative distance traveled in feet 135 display.println(" dist feet"); 136 display.print(dur,3); 137 display.println(" hours"); 138 display.display(); 139 140 /*Copy current lat/lon into previous lat/lon for next distance measurement 141 and current time to previous time for next time measurement.*/ 142 plat = lat; 143 plon = lon; 144 tm1 = tm; 145 } 146} 147 148//Function to display leading zero if single digit 149void printZero(int x) { 150 if (x < 10) { 151 display.print("0"); 152 } 153 display.print(x); 154} 155 156//Haversine formula to calculate distance between current and previous latitude/longitude points 157float HaverSine(float lat1, float lon1, float lat2, float lon2) 158{ 159 float ToRad = PI / 180.0; 160 float R = 6371; // radius earth in Km 161 162 float dLat = (lat2-lat1) * ToRad; 163 float dLon = (lon2-lon1) * ToRad; 164 165 float a = sin(dLat/2) * sin(dLat/2) + 166 cos(lat1 * ToRad) * cos(lat2 * ToRad) * 167 sin(dLon/2) * sin(dLon/2); 168 169 float c = 2 * atan2(sqrt(a), sqrt(1-a)); 170 171 float d = R * c; 172 return d; 173}
Comments
Only logged in users can leave comments
maulepilot
1 Followers
•4 Projects
1
Anonymous user
3 years ago
I can’t get the oled display to work, it turns on, and flashes for a second but doesn’t display anything, I haven’t had a chance to do much troubleshooting, but I’m pretty much a newb with this stuff. I have tried uploading the code with the web editor and uploaded the library’s, but it still acts the same.