Finally answered some programming fundamentals for myself! The detailed declarations outside of void setup(){} and loop(){} are effectively code modules that can then called as condensed functions later e.g in the main loop (if you want)..duh.
I got the oled char code to get going from the Adafruit GFX example.
The most important thing I've learned now is code structure from top down as read during compile time - so why it fails when code is not called in order of dependency! (yep! I'm not a programmer!)
I've tidied this to a format that works for me to keep track of steps (especially visually useful is a section "}- // end xxx" ) in the clear, logical order that the program loads and runs by with comments on terms and their- function to aid learning - dunno if I'm using the right terminology here, but the order is:
title/usage comments; #includes; defines; global constants/variables; setup body; Control Structures{}; loop body containing desired functions()/code modules
/******************************************************************************
Steve_GPS_OLED.ino to display data in serialmon and OLED, adapted from:
TinyGPSPlus_GPS_Shield.ino
TinyGPS++ Library Example for the SparkFun GPS Logger Shield
By Jim Lindblom @ SparkFun Electronics
February 9, 2016
https://stevepedwards.today/github.com/sparkfun/GPS_Shield
This example uses SoftwareSerial to communicate with the GPS module on
pins 8 and 9. It uses the TinyGPS++ library to parse the NMEA strings sent
by the GPS module, and prints interesting GPS information to the serial
monitor.
After uploading the code, open your serial monitor, set it to 9600 baud, and
watch for latitude, longitude, altitude, course, speed, date, time, and the
number of visible satellites.
Resources:
TinyGPS++ Library - https://stevepedwards.today/github.com/mikalhart/TinyGPSPlus/releases
SoftwareSerial Library
Development/hardware environment specifics:
Arduino IDE 1.6.7
GPS Logger Shield v2.0 - Make sure the UART switch is set to SW-UART
Arduino Uno, RedBoard, Pro, etc.
OLED pins from front are 1 to Nano 5v, 2 to Nano pin GND, 3 to Nano pin A4, 4 to Nano pin A5;
Neo7 GPS pins from front are pin 1 to Nano D9, pin2 to D8, pin 3 to GND, pin 4 to Nano 5V;
******************************************************************************/
//#include is used to include outside libraries in your sketch.
// 0/1 UART for programming/Serial monitor-ing, use SoftwareSerial:
#include <SoftwareSerial.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <TinyGPS++.h> // Include the TinyGPS++ library
// #define is a useful C component that allows the programmer to give a name to a
//constant value before the program is compiled.
#define ARDUINO_GPS_RX 9 // GPS TX, Arduino RX pin
#define ARDUINO_GPS_TX 8 // GPS RX, Arduino TX pin
#define gpsPort ssGPS // Alternatively, use Serial1 on the Leonardo
// Define the serial monitor port. On the Uno, and Leonardo this is 'Serial'
// on other boards this may be 'SerialUSB'
#define SerialMonitor Serial
#define GPS_BAUD 9600 // GPS module baud rate. GP3906 defaults to 9600.
// If you're using an Arduino Uno, RedBoard, or any board that uses the
// 0/1 UART for programming/Serial monitor-ing, use SoftwareSerial:
// OLED screen type parameters
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
TinyGPSPlus tinyGPS; // Create a TinyGPSPlus object
// NOTE: This define has to be placed below the Adafruit library it comes from as page is read top down!
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
SoftwareSerial ssGPS(ARDUINO_GPS_TX, ARDUINO_GPS_RX); // Create a SoftwareSerial
// Set gpsPort to either ssGPS if using SoftwareSerial or Serial1 if using an
// Arduino with a dedicated hardware serial port
// Integers are your primary data-type for number storage.
// (minimum value of -2^15 and a maximum value of (2^15) - 1)
// Arduino takes care of dealing with negative numbers for you
int m = 0; // global constant to reserve mem for this data, feet to meters later
// The void keyword is used only in function declarations.
// It indicates that the function is expected
// to return no information to the function from which it was called.
// The setup() function is called when a sketch starts. Use it to initialize
//variables, pin modes, start using libraries, etc. The setup function will only
//run once,after each powerup or reset of the Arduino board.
void setup() {
SerialMonitor.begin(9600);
gpsPort.begin(GPS_BAUD);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32
Serial.println(F("SSD1306 allocation failed"));
// Address 0x3C for 128x32
for(;;); // Don't proceed, loop forever
}
} // end setup
// formats the raw GPS data to readable form using TinyGPS++ Lib
void printGPSInfo() {
// Print latitude, longitude, altitude in feet, course, speed, date, time,
// and the number of visible satellites.
SerialMonitor.print("Lat: "); SerialMonitor.println(tinyGPS.location.lat(), 6);
SerialMonitor.print("Long: "); SerialMonitor.println(tinyGPS.location.lng(), 6);
SerialMonitor.print("Alt: "); SerialMonitor.print(tinyGPS.altitude.feet()); SerialMonitor.println(" feet");
int m = (tinyGPS.altitude.feet()) / 3.28084;
SerialMonitor.print("Alt: "); SerialMonitor.print(m); SerialMonitor.println(" meters");
SerialMonitor.print("Date: "); printDate();
SerialMonitor.print("Time: "); printTime();
SerialMonitor.print("Sats: "); SerialMonitor.println(tinyGPS.satellites.value());
SerialMonitor.println();
} //end printGPSInfo()
void printGPS_OLED() {
// OLED screen page 1
// Print latitude, longitude, altitude in feet, course, speed, date, time,
// and the number of visible satellites.
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.cp437(true); // Use full 256 char 'Code Page 437' font
display.print("Lat: "); display.println(tinyGPS.location.lat(), 6);
display.print("Long: "); display.println(tinyGPS.location.lng(), 6);
display.print("Alt: "); display.print(tinyGPS.altitude.feet()); display.println(" feet");
int m = (tinyGPS.altitude.feet()) / 3.28084;
display.print("Alt: "); display.print(m); display.print(" meters");
display.display();
delay(3000);
// OLED screen page 2
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.cp437(true); // Use full 256 char 'Code Page 437' font
display.print("Date: "); display.println(); //display.printDate();
display.print("Time: "); display.println(); //display.printTime();
display.print("Sats: "); display.println(tinyGPS.satellites.value());
display.println();
display.display();
delay(3000);
} // end printGPS_OLED()
// This custom version of delay() ensures that the tinyGPS object
// is being "fed". From the TinyGPS++ examples.
static void smartDelay(unsigned long ms) {
unsigned long start = millis();
do
{
// If data has come in from the GPS module
while (gpsPort.available())
tinyGPS.encode(gpsPort.read()); // Send it to the encode function
// tinyGPS.encode(char) continues to "load" the tinGPS object with new
// data coming in from the GPS module. As full NMEA strings begin to come in
// the tinyGPS library will be able to start parsing them for pertinent info
} while (millis() - start < ms);
} // end smartDelay()
// printDate() formats the GPS date into dd/mm/yy.
void printDate() {
SerialMonitor.print(tinyGPS.date.day());
SerialMonitor.print("/");
SerialMonitor.print(tinyGPS.date.month());
SerialMonitor.print("/");
SerialMonitor.println(tinyGPS.date.year());
} // end printDate()
// printTime() formats the GPS time into "hh:mm:ss", and prints leading 0's
// where they're called for.
void printTime() {
SerialMonitor.print(tinyGPS.time.hour());
SerialMonitor.print(":");
if (tinyGPS.time.minute() < 10) SerialMonitor.print('0');
SerialMonitor.print(tinyGPS.time.minute());
SerialMonitor.print(":");
if (tinyGPS.time.second() < 10) SerialMonitor.print('0');
SerialMonitor.println(tinyGPS.time.second());
} // end printTime()
// After creating a setup() function, which initializes and sets the initial values,
// loop section loops forever so contained functions from above update new data values from GPS
// and print to serialmon and OLED
void loop() {
printGPS_OLED();
printGPSInfo();
// "Smart delay" looks for GPS data while the Arduino's not doing anything else
smartDelay(1000);
} //end loop
The SerialMonitor.printDate(); / SerialMonitor.printTime(); cannot be called as display.printDate() or display.printTime(); for the OLED, as it is not in the Adafruit library, so I'm stuck there for a bit to know combining library functions...?
hmm.. think I have to write my own library addition for that?
https://www.arduino.cc/en/Hacking/LibraryTutorial
No, I just had to repeat the long format of date and time in the printGPS_OLED() function:
/******************************************************************************
Steve_GPS_OLED.ino to display data in serialmon and OLED, adapted from:
TinyGPSPlus_GPS_Shield.ino
TinyGPS++ Library Example for the SparkFun GPS Logger Shield
By Jim Lindblom @ SparkFun Electronics
February 9, 2016
https://stevepedwards.today/github.com/sparkfun/GPS_Shield
This example uses SoftwareSerial to communicate with the GPS module on
pins 8 and 9. It uses the TinyGPS++ library to parse the NMEA strings sent
by the GPS module, and prints interesting GPS information to the serial
monitor.
After uploading the code, open your serial monitor, set it to 9600 baud, and
watch for latitude, longitude, altitude, course, speed, date, time, and the
number of visible satellites.
Resources:
TinyGPS++ Library - https://stevepedwards.today/github.com/mikalhart/TinyGPSPlus/releases
SoftwareSerial Library
Development/hardware environment specifics:
Arduino IDE 1.6.7
GPS Logger Shield v2.0 - Make sure the UART switch is set to SW-UART
Arduino Uno, RedBoard, Pro, etc.
OLED pins from front are 1 to Nano 5v, 2 to Nano pin GND, 3 to Nano pin A4, 4 to Nano pin A5;
Neo7 GPS pins from front are pin 1 to Nano D9, pin2 to D8, pin 3 to GND, pin 4 to Nano 5V;
******************************************************************************/
//#include is used to include outside libraries in your sketch.
// 0/1 UART for programming/Serial monitor-ing, use SoftwareSerial:
#include <SoftwareSerial.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <TinyGPS++.h> // Include the TinyGPS++ library
// #define is a useful C component that allows the programmer to give a name to a
//constant value before the program is compiled.
#define ARDUINO_GPS_RX 9 // GPS TX, Arduino RX pin
#define ARDUINO_GPS_TX 8 // GPS RX, Arduino TX pin
#define gpsPort ssGPS // Alternatively, use Serial1 on the Leonardo
// Define the serial monitor port. On the Uno, and Leonardo this is 'Serial'
// on other boards this may be 'SerialUSB'
#define SerialMonitor Serial
#define GPS_BAUD 9600 // GPS module baud rate. GP3906 defaults to 9600.
// If you're using an Arduino Uno, RedBoard, or any board that uses the
// 0/1 UART for programming/Serial monitor-ing, use SoftwareSerial:
// OLED screen type parameters
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
TinyGPSPlus tinyGPS; // Create a TinyGPSPlus object
// NOTE: This define has to be placed below the Adafruit library it comes from as page is read top down!
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
SoftwareSerial ssGPS(ARDUINO_GPS_TX, ARDUINO_GPS_RX); // Create a SoftwareSerial
// Set gpsPort to either ssGPS if using SoftwareSerial or Serial1 if using an
// Arduino with a dedicated hardware serial port
// Integers are your primary data-type for number storage.
// (minimum value of -2^15 and a maximum value of (2^15) - 1)
// Arduino takes care of dealing with negative numbers for you
int m = 0; // global constant to reserve mem for this data, feet to meters later
// The void keyword is used only in function declarations.
// It indicates that the function is expected
// to return no information to the function from which it was called.
// The setup() function is called when a sketch starts. Use it to initialize
//variables, pin modes, start using libraries, etc. The setup function will only
//run once,after each powerup or reset of the Arduino board.
void setup() {
SerialMonitor.begin(9600);
gpsPort.begin(GPS_BAUD);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32
Serial.println(F("SSD1306 allocation failed"));
// Address 0x3C for 128x32
for(;;); // Don't proceed, loop forever
}
} // end setup
// formats the raw GPS data to readable form using TinyGPS++ Lib
void printGPSInfo() {
// Print latitude, longitude, altitude in feet, course, speed, date, time,
// and the number of visible satellites.
SerialMonitor.print("Lat: "); SerialMonitor.println(tinyGPS.location.lat(), 6);
SerialMonitor.print("Long: "); SerialMonitor.println(tinyGPS.location.lng(), 6);
SerialMonitor.print("Alt: "); SerialMonitor.print(tinyGPS.altitude.feet()); SerialMonitor.println(" feet");
int m = (tinyGPS.altitude.feet()) / 3.28084;
SerialMonitor.print("Alt: "); SerialMonitor.print(m); SerialMonitor.println(" meters");
SerialMonitor.print("Date: "); printDate();
SerialMonitor.print("Time: "); printTime();
SerialMonitor.print("Sats: "); SerialMonitor.println(tinyGPS.satellites.value());
SerialMonitor.println();
} //end printGPSInfo()
void printGPS_OLED() {
// OLED screen page 1
// Print latitude, longitude, altitude in feet, course, speed, date, time,
// and the number of visible satellites.
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.cp437(true); // Use full 256 char 'Code Page 437' font
display.print("Lat: "); display.println(tinyGPS.location.lat(), 6);
display.print("Long: "); display.println(tinyGPS.location.lng(), 6);
display.print("Alt: "); display.print(tinyGPS.altitude.feet()); display.println(" feet");
int m = (tinyGPS.altitude.feet()) / 3.28084;
display.print("Alt: "); display.print(m); display.print(" meters");
display.display();
delay(3000);
// OLED screen page 2
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.cp437(true); // Use full 256 char 'Code Page 437' font
display.print("Date: "); display.print(tinyGPS.date.day());
display.print("/");
display.print(tinyGPS.date.month());
display.print("/");
display.println(tinyGPS.date.year());
display.print("Time: "); display.print(tinyGPS.time.hour());
display.print(":");
if (tinyGPS.time.minute() < 10) display.print('0');
display.print(tinyGPS.time.minute());
display.print(":");
if (tinyGPS.time.second() < 10) display.print('0');
display.println(tinyGPS.time.second());
display.print("Sats: "); display.println(tinyGPS.satellites.value());
display.println();
display.display();
delay(3000);
} // end printGPS_OLED()
// This custom version of delay() ensures that the tinyGPS object
// is being "fed". From the TinyGPS++ examples.
static void smartDelay(unsigned long ms) {
unsigned long start = millis();
do
{
// If data has come in from the GPS module
while (gpsPort.available())
tinyGPS.encode(gpsPort.read()); // Send it to the encode function
// tinyGPS.encode(char) continues to "load" the tinGPS object with new
// data coming in from the GPS module. As full NMEA strings begin to come in
// the tinyGPS library will be able to start parsing them for pertinent info
} while (millis() - start < ms);
} // end smartDelay()
// printDate() formats the GPS date into dd/mm/yy.
void printDate() {
SerialMonitor.print(tinyGPS.date.day());
SerialMonitor.print("/");
SerialMonitor.print(tinyGPS.date.month());
SerialMonitor.print("/");
SerialMonitor.println(tinyGPS.date.year());
} // end printDate()
// printTime() formats the GPS time into "hh:mm:ss", and prints leading 0's
// where they're called for.
void printTime() {
SerialMonitor.print(tinyGPS.time.hour());
SerialMonitor.print(":");
if (tinyGPS.time.minute() < 10) SerialMonitor.print('0');
SerialMonitor.print(tinyGPS.time.minute());
SerialMonitor.print(":");
if (tinyGPS.time.second() < 10) SerialMonitor.print('0');
SerialMonitor.println(tinyGPS.time.second());
} // end printTime()
// After creating a setup() function, which initializes and sets the initial values,
// loop section loops forever so contained functions from above update new data values from GPS
// and print to serialmon and OLED
void loop() {
printGPS_OLED();
printGPSInfo();
// "Smart delay" looks for GPS data while the Arduino's not doing anything else
smartDelay(1000);
} //end loop