Search code examples
c++arduinogpslocation

RE: Arduino with neo 6m GPS and push button


I am an aeronautical student, new to the coding environment. I'm currently working on a GPS neo 6m module with Arduino mega 2560, where I wanted to save the current location upon pressing the push button. Which function is to be used to save the location by pressing the push button. Here is what I have done so far. Any help would be much appreciated. Thanks in advance.

#include <SoftwareSerial.h>
// The TinyGPS++ object
TinyGPSPlus gps;
static const int RXPin = 4, TXPin = 3; //gps module connections
static const uint32_t GPSBaud = 9600;
// The serial connection to the GPS device
SoftwareSerial ss(RXPin, TXPin);

const int PUSH_BUTTON = 2;

void setup(){
    pinMode(PUSH_BUTTON, INPUT_PULLUP); //push button input
    
    Serial.begin(9600);
    ss.begin(GPSBaud);
} 

void loop(){
  unsigned char i;
  static const double homeLat = 12.334455, homeLon = 05.112233;


  while (ss.available() > 0){
    gps.encode(ss.read());
    if (gps.location.isUpdated()){
      Serial.print("Latitude= "); 
      Serial.print(gps.location.lat(), 6);
      Serial.print(" Longitude= "); 
      Serial.println(gps.location.lng(), 6);
    }

    
    status = digitalRead(PUSH_BUTTON);
    if (status== HIGH){
          *missing code/confused*

    }  
  
    delay(1000);
  }
}```

Solution

  • It is simple just store the values in 2 variables.

        #include <SoftwareSerial.h>
        // The TinyGPS++ object
        TinyGPSPlus gps;
        static const int RXPin = 4, TXPin = 3; //gps module connections
        static const uint32_t GPSBaud = 9600;
        // The serial connection to the GPS device
        SoftwareSerial ss(RXPin, TXPin);
        
        const int PUSH_BUTTON = 2;
        double save_lat, save_lang;
        void setup(){
            pinMode(PUSH_BUTTON, INPUT_PULLUP); //push button input
            
            Serial.begin(9600);
            ss.begin(GPSBaud);
        } 
        
        void loop(){
          unsigned char i;
          static const double homeLat = 12.334455, homeLon = 05.112233;
        
        
          while (ss.available() > 0){
            gps.encode(ss.read());
            if (gps.location.isUpdated()){
              Serial.print("Latitude= "); 
              Serial.print(gps.location.lat(), 6);
              Serial.print(" Longitude= "); 
              Serial.println(gps.location.lng(), 6);
            }
        
            
            status = digitalRead(PUSH_BUTTON);
            if (status== HIGH){
     if (gps.location.isUpdated()){
                 save_lat = gps.location.lat();
        save_lang = gps.location.lng();
            }  
          }
            delay(1000);
          }
        }