I have built a relatively simple Arduino GPS utilizing a pro mini, I2C OLED (128X64), and a UBLOX NEO 6M GPS module. I have modified code from another person that was relatively simple to use. Since the original code had characters too big for my oled, and I want to display more information that any screen could handle, I redesigned the code to show a particular set of data for two seconds then move on to the next set (with the time/date always on the top). This worked relatively well until I realized that the loop for the code received a new set of data at the beginning and played all the data over those next 8 seconds before updating, so by the time it got to speed it was showing a speed from 6 seconds ago. I want to fix the code to where everytime it switches to showing another set of data, its updating it right there so its the most current I can get. I can't seem to understand how to rewrite the loop code to do that. If you know how, please post it here. Any help is appreciated, thanks.
(btw this code uses the TinyGPS library, and the Adafruit library for OLED's)
here's the code
#include <SPI.h> //Serial Peripheral Interface (SPI) library for synchronous serial data protocol
#include <Wire.h> //Wire library used for I2C communication: Arduino Pro Mini pins used = A4 (SDA) and A5 (SCL)
#include <Adafruit_GFX.h> //Adafruit graphic display library used for the OLED display
#include <Adafruit_SSD1306.h> //Adafruit driver for OLED
#include <SoftwareSerial.h> //SoftwareSerial library used to allow serial communication on other digital pins (i.e. Pins 3 & 4 for the this GPS project)
#include <TinyGPS.h> //GPS Library used to read the data from GPS Module
#define OLED_RESET 5
Adafruit_SSD1306 display(OLED_RESET);
TinyGPS gps; //Create the TinyGPS object, giving it a name of your choice (here we use the name gps)
SoftwareSerial nss(3, 4); //set sotfware serial communications to arduino ports 3 and 4 (TX = 3 and RX = 4)
void setup() {
nss.begin(9600);
unsigned long age, date, time, chars = 0;
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize the OLED and set the I2C address to 0x3C (for the 128x64 OLED)
display.clearDisplay(); //Clear display buffer.
display.setTextSize(1); //set text size to 2 (large)
display.setTextColor(WHITE);
display.setCursor(0,0); //set text start position to column=0 and row=0
display.print("ARDUINO"); //print "SCULLCOM" to display
display.setTextSize(0); //set text size to 1 (small)
display.setCursor(0,9); //set text start position to column=0 and row=18
display.print("GPS SYSTEM"); //print "GPS SYSTEM" to display
display.setCursor(0,20); //set text start position to column=0 and row=40
display.setTextColor(WHITE); //
display.print("locating satellites"); //print "Trying to locate GPS satellites ..." to displa
display.display(); //update OLED with new display data
delay(1000); //short delay
display.clearDisplay(); //clear display
}
//MAIN PROGRAMME LOOP
void loop() {
bool newdata = false;
unsigned long start = millis();
while(millis() - start < 1000){ // Every 1 seconds we print an update
if (feedgps())
newdata = true;
}
if (newdata)
{
gpsdump(gps);
}
}
//PRINT GPS DATA TO OLED DISPLAY
void gpsdump(TinyGPS &gps)
{
display.clearDisplay();
float flat, flon, falt;
float fmph = gps.f_speed_mph();
float faltitude = (gps.f_altitude());
faltitude = (faltitude*3.2808);
display.setTextSize(0); //set text size to 1
display.setTextColor(WHITE); //
display.setCursor(0,0); //set text start position for date and time (row = 0, column =0)
print_date(gps);
display.setCursor(105, 0);
display.print("UTC"); //prints date and time on top line to OLED
display.display();
gps.f_get_position(&flat, &flon); //retrieves latitude and longditude data
display.setCursor(0,10); //set text start position to column=0 and row=20
display.print("Altitude : "); //print "Altitude" : to display
display.println(faltitude);//print altitude data to display
display.setCursor(105,10); //set text start position to column=100 and row=20
display.println("FT");
display.display();
delay(2000);
display.clearDisplay();
display.setTextSize(0); //set text size to 1
display.setTextColor(WHITE); //
display.setCursor(0,0); //set text start position for date and time (row = 0, column =0)
print_date(gps);
display.setCursor(105, 0);
display.print("UTC");
display.display();
display.setCursor(0,10); //set text start position to column=0 and row=30
display.print("Sats : "); //print "Sats :" to display
display.println(gps.satellites());//print number of satellites detected to display
display.display();
delay(2000);
display.clearDisplay();
display.setTextSize(0); //set text size to 1
display.setTextColor(WHITE); //
display.setCursor(0,0); //set text start position for date and time (row = 0, column =0)
print_date(gps);
display.setCursor(105, 0);
display.print("UTC");
display.display();
display.setCursor(0,10); //set text start position to column=0 and row=40
display.print("latitude : "); //print "latitude :" to display
display.println(flat,6); //print latitude data to display up to 6 decimal places
display.setCursor(0,20); //set text start position to column=0 and row=50
display.print("longitude: "); //print "longitude:" to display
display.println(flon,6); //print longitude data to display up to 6 decimal places
display.display();
delay(2000);
display.clearDisplay();
display.setTextSize(0); //set text size to 1
display.setTextColor(WHITE); //
display.setCursor(0,0); //set text start position for date and time (row = 0, column =0)
print_date(gps);
display.setCursor(105, 0);
display.print("UTC");
display.display();
display.setCursor(0, 10);
display.print("Speed: ");
display.println(fmph);
display.setCursor(70 ,10);
display.print("mph");
display.display();
delay(2000);
}
//TEST FOR NEW DATA FROM THE GPS MODULE
bool feedgps()
{
while (nss.available())
{
if (gps.encode(nss.read())) //Each byte of NEMA data must be giving to TinyGPS by using encode(). True is returned when new data has been fully decoded and can be used
return true;
}
return false;
}
//GET DATE AND TIME FOR DISPLAY
static void print_date(TinyGPS &gps)
{
int year;
byte month, day, hour, minute, second, hundredths;
unsigned long age;
gps.crack_datetime(&year, &month, &day, &hour, &minute, &second);
{
char sz[32];
sprintf(sz, "%02d/%02d/%02d %02d:%02d",
month, day, year, hour, minute, second);
display.print(sz); //Print date and time to OLED
}
}