Created
May 14, 2021 20:10
-
-
Save LorisTecnology/93fe337c998afdd3243c86c624d2b7b4 to your computer and use it in GitHub Desktop.
Arduino Sensors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <TinyGPS++.h> | |
| #include <SoftwareSerial.h> | |
| // Choose two Arduino pins to use for software serial | |
| int RXPin = 2; | |
| int TXPin = 3; | |
| int GPSBaud = 9600; | |
| // Create a TinyGPS++ object | |
| TinyGPSPlus gps; | |
| // Create a software serial port called "gpsSerial" | |
| SoftwareSerial gpsSerial(RXPin, TXPin); | |
| void setup() | |
| { | |
| // Start the Arduino hardware serial port at 9600 baud | |
| Serial.begin(9600); | |
| // Start the software serial port at the GPS's default baud | |
| gpsSerial.begin(GPSBaud); | |
| } | |
| void loop() | |
| { | |
| // This sketch displays information every time a new sentence is correctly encoded. | |
| if (gpsSerial.available() > 0){ | |
| if (gps.encode(gpsSerial.read())) | |
| displayInfo(); | |
| } | |
| // If 5000 milliseconds pass and there are no characters coming in | |
| // over the software serial port, show a "No GPS detected" error | |
| if (millis() > 5000 && gps.charsProcessed() < 10) | |
| { | |
| Serial.println("No GPS detected"); | |
| while(true); | |
| } | |
| } | |
| void displayInfo() | |
| { | |
| if (gps.location.isValid()) | |
| { | |
| Serial.print("Latitude: "); | |
| Serial.println(gps.location.lat(), 6); | |
| Serial.print("Longitude: "); | |
| Serial.println(gps.location.lng(), 6); | |
| Serial.print("Altitude: "); | |
| Serial.println(gps.altitude.meters()); | |
| } | |
| else | |
| { | |
| Serial.println("Location: Not Available"); | |
| } | |
| Serial.print("Date: "); | |
| if (gps.date.isValid()) | |
| { | |
| Serial.print(gps.date.month()); | |
| Serial.print("/"); | |
| Serial.print(gps.date.day()); | |
| Serial.print("/"); | |
| Serial.println(gps.date.year()); | |
| } | |
| else | |
| { | |
| Serial.println("Not Available"); | |
| } | |
| Serial.print("Time: "); | |
| if (gps.time.isValid()) | |
| { | |
| if (gps.time.hour() < 10) Serial.print(F("0")); | |
| Serial.print(gps.time.hour()); | |
| Serial.print(":"); | |
| if (gps.time.minute() < 10) Serial.print(F("0")); | |
| Serial.print(gps.time.minute()); | |
| Serial.print(":"); | |
| if (gps.time.second() < 10) Serial.print(F("0")); | |
| Serial.print(gps.time.second()); | |
| Serial.print("."); | |
| if (gps.time.centisecond() < 10) Serial.print(F("0")); | |
| Serial.println(gps.time.centisecond()); | |
| } | |
| else | |
| { | |
| Serial.println("Not Available"); | |
| } | |
| Serial.println(); | |
| Serial.println(); | |
| delay(1000); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment