I'm trying to connect a GPS unit (GY-GPS6MV2) and an OLED screen as in the diagram below (green connector is GPS; VCC, RX, TX, GND left to right):
Here's the code I've come up with:
#include <Wire.h>
#include <Adafruit_SH1106.h>
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#define OLED_RESET 4
Adafruit_SH1106 display(OLED_RESET);
static const int RXPin = 5, TXPin = 3;
static const uint32_t GPSBaud = 9600;
float vkph;
float vmps;
TinyGPSPlus gps;
SoftwareSerial ss(RXPin, TXPin);
void setup(){
Serial.begin(9600);
ss.begin(GPSBaud);
display.begin(SH1106_SWITCHCAPVCC, 0x3C);
display.display();
delay(2000);
display.clearDisplay();
}
void loop(){
while (ss.available() > 0){
gps.encode(ss.read());
vkph = gps.speed.kmph();
vmps = gps.speed.mps();
if (gps.location.isUpdated()){
Serial.print("Position: ");
Serial.print(gps.location.lat(), 6);
Serial.print(" : ");
Serial.println(gps.location.lng(), 6);
Serial.print("Speed: ");
Serial.print(vmps);
Serial.print(" m/s");
Serial.print(", ");
Serial.print(vkph);
Serial.println( " km/hr");
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println("Hello"); // Changing this to display.println(vmps);
display.setTextSize(2);
display.setTextColor(WHITE);
display.println("World"); // Changing this to display.println(vkph);
display.display();
delay(1000);
}
}
}
The GPS outputs to the serial monitor fine, and the screen displays "Hello \n
World" as expected. But when I make the changes indicated in the comments to the display part of the code, the display just shows its splash screen, flashing when the GPS updates.
How can I get the display to show the speeds in m/s and km/hr?