I'm working on a simple project to display GPS data (from a cheap GY-NEO6MV2) on a 128x32 OLED display with a UNO R3.
I'm been able to correctly display and constantly update the data I want (speed, altitude, satellites, hdop), but as I was trying to set a default value for when the GPS data is not updated, I've failed miserably.
I don't even know what I don't understand. Here's the setup:
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
// GPS
static const int RXPin = 3, TXPin = 4;
static const uint32_t GPSBaud = 9600;
int altitude;
// The TinyGPS++ object
TinyGPSPlus gps;
// The serial connection to the GPS device
SoftwareSerial ss(RXPin, TXPin);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
ss.begin(GPSBaud);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;)
; // Don't proceed, loop forever
}
// Clear the buffer and set default color
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.cp437(true);
display.display();
}
void loop() {
// put your main code here, to run repeatedly:
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0, 0);
// GPS read and values update
if (ss.available() > 0) {
gps.encode(ss.read());
if (gps.altitude.isUpdated()) {
altitude = gps.altitude.meters();
// Altitude update
display.print(altitude);
}
} else {
display.print("---");
}
display.display();
}
I can only display "---" for a brief moment and then the sreen goes black again. So I guess that it can read the serial at first and then
if (ss.available() > 0)
is not verified anymore, but I've tried to print raw data on the serial monitor and I can see it's correctly updated periodically. I've also try to switch to hardware serial but I have the same problem.
It's also very weird that in my first attempt, where I only displayed the altitude without trying to print the "---" when it's not updated, it worked flawlessly:
if (gps.altitude.isUpdated()) {
altitude = gps.altitude.meters();
// Altitude update
display.fillRect(0, 0, 50, 8, SSD1306_BLACK); // To only delete the altitude
display.setTextSize(1);
display.setCursor(0, 0);
display.print(altitude);
display.display();
}
Here, when I tried to put the "else" condition, the altitude was correctly replaced by the "---" when the signal was lost, but it would not show back when the signal was re-established (red LED blinking on the GPS board).
So as you can see same code but different problems, I'm very confused and anything will help :)