switch between screens using a pushbutton

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 :slight_smile:

(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
  }

}

I have difficulty understanding how you implement the two second delay between the display of the first set of parameters and the display of the second set of parameters. gpsdump only has a delay at the end of the function, not in between. That might be just me, though, but maybe you can clarify.

Next question is why you use a construction like below?

  unsigned long start = millis();
  while (millis() - start < 1000) {  // Every 1 seconds we print an update
    if (feedgps())
      newdata = true;
  }

This will call feedgps numerous times in one second (unless feedgps take a long time to run). It does not do what the comment says it does. Only the result of the last call will will be used later.

To get to your problem. Split your gpsdump into two parts; e.g. gpsdump_position and gpsdump_speed_alt.

/*
  display speed and altitude
*/
void gpsdump_speed_alt(TinyGPS &gps)
{

  display.clearDisplay();

  float 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");

  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 position
*/
void gpsdump_position(TinyGPS &gps)
{

  display.clearDisplay();

  float flat, flon;

  gps.f_get_position(&flat, &flon); //retrieves latitude and longditude data

  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();
}

Next, depending on the 'state' of the button, call either the one or the other (in loop()).

if(digitalRead(yourButton) == HIGH)
  gpsdump_speed_lat(gp);
else
  gpsdump_position(gps);

The above assumes a button between pin and ground and using a pull-up resistor.

Note:

I received a bunch of errors

Next time, post the errors and the code that causes the errors.

PS
I don't have the libraries so I can't compile and test.

Both the switching of screens and the enabling/disabling of SD writing have the same solution.

Use "flags" , boolean variables, set true or false or toggled with the button press. Use the state of those flags in conditional tests of what screen to print or whether to log or not.

For more options, you can use the button to set a count, and then use switch case on the number.

You need to use debounced, state change detection for the button press. Here's a simple example based on INPUT_PULLUP logic and a simple blocking debounce. You can use whatever non blocking debounce you are comfortable with or use a library like Bounce2.h.

byte val = 0;
byte oldVal = 0;
unsigned long debouncePeriod = 20;
boolean setFlag = false;

void setup() {
  pinMode(11, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop() {

  if(setFlag)
  {
    Serial.println("button pressed");
    setFlag = false;
  }
  
  val = digitalRead(11);    //read push button state

  if ((val == LOW) && (oldVal == HIGH))  // check is button pressed and is changed using logic for input pullup
  {
    delay(debouncePeriod); //blocking debounce routine using delay
    val = digitalRead(11); //read button again
    if ((val == LOW) && (oldVal == HIGH)) {
      setFlag = true;
    }
  }
  oldVal = val;
}

Button was declared but not setup as INPUT in the original sketch - or what do you declare with
inPin=7 ?

Cool project coolguy!

I made something similar:

If you'd like I can post my code. It has a menu system that works with a single button and a pot.

coolguy225:
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 :slight_smile:

(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
}

}

Cool box, Chris.
Does it still spend mints? :slight_smile:

Thanks everyone for the response, I do appreciate them.

Next time, post the errors and the code that causes the errors.

I tried implementing your code into my sketch ( keep in mind i'm not very good at coding, still learning, so I may have made some simple mistakes)

here are the following error messages.

sketch_jun11b.ino: In function 'void loop()':
sketch_jun11b:49: error: 'gpsdump_speed_lat' was not declared in this scope
sketch_jun11b:61: error: 'gpsdump' was not declared in this scope

here is what the new code looks like:

#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() {
  if(digitalRead(inPin) == HIGH)
  gpsdump_speed_lat(gps);
else
  gpsdump_position(gps);
    
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
    

/*
  display speed and altitude
*/
void gpsdump_speed_alt(TinyGPS &gps)
{

  display.clearDisplay();

  float 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");

  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 position
*/
void gpsdump_position(TinyGPS &gps)
{

  display.clearDisplay();

  float flat, flon;

  gps.f_get_position(&flat, &flon); //retrieves latitude and longditude data

  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();
}

//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
  }

}

Use "flags" , boolean variables, set true or false or toggled with the button press. Use the state of those flags in conditional tests of what screen to print or whether to log or not.

I did test out your code and it seems to work like I want it to. I just am not sure how to put the loop section of my code into your.

If you'd like I can post my code. It has a menu system that works with a single button and a pot.

If you could I would like so see your code since i'm looking for other types of code as well. Also if anyone is interested I could post a short video of my gps, or just some pictures.

sketch_jun11b.ino: In function 'void loop()':
sketch_jun11b:49: error: 'gpsdump_speed_lat' was not declared in this scope
sketch_jun11b:61: error: 'gpsdump' was not declared in this scope

You get the errors because you did not call the functions by their correct names

void gpsdump_speed_alt(TinyGPS &gps)
void gpsdump_position(TinyGPS &gps)

Here is a Serial print version (with different software serial pins) of your sketch with a screen switch button. Because of some blocking functions in the gps code, the button is a little slow, and you may need to hold it down for at least one second to see the screen switch.

#include <SoftwareSerial.h>
SoftwareSerial nss(6, 7);//software serial pins for my gps shield
#include <TinyGPS.h>
TinyGPS gps;

int inPin = 11;//screen switch variables
byte val = 0;
byte oldVal = 0;
unsigned long debouncePeriod = 20;
boolean firstScreen = true;

void setup() {

  pinMode(inPin, INPUT_PULLUP);
  nss.begin(9600);
  Serial.begin(115200);
  delay(1000);
}

void loop() {
  bool newdata = false;
  unsigned long start = millis();
  while (millis() - start < 1000) { 
    if (feedgps())
      newdata = true;
  }
  screenSwitch();//read screen control button

  if (newdata && firstScreen)
  {
    gpsdump_position(gps);
  }
   if (newdata && !firstScreen)
  {
    gpsdump_speed_alt(gps);
  }
}

void gpsdump_speed_alt(TinyGPS &gps)
{
  float falt;
  float fmph = gps.f_speed_mph();
  float faltitude = (gps.f_altitude());
  faltitude = (faltitude * 3.2808);

  print_date(gps);
  Serial.print("Speed: ");
  Serial.println(fmph);

  Serial.print("Altitude: ");
  Serial.println(faltitude);
  Serial.println();
}

void gpsdump_position(TinyGPS &gps)
{
  float flat, flon;
  gps.f_get_position(&flat, &flon);
  Serial.print("Lat: ");
  Serial.println(flat, 6);
  Serial.print("Lon: ");
  Serial.println(flon, 6);
  Serial.println();
}

//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;
  gps.crack_datetime(&year, &month, &day, &hour, &minute, &second);
  {
    char sz[32];
    sprintf(sz, "%02d/%02d/%02d  %02d:%02d:%02d", month, day, year, hour, minute, second);
    Serial.print(sz);
    Serial.println();
  }
}

void screenSwitch()
{
  val = digitalRead(inPin);    //read input value, and store it

  if ((val == LOW) && (oldVal == HIGH)) {    // check is button pressed and is change inputpullup logic
    delay(20); //blocking debounce routine using delay
    val = digitalRead(inPin); //read button again
    if ((val == LOW) && (oldVal == HIGH))//debounced reading
    {
      //toggle between screens
      if (firstScreen == true)
        firstScreen = false;
      else
        firstScreen = true;
    }
  }
  oldVal = val;
}

Here is a Serial print version (with different software serial pins) of your sketch with a screen switch button. Because of some blocking functions in the gps code, the button is a little slow, and you may need to hold it down for at least one second to see the screen switch.

I Used your code , and edited it to make it work for my display, but a problem I now ran into is that the latitude, and longitude are being displayed as 0.000000, and I can't seem to figure what the problem might be, the mods to the circuit is the button between pins 11 and ground, and a 1K pull up in between pin 11 and VCC.

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>    

#define OLED_RESET 5
Adafruit_SSD1306 display(OLED_RESET);

SoftwareSerial nss(3, 4);//software serial pins for my gps shield
TinyGPS gps;

int inPin = 11;//screen switch variables
byte val = 0;
byte oldVal = 0;
unsigned long debouncePeriod = 20;
boolean firstScreen = true;

void setup() {

  pinMode(inPin, INPUT_PULLUP);
  nss.begin(9600);
  
    unsigned long age, date, time, chars = 0;
    
   display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
    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
}

void loop() {
  bool newdata = false;
  unsigned long start = millis();
  while (millis() - start < 1000) { 
    if (feedgps())
      newdata = true;
  }
  screenSwitch();//read screen control button

  if (newdata && firstScreen)
  {
    display.clearDisplay();
    gpsdump_position(gps);
  }
   if (newdata && !firstScreen)
  {
    display.clearDisplay();
    gpsdump_speed_alt(gps);
  }
}

void gpsdump_speed_alt(TinyGPS &gps)
{
  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(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(); 
}

void gpsdump_position(TinyGPS &gps)
{
  
  float flat, flon;
  
 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(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); 
  display.println();
  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();
}

//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;
  gps.crack_datetime(&year, &month, &day, &hour, &minute, &second);
  {
    char sz[32];
    sprintf(sz, "%02d/%02d/%02d  %02d:%02d:%02d", month, day, year, hour, minute, second);
    display.print(sz);
    display.println();
  }
}

void screenSwitch()
{
  val = digitalRead(inPin);    //read input value, and store it

  if ((val == LOW) && (oldVal == HIGH)) {    // check is button pressed and is change inputpullup logic
    delay(20); //blocking debounce routine using delay
    val = digitalRead(inPin); //read button again
    if ((val == LOW) && (oldVal == HIGH))//debounced reading
    {
      //toggle between screens
      if (firstScreen == true)
        firstScreen = false;
      else
        firstScreen = true;
    }
  }
  oldVal = val;
}
void gpsdump_position(TinyGPS &gps)
{
  
  float flat, flon;
  
 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(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); 
  display.println();
  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();
}

You forgot this line

  gps.f_get_position(&flat, &flon);

The first two lines of the function should be

void gpsdump_position(TinyGPS &gps)
{
  float flat, flon;
  gps.f_get_position(&flat, &flon);

the mods to the circuit is the button between pins 11 and ground, and a 1K pull up in between pin 11 and VCC.

The button is between pin 11 and ground, but just using the internal pullup on the pin. No extra resistors.

 pinMode(inPin, INPUT_PULLUP);

You forgot this line

ahh yes, That's seems to have fixed it, it's working the way I want it to now. Thak you for all the help I do appreciate it. :slight_smile:

btw, here is my code.