Good afternoon
There is a video of the two displays however I'm unsure how to link them here!
Hardware;
-Arduino UNO R3
-SSD1306
(Awaiting GPS module) not necessary for sketches.
Job spec;
To display 1xbitmap for 2 seconds then 1x text for 2 seconds then to initialise a GPS speedometer display and maintain that until power is cycled
-I have developed a code based on U8g2 library to display the picture then the text on loop. These are working fine when sent to the screen.
-I have also copied a code for a GPS speedometer (this works fine when sent to the screen but operates on U8G, not U8G2)
I've now managed to combine the two sketches (maybe) although I'm not sure what if anything will happen as I'm now out of dynamic memory and I'm not sure how to reduce it further to see if the program will run. Perhaps there's lines that're unnecessary now that I've combined the two codes or maybe there's a simpler way of displaying text/Bitmap that uses less memory.
I'm hoping to eventually move this to an arduino NANO as I'm needing everything to be extremely compact so i understand this may further my storage problems.
Looking forward to any help anyone can offer!
Thanks, Jack
#include <U8g2lib.h>
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#define OLED_ADDR 0x3C
#define time_delay 900
#include <SoftwareSerial.h>
#define rxPin 2
#define txPin 3 //unused
SoftwareSerial neogps(rxPin,txPin);
U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE);
void u8g2_prepare() {
u8g2.setFont(u8g2_font_maniac_tf);
u8g2.setFontRefHeightExtendedText();
u8g2.setDrawColor(2);
u8g2.setFontPosTop();
u8g2.setFontDirection(0);
u8g2.setDrawColor(WHITE);
u8g2.setCursor(20, 18);
u8g2.println(F("Caddy.\n"));
}
#define image_width 128
#define image_height 64
static const unsigned char image_bits[] U8X8_PROGMEM = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0x00, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x5f, 0x00, 0xfc, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x4f, 0x00, 0xf8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x67, 0x00, 0xe0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0x7f, 0x00, 0x00, 0x60, 0x01, 0x08, 0xe0, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0x7f, 0xfe, 0xff, 0x7f, 0xf9, 0xf7, 0x03, 0xfe, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0xfe, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0x7f, 0x3e, 0xe0, 0xff, 0xff, 0x37, 0xf0, 0xfe, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0x7f, 0x3e, 0xc0, 0xff, 0xfe, 0x17, 0xe0, 0xfe, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0x7f, 0x1e, 0xc0, 0xff, 0xfe, 0x07, 0x40, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0x7f, 0x1e, 0xc0, 0xff, 0xfe, 0x07, 0x60, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x20, 0xfc, 0x00, 0x00, 0x80, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, 0xf0, 0x00, 0x00, 0x38, 0xf0, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0x0f, 0x00, 0x00, 0x00, 0x00, 0xf0, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
void u8g2_bitmap() {
u8g2.drawXBMP(0, 5, image_width, image_height, image_bits);
}
#include <TinyGPS++.h> //1.0.3
TinyGPSPlus gps;
//---------------------------------------------------------------------------
int x_max = 128;
int y_max = 62;
int x_center = x_max/2;
int y_center = y_max/2+10;
int arc = y_max/2;
int angle = 0;
//---------------------------------------------------------------------------
int needle_pos = 0;
u8g2_uint_t xx = 0;
#define sat_logo_width 20
#define sat_logo_height 20
const unsigned char sat_logo[] = {
0x00, 0x01, 0x00, 0x80, 0x07, 0x00, 0xc0, 0x06, 0x00, 0x60, 0x30, 0x00,
0x60, 0x78, 0x00, 0xc0, 0xfc, 0x00, 0x00, 0xfe, 0x01, 0x00, 0xff, 0x01,
0x80, 0xff, 0x00, 0xc0, 0x7f, 0x06, 0xc0, 0x3f, 0x06, 0x80, 0x1f, 0x0c,
0x80, 0x4f, 0x06, 0x19, 0xc6, 0x03, 0x1b, 0x80, 0x01, 0x73, 0x00, 0x00,
0x66, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x70, 0x00, 0x00
};
double lat;
double lng;
String hour, minute;
int second;
int num_sat, speed;
String heading;
void gauge(uint8_t angle) {
u8g2.setFont(u8g2_font_maniac_tf);
//---------------------------------------------------------------------------
u8g2.drawCircle(x_center,y_center,arc+6, U8G2_DRAW_UPPER_RIGHT);
u8g2.drawCircle(x_center,y_center,arc+4, U8G2_DRAW_UPPER_RIGHT);
u8g2.drawCircle(x_center,y_center,arc+6, U8G2_DRAW_UPPER_LEFT);
u8g2.drawCircle(x_center,y_center,arc+4, U8G2_DRAW_UPPER_LEFT);
//---------------------------------------------------------------------------
u8g2.drawStr(20, 42, "0");
u8g2.drawStr(18, 29, "25");
u8g2.drawStr(28, 14, "50");
u8g2.drawStr(60, 14, "100");
u8g2.drawStr(91, 14, "150");
u8g2.drawStr(101, 29, "175");
u8g2.drawStr(105, 42, "200");
//---------------------------------------------------------------------------
u8g2.setCursor(57,25);
u8g2.print(F("MPH.\n"));
u8g2.setCursor(50,32);
u8g2.print(F("SPEED.\n"));
//---------------------------------------------------------------------------
float x1=sin(2*angle*2*3.14/360);
float y1=cos(2*angle*2*3.14/360);
u8g2.drawLine(x_center, y_center, x_center+arc*x1, y_center-arc*y1);
u8g2.drawDisc(x_center, y_center, 5, U8G2_DRAW_UPPER_LEFT);
u8g2.drawDisc(x_center, y_center, 5, U8G2_DRAW_UPPER_RIGHT);
//---------------------------------------------------------------------------
u8g2.drawXBM(0, 0, sat_logo_width, sat_logo_height, sat_logo);
u8g2.setCursor(18, 5);
u8g2.print(num_sat, 5);
//---------------------------------------------------------------------------
u8g2.setCursor(110, 5);
u8g2.print(heading);
//---------------------------------------------------------------------------
u8g2.setCursor(0, 55);
u8g2.print(lat, 4);
u8g2.setCursor(0, 62);
u8g2.print(lng, 4);
//---------------------------------------------------------------------------
//Display time
u8g2.setFont(u8g2_font_maniac_tf);
u8g2.setCursor(90, 65);
u8g2.print(hour);
if(second%2 == 0)
{u8g2.drawStr(104, 65, ":");}
else
{u8g2.drawStr(104, 65, " ");}
u8g2.setCursor(111, 65);
u8g2.print(minute);
u8g2.setFont(u8g_font_profont22);
u8g2.setCursor(54,60);
if (speed<10){
u8g2.print(F("0.\n"));
}
if (speed>99) {
u8g2.setCursor(47,60);
}
u8g2.print(speed);
}
int i = 200;
void setup(void) {
Serial.begin(9600);
neogps.begin(9600);
u8g2.setFont(u8g_font_chikita);
u8g2.setColorIndex(1);
}
void loop(void) {
needle_pos = map(speed,0,200,0,90);
xx = needle_pos;
if (xx<45)
{xx=xx+135;}
else
{xx=xx-45;}
{
u8g2.firstPage();
do {
gauge(xx);
}
while( u8g2.nextPage() );
}
}
void u8g2clearBuffer() {
u8g2.clearBuffer();
u8g2_prepare();
u8g2.sendBuffer();
delay(time_delay);
u8g2.clearBuffer();
u8g2.sendBuffer();
delay(time_delay);
// one tab
u8g2.clearBuffer();
u8g2_bitmap();
u8g2.sendBuffer();
delay(time_delay);
u8g2.clearBuffer();
u8g2.sendBuffer();
delay(time_delay);
}
void Read_GPS(){
//------------------------------------------------------------------
boolean newData = false;
for (unsigned long start = millis(); millis() - start < 1000;)
{
while (neogps.available())
{
if (gps.encode(neogps.read()))
{
newData = true;
break;
}
}
}
if(newData == true){
newData = false;
Get_GPS();
}
else {
}
}
void Get_GPS(){
num_sat = gps.satellites.value();
if (gps.location.isValid() == 1) {
speed = gps.speed.mph();
lat = gps.location.lat();
lng = gps.location.lng();
heading = gps.cardinal(gps.course.value());
}
if (gps.time.isValid()){
hour = String(gps.time.hour());
hour = (hour.length() == 1) ? "0"+hour : hour;
minute = String(gps.time.minute());
minute= (minute.length() == 1) ? "0"+minute : minute;
second = gps.time.second();
}
}


