I have modified some gps code for my use the basically shows me speed, and altitude waits two seconds then shows the lat and lon, and the satellites connected. Then when the code loops around it updates the data. I want to change the code to where instead of it switching automatically, I can push a button to change it. I'm not sure how to go about doing this because when I tried to apply something to do that, I received a bunch of errors. If you have any idea how I could apply something like that onto the code, Please post it here, Thanks
(on a side note - I'm also considering using a microSD module to log data every 20 seconds or so when I push a button, and stop when I push it again If you have any idea on how I would go about doing that, please also include it, Thanks.)
my 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);
int inPin = 7;
int val = 0;
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");
display.setCursor(0, 10);
display.print("Speed: ");
display.println(fmph);
display.setCursor(70 ,10);
display.print("mph");
gps.f_get_position(&flat, &flon); //retrieves latitude and longditude data
display.setCursor(0, 20); //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, 20); //set text start position to column=100 and row=20
display.println("FT");
display.display();
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("lat "); //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("lon "); //print "longitude:" to display
display.println(flon,6); //print longitude data to display up to 6 decimal places
display.display();
display.setCursor(102, 10); //set text start position to column=0 and row=30
display.print("Sats ");
display.setCursor(105, 20);
display.println(gps.satellites());//print number of satellites detected to display
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
}
}