Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <LiquidCrystal_I2C.h> //I2C lcd library
- LiquidCrystal_I2C lcd(0x27, 20, 4); //define I2C address of lcd and define lcd type as twenty characters, four lines
- #include <NMEAGPS.h> //include GPS library
- NMEAGPS gps;
- #define gps_port Serial
- gps_fix currentFix;
- const int menuPin = 4; //define pin 4 as the button to cycle the menu
- byte menuState = 0; //current state of menu button press
- byte menuLastState = 0; //last state of menu button press
- byte menuCounter = 0; //create to store number of presses of menu button
- void setup() {
- pinMode(menuPin, INPUT); //set the menu button pin to input
- digitalWrite(menuPin, HIGH); //set the menu button pin to high
- gps_port.begin( 9600 ); //start the gps port
- lcd.init(); //start the lcd
- lcd.backlight(); //set the backlight on
- lcd.setCursor(0, 0);
- lcd.print( F("Initialising System") ); //start up message
- lcd.setCursor(0, 2);
- lcd.print(F("Please Wait..."));
- delay(3000); // display the start up message for three seconds
- lcd.clear(); //and then clear display
- }
- void loop(){
- while (gps.available( gps_port )) { //while the GPS port is available,
- currentFix = gps.read(); //read the GPS
- }
- menu(); //run the menu function
- }
- void menu() {
- menuState = digitalRead(menuPin); //check if the menu button has been pressed
- if(menuState != menuLastState)
- {
- if(menuState == 1)
- {
- menuCounter++; //increment the counter
- }
- if (menuState == 1)
- {
- if(menuCounter==1) {
- time(); //run the time function
- }
- if(menuCounter==2) {
- position(); //run the position function
- }
- if(menuCounter==3) {
- speed(); //run the speed function
- }
- if(menuCounter==4){
- engine(); //place holder, currently non functional
- }
- if(menuCounter==5){
- weather(); //place holder, currently non functional
- }
- if(menuCounter>4) menuCounter=0; //reset the counter to go back to the start
- }
- menuLastState = menuState; //remember the state of the button press
- delay(20); //short delay for debouncing
- }
- }
- void time() {
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print(F("Current Time & Date")); //display function title
- if (currentFix.valid.time) { //if the signal is valid
- lcd.setCursor(0,2);
- if (currentFix.dateTime.hours < 10) //to display a zero in front of single digit hours
- lcd.print( '0' );
- lcd.print(currentFix.dateTime.hours ); //display the hour
- lcd.print( ':' );
- if (currentFix.dateTime.minutes < 10) //to display a zero in front of single digit minutes
- lcd.print( '0' );
- lcd.print(currentFix.dateTime.minutes ); //display the minute
- lcd.print( ':' );
- if (currentFix.dateTime.seconds < 10) //to display a zero in front of single digit seconds
- lcd.print( '0' );
- lcd.print(currentFix.dateTime.seconds ); //display the seconds
- lcd.print(F(" UTC")); //indicate time displayed is "UTC"
- lcd.setCursor(0, 3);
- if (currentFix.dateTime.date < 10) //to display zero in front of single digit date
- lcd.print( '0' );
- lcd.print(currentFix.dateTime.date ); //display date
- lcd.print( '/' );
- if (currentFix.dateTime.month < 10) //to display zero in front of single digit month
- lcd.print( '0' );
- lcd.print(currentFix.dateTime.month ); //display month
- lcd.print( '/' );
- if (currentFix.dateTime.year < 10) //to display zero in front of single digit year
- lcd.print( '0' );
- lcd.print(currentFix.dateTime.year ); //display year
- }
- }
- void position() {
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print(F("Position & Altitude")); //display function title
- if (currentFix.valid.altitude) { //if signal is valid
- lcd.setCursor(0, 1);
- lcd.print(currentFix.altitude(), 2); //display altitude
- lcd.print(F(" Metres")); //in metres
- }
- if (currentFix.valid.location) { //if signal is valid
- lcd.setCursor(0, 2);
- lcd.print(currentFix.latitude(), 6); //display latitude to six decimal places
- lcd.print(F(" Latitude"));
- lcd.setCursor(0, 3);
- lcd.print(currentFix.longitude(), 6); //display longitude to six decimal places
- lcd.print(F(" Longitude"));
- }
- }
- void speed() {
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print(F("Speed & Heading")); //display function title
- if (currentFix.valid.speed) { //if signal is valid
- lcd.setCursor(0, 2);
- lcd.print(currentFix.speed_mph(), 2); //display speed to two decimal places
- lcd.print(F(" MPH")); //in miles per hour
- lcd.setCursor(0, 3);
- lcd.print(currentFix.heading(), 3); //display heading to three decimal places
- lcd.print(F(" DEG")); //in degrees
- }
- }
- void engine(){ //Temporary placeholder dummy function
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print(F("Engine Monitoring"));
- lcd.setCursor(0, 1);
- lcd.print(F("Coolant Temp:"));
- lcd.setCursor(0, 2);
- lcd.print(F("Eng' Oil Temp:"));
- lcd.setCursor(0, 3);
- lcd.print(F("G'box Oil Temp:"));
- }
- void weather(){ //Temporary placeholder dummy function
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print(F("Weather Conditions"));
- lcd.setCursor(0, 1);
- lcd.print(F("Temperature:"));
- lcd.setCursor(0, 2);
- lcd.print(F("Pressure:"));
- lcd.setCursor(0, 3);
- lcd.print(F("Humidity:"));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement