I am trying to build a simple GPS device that displays the info received from the GPS module on a 16x2 LCD display. The NMEA string includes a lot of different values, and I'm attempting to cycle through what value is displayed on the LCD. I have the pin layed out for the button, and I got as far as trying to get the LCD to display 2 different data depending on whether the button is being pressed or not. Ideally a single button press cycles through what is shown on the LCD, but I'm confused as to how to go about coding this. I tried google searches, though maybe I don't know what exactly to look for. So I turn to the more experienced people here in the hopes of receiving some guidance
#include <TinyGPS.h>
#include <LiquidCrystal.h>
TinyGPS gps;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
long lat, lon;
unsigned long time, date;
const int buttonPin = 7;
int buttonState = 0;
void setup(){
 lcd.begin(16, 2);
 lcd.setCursor(5, 0);
 lcd.print("NAVIAN");
 lcd.setCursor(6, 1);
 lcd.print("GPS");
 delay ( 2000 );
 pinMode(buttonPin, INPUT);
 Serial.begin(4800);
Â
}
void loop(){
 read();
 buttonState = digitalRead(buttonPin);
 switch (buttonState){
  case HIGH:{
   printDATETIME();
   break;
  }
  case LOW:{
   printPOS();
  }
 }
}Â
Â
void read(){
 while (Serial.available())
 {
  int c = Serial.read();
  if (gps.encode(c))
  {
   gps.get_position(&lat, &lon);
   gps.get_datetime(&date, &time);
  }
 }
}
void printPOS(){
 lcd.setCursor(0, 0);
 lcd.print("LAT:");
 lcd.print(lat);
 lcd.print("    ");
 lcd.setCursor(0, 1);
 lcd.print("LON:");
 lcd.print(lon);
 lcd.print("    ");
}
void printALTTIME(){
 lcd.setCursor(0, 0);
 lcd.print("ALT:");
 lcd.print(gps.f_altitude());
 lcd.setCursor(11, 0);
 lcd.print("m  ");
 lcd.setCursor(0, 1);
 lcd.print(time);
 lcd.print("     ");
}
void printALTDATE(){
 lcd.setCursor(0, 0);
 lcd.print("AlT:");
 lcd.print(gps.f_altitude());
 lcd.setCursor(11, 0);
 lcd.print("m  ");
 lcd.setCursor(0, 1);
 lcd.print(date);
 lcd.print("     ");
}
void printDATETIME(){
 lcd.setCursor(0, 0);
 lcd.print(time);
 lcd.print("     ");
 lcd.setCursor(0, 1);
 lcd.print(date);
 lcd.print("     ");
}
Ideally a single button press cycles through what is shown on the LCD, but I'm confused as to how to go about coding this
You could look at the state change detection example, to see how to detect that a switch has changed state.
Each time that happens, increment a counter. Reset it when it exceeds the number of items to display.
Display the nth item when the counter value is n. Use a switch statement and cases.
read();
Read what? Good function names consist of a noun and verb and they describe EXACTLY what the function does. Bad function names, like yours, don't mean squat.
#include <TinyGPS.h>
#include <LiquidCrystal.h>
TinyGPS gps;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
long lat, lon;
unsigned long time, date;
const int buttonPin = 8;
int buttonState = 0;
int buttonPushCounter = 0;
int lastButtonState = 0;
void setup(){
 lcd.begin(16, 2);
 lcd.setCursor(5, 0);
 lcd.print("NAVIAN");
 lcd.setCursor(6, 1);
 lcd.print("GPS");
 delay ( 2000 );
 pinMode(buttonPin, INPUT);
 Serial.begin(4800);
Â
}
void loop(){
 readGPS();
 click();
 switch (buttonPushCounter) {
  case 0:
  printPOS();
  break;
  case 1:
  printALTTIME();
  break;
  case 2:
  printALTDATE();
  break;
 }
}Â Â Â
 Â
Â
void readGPS(){
 while (Serial.available())
 {
  int c = Serial.read();
  if (gps.encode(c))
  {
   gps.get_position(&lat, &lon);
   gps.get_datetime(&date, &time);
  }
 }
}
void printPOS(){
 lcd.setCursor(0, 0);
 lcd.print("LAT:");
 lcd.print(lat);
 lcd.print("    ");
 lcd.setCursor(0, 1);
 lcd.print("LON:");
 lcd.print(lon);
 lcd.print("    ");
}
void printALTTIME(){
 lcd.setCursor(0, 0);
 lcd.print("ALT:");
 lcd.print(gps.f_altitude());
 lcd.setCursor(11, 0);
 lcd.print("m     ");
 lcd.setCursor(0, 1);
 lcd.print(time);
 lcd.print("     ");
}
void printALTDATE(){
 lcd.setCursor(0, 0);
 lcd.print("AlT:");
 lcd.print(gps.f_altitude());
 lcd.setCursor(11, 0);
 lcd.print("m     ");
 lcd.setCursor(0, 1);
 lcd.print(date);
 lcd.print("     ");
}
void printDATETIME(){
 lcd.setCursor(0, 0);
 lcd.print(time);
 lcd.print("     ");
 lcd.setCursor(0, 1);
 lcd.print(date);
 lcd.print("     ");
}
Â
void click(){
 buttonState = digitalRead(buttonPin);
 if (buttonState != lastButtonState) {
  if (buttonState == HIGH) {
   buttonPushCounter += 1;
   if (buttonPushCounter ==3) {
   buttonPushCounter = 0;
   }
  }
 }
 lastButtonState = buttonState;
}
This results in the LCD madly flashing between coordinates etc.. And I have no idea why
This results in the LCD madly flashing between coordinates etc.. And I have no idea why
Possibly because your switch is not wired correctly. How is it wired?
It would be simpler to connect one side to ground and one side to the digital pin. Then, use INPUT_PULLUP as the type in the pinMode() call. Finally, LOW means pressed.