adafruit library help

hi, i am working on a project that uses two adafruit products namely the GPS and OLED module. they use the respective libraries

#include <Adafruit_GPS.h>
#include <Adafruit_SSD1306.h>

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

the GPS and OLED both do not work

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GPS.h>

#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif


#include <SoftwareSerial.h>

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);


SoftwareSerial mySerial(2, 3);
Adafruit_GPS GPS(&mySerial);


#define GPSECHO  true

the program compiles, but when i add that line of code above, the GPS and OLED does not work. if i remove it, the GPS works but not the OLED.

Anybody have any ideas why this is happening?
thanks!

So how have you wired these up and how about posting actual code instead of a bunch of # defines?

hi, this is my code

// Test code for Adafruit GPS modules using MTK3329/MTK3339 driver
//
// This code shows how to listen to the GPS module in an interrupt
// which allows the program to have more 'freedom' - just parse
// when a new NMEA sentence is available! Then access data when
// desired.
//
// Tested and works great with the Adafruit Ultimate GPS module
// using MTK33x9 chipset
//    ------> http://www.adafruit.com/products/746
// Pick one up today at the Adafruit electronics shop 
// and help support open source hardware & software! -ada

//This code is intended for use with Arduino Leonardo and other ATmega32U4-based Arduinos


#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GPS.h>
#define GPSECHO  true
#if (SSD1306_LCDHEIGHT != 64)
#include <SoftwareSerial.h>
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif


 //things i added to the working code
//**************************************************
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
//**************************************************

SoftwareSerial mySerial(2, 3);
Adafruit_GPS GPS(&mySerial);




void setup()  
{


  Serial.begin(115200);
  delay(5000);
  Serial.println("Adafruit GPS library basic test!");

  
  
  //things i added to the working code
 //****************************** 
   display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.display();
  delay(2000);
  display.setTextColor(WHITE);
  display.setTextSize(2);
  display.setCursor(5,10);
  display.println("test print");
  display.setTextColor(WHITE);
  display.display();
  
 //****************************** 
  // 9600 NMEA is the default baud rate for Adafruit MTK GPS's- some use 4800
  GPS.begin(9600);

  // uncomment this line to turn on RMC (recommended minimum) and GGA (fix data) including altitude
  GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
  // uncomment this line to turn on only the "minimum recommended" data
  //GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY);
  // For parsing data, we don't suggest using anything but either RMC only or RMC+GGA since
  // the parser doesn't care about other sentences at this time

  // Set the update rate
  GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);   // 1 Hz update rate
  // For the parsing code to work nicely and have time to sort thru the data, and
  // print it out we don't suggest using anything higher than 1 Hz

  // Request updates on antenna status, comment out to keep quiet
  GPS.sendCommand(PGCMD_ANTENNA);

  delay(1000);
  // Ask for firmware version
  // mySerial.println(PMTK_Q_RELEASE);
}

uint32_t timer = millis();
void loop()                     // run over and over again
{

  // display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);

  char c = GPS.read();
  // if you want to debug, this is a good time to do it!
  if ((c) && (GPSECHO))
    Serial.write(c); 

  // if a sentence is received, we can check the checksum, parse it...
  if (GPS.newNMEAreceived()) {


    if (!GPS.parse(GPS.lastNMEA()))   // this also sets the newNMEAreceived() flag to false
      return;  // we can fail to parse a sentence in which case we should just wait for another
  }

  // if millis() or timer wraps around, we'll just reset it
  if (timer > millis())  timer = millis();

  // approximately every 2 seconds or so, print out the current stats
  if (millis() - timer > 2000) { 
    timer = millis(); // reset the timer
    delay(1000);
    Serial.print("\nTime: ");
   
   //*******************************************
      display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(5,3);
    display.println(GPS.day, DEC);
    display.display();
//*******************************************


    Serial.print((GPS.hour + 1 ) % 24 , DEC); 
    Serial.print(':');
    Serial.print(GPS.minute, DEC); 
    Serial.print(':');
    Serial.print(GPS.seconds, DEC); 
    Serial.print('.');
    Serial.println(GPS.milliseconds);
    Serial.print("Date: ");

    if((GPS.hour + 1 ) % 24 < GPS.hour){
      GPS.day = GPS.day + 1;
    }

    Serial.print(GPS.day, DEC); 
    Serial.print('/');
    Serial.print(GPS.month, DEC); 
    Serial.print("/20");
    Serial.println(GPS.year, DEC);
    Serial.print("Fix: "); 
    Serial.print((int)GPS.fix);
    Serial.print(" quality: "); 
    Serial.println((int)GPS.fixquality); 
    if (GPS.fix) {
      Serial.print("Location: ");
      Serial.print(GPS.latitude, 4); 
      Serial.print(GPS.lat);
      Serial.print(", "); 
      Serial.print(GPS.longitude, 4); 
      Serial.println(GPS.lon);

      Serial.print("Speed (Km/h: "); 
      Serial.println(GPS.speed* 1.852);
      Serial.print("Angle: "); 
      Serial.println(GPS.angle);
      Serial.print("Altitude: "); 
      Serial.println(GPS.altitude);
      Serial.print("Satellites: "); 
      Serial.println((int)GPS.satellites);
    }
  }
}

i made some modifications to it and it somewhat works, just not all the time. the are where i commented is the code i added. for now, i am just trying to display something on the screen.

alright so the program compiles and displays the adafruit logo on my screen and then displays a Zero on the display.

 display.println(GPS.day, DEC);

the funny thing is that once it displays the zero, it then loops back to the adafruit logo and repeats the process.

the same goes for year and month

could it be the char to int conversion or something like that? i faced this problem a while ago

cheers!

another update, i realise the zero is Not from the GPS.day , it is actually from here

   display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.display();
  delay(2000);
  display.setTextColor(WHITE);
  display.setTextSize(1);
  display.setCursor(30,30);
  display.println("test print");
  display.setTextColor(WHITE);
  display.display();
  delay(2000);
  display.clearDisplay();

i realise this when i change the coordinate for the sketch. im not sure if the program actually proceeds after this point..

it then loops back to the adafruit logo and repeats the process.

Is that, perhaps, a clue?

hi AWOL,

Well yes it is, but i checked that and it was not in a loop. it could be and that might cause the program to not proceed..im not sure

You've lost about 1K or RAM with just the display buffer.
How much RAM are you using in total? (I don't have the GPS library you're using)

Currently the upload size is about 20k im not sure about the RAM

yeah, it takes a while to load the code

actually maybe it would help if i posted the two sketches i want to merge

this is the display sketch

/*********************************************************************
 * This is an example for our Monochrome OLEDs based on SSD1306 drivers
 * 
 * Pick one up today in the adafruit shop!
 * ------> http://www.adafruit.com/category/63_98
 * 
 * This example is for a 128x64 size display using I2C to communicate
 * 3 pins are required to interface (2 I2C and one reset)
 * 
 * Adafruit invests time and resources providing this open source code, 
 * please support Adafruit and open-source hardware by purchasing 
 * products from Adafruit!
 * 
 * Written by Limor Fried/Ladyada  for Adafruit Industries.  
 * BSD license, check license.txt for more information
 * All text above, and the splash screen must be included in any redistribution
 *********************************************************************/

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2


#define LOGO16_GLCD_HEIGHT 16 
#define LOGO16_GLCD_WIDTH  16 


#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif

void setup()   {                
  Serial.begin(9600);

  // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3D (for the 128x64)
  // init done

  // Show image buffer on the display hardware.
  // Since the buffer is intialized with an Adafruit splashscreen
  // internally, this will display the splashscreen.
  display.display();
  delay(2000);

  // Clear the buffer.
  display.clearDisplay();



  display.setTextColor(WHITE);

  display.setTextSize(2);
  display.setCursor(5,10);
  display.println("Test");


  display.setTextColor(WHITE);
  display.display();
  delay(2000);


}


void loop() {

  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);


  display.setCursor(0,3);
  display.println("hello");
  display.display();


}

and this is the GPS sketch

// Test code for Adafruit GPS modules using MTK3329/MTK3339 driver
//
// This code shows how to listen to the GPS module in an interrupt
// which allows the program to have more 'freedom' - just parse
// when a new NMEA sentence is available! Then access data when
// desired.
//
// Tested and works great with the Adafruit Ultimate GPS module
// using MTK33x9 chipset
//    ------> http://www.adafruit.com/products/746
// Pick one up today at the Adafruit electronics shop 
// and help support open source hardware & software! -ada

//This code is intended for use with Arduino Leonardo and other ATmega32U4-based Arduinos

#include <Adafruit_GPS.h>
#include <SoftwareSerial.h>

// Connect the GPS Power pin to 5V
// Connect the GPS Ground pin to ground
// If using software serial (sketch example default):
//   Connect the GPS TX (transmit) pin to Digital 8
//   Connect the GPS RX (receive) pin to Digital 7
// If using hardware serial:
//   Connect the GPS TX (transmit) pin to Arduino RX1 (Digital 0)
//   Connect the GPS RX (receive) pin to matching TX1 (Digital 1)

// If using software serial, keep these lines enabled
// (you can change the pin numbers to match your wiring):
SoftwareSerial mySerial(2, 3);
Adafruit_GPS GPS(&mySerial);

// If using hardware serial, comment
// out the above two lines and enable these two lines instead:
//Adafruit_GPS GPS(&Serial1);
//HardwareSerial mySerial = Serial1;

// Set GPSECHO to 'false' to turn off echoing the GPS data to the Serial console
// Set to 'true' if you want to debug and listen to the raw GPS sentences
#define GPSECHO  true

void setup()  
{
    
  // connect at 115200 so we can read the GPS fast enough and echo without dropping chars
  // also spit it out
  Serial.begin(115200);
  delay(5000);
  Serial.println("Adafruit GPS library basic test!");

  // 9600 NMEA is the default baud rate for Adafruit MTK GPS's- some use 4800
  GPS.begin(9600);
  
  // uncomment this line to turn on RMC (recommended minimum) and GGA (fix data) including altitude
  GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
  // uncomment this line to turn on only the "minimum recommended" data
  //GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY);
  // For parsing data, we don't suggest using anything but either RMC only or RMC+GGA since
  // the parser doesn't care about other sentences at this time
  
  // Set the update rate
  GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);   // 1 Hz update rate
  // For the parsing code to work nicely and have time to sort thru the data, and
  // print it out we don't suggest using anything higher than 1 Hz

  // Request updates on antenna status, comment out to keep quiet
  GPS.sendCommand(PGCMD_ANTENNA);

  delay(1000);
  // Ask for firmware version
 // mySerial.println(PMTK_Q_RELEASE);
}

uint32_t timer = millis();
void loop()                     // run over and over again
{
  char c = GPS.read();
  // if you want to debug, this is a good time to do it!
  if ((c) && (GPSECHO))
    Serial.write(c); 
  
  // if a sentence is received, we can check the checksum, parse it...
  if (GPS.newNMEAreceived()) {
    // a tricky thing here is if we print the NMEA sentence, or data
    // we end up not listening and catching other sentences! 
    // so be very wary if using OUTPUT_ALLDATA and trytng to print out data
    //Serial.println(GPS.lastNMEA());   // this also sets the newNMEAreceived() flag to false
  
    if (!GPS.parse(GPS.lastNMEA()))   // this also sets the newNMEAreceived() flag to false
      return;  // we can fail to parse a sentence in which case we should just wait for another
  }

  // if millis() or timer wraps around, we'll just reset it
  if (timer > millis())  timer = millis();

  // approximately every 2 seconds or so, print out the current stats
  if (millis() - timer > 2000) { 
    timer = millis(); // reset the timer
    delay(1000);
    Serial.print("\nTime: ");
    
    
    
    Serial.print((GPS.hour + 1 ) % 24 , DEC); Serial.print(':');
    Serial.print(GPS.minute, DEC); Serial.print(':');
    Serial.print(GPS.seconds, DEC); Serial.print('.');
    Serial.println(GPS.milliseconds);
    Serial.print("Date: ");
    
    if((GPS.hour + 1 ) % 24 < GPS.hour){
    GPS.day = GPS.day + 1;
    }
    
    Serial.print(GPS.day, DEC); Serial.print('/');
    Serial.print(GPS.month, DEC); Serial.print("/20");
    Serial.println(GPS.year, DEC);
    Serial.print("Fix: "); Serial.print((int)GPS.fix);
    Serial.print(" quality: "); Serial.println((int)GPS.fixquality); 
    if (GPS.fix) {
      Serial.print("Location: ");
      Serial.print(GPS.latitude, 4); Serial.print(GPS.lat);
      Serial.print(", "); 
      Serial.print(GPS.longitude, 4); Serial.println(GPS.lon);
      
      Serial.print("Speed (Km/h: "); Serial.println(GPS.speed* 1.852);
      Serial.print("Angle: "); Serial.println(GPS.angle);
      Serial.print("Altitude: "); Serial.println(GPS.altitude);
      Serial.print("Satellites: "); Serial.println((int)GPS.satellites);
    }
  }
}

basically i tried to merge these two, trying to display the GPS data on the OLED

And what is the RAM usage of each sketch?

And what is the RAM usage of each sketch?

i dont know, am not sure how to check.. how does this make a difference?

so i made some minor adjustments to the sketch and now, it prints on the OLED..However, i cannot get the serial to print out the Serial.print();
for example,

  Serial.println("Adafruit GPS library basic test!");

this does not show up at the beginning of the serial monitor.

$,145.87,0015,,,*E,,0,0,,,M,,M,,*49
$GPGSA,A,1,,,,,,,,,,,,,,,*1E
$GPGSV,1,1,02,10,,,23,05,,,23*7F
$GPRMC,162619.082,V,,,,,0.00,145.87,0015,,,*E,,0,0,,,M,,M,,*49
$GPGSA,A,1,,,,,,,,,,,,,,,*1E
$GPGSV,1,1,02,10,,,23,05,,,23*7F
$GPRMC,162619.082,V,,,,,0.00,145.87,0015,,,*E

this is what it shows at the start.

the code is bellow

// Test code for Adafruit GPS modules using MTK3329/MTK3339 driver
//
// This code shows how to listen to the GPS module in an interrupt
// which allows the program to have more 'freedom' - just parse
// when a new NMEA sentence is available! Then access data when
// desired.
//
// Tested and works great with the Adafruit Ultimate GPS module
// using MTK33x9 chipset
//    ------> http://www.adafruit.com/products/746
// Pick one up today at the Adafruit electronics shop 
// and help support open source hardware & software! -ada

//This code is intended for use with Arduino Leonardo and other ATmega32U4-based Arduinos


#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GPS.h>
#define GPSECHO  true
#if (SSD1306_LCDHEIGHT != 64)
#include <SoftwareSerial.h>
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif


 //things i added to the working code
//**************************************************
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
//**************************************************

SoftwareSerial mySerial(2, 3);
Adafruit_GPS GPS(&mySerial);




void setup()  
{


  Serial.begin(115200);
 
  Serial.println("Adafruit GPS library basic test!");

  
  
  //things i added to the working code
 //****************************** 
   display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.display();
  
  delay(2000);
  display.clearDisplay();
  display.setTextColor(WHITE);
  display.setTextSize(1);
  display.setCursor(0,30);
  display.println("test print");
 
  display.setTextColor(WHITE);
  display.display();
  delay(2000);
  display.clearDisplay();
//  
 //****************************** 
  // 9600 NMEA is the default baud rate for Adafruit MTK GPS's- some use 4800
  GPS.begin(9600);
 
  // uncomment this line to turn on RMC (recommended minimum) and GGA (fix data) including altitude
  GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
  // uncomment this line to turn on only the "minimum recommended" data
  //GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY);
  // For parsing data, we don't suggest using anything but either RMC only or RMC+GGA since
  // the parser doesn't care about other sentences at this time

  // Set the update rate
  GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);   // 1 Hz update rate
  // For the parsing code to work nicely and have time to sort thru the data, and
  // print it out we don't suggest using anything higher than 1 Hz

  // Request updates on antenna status, comment out to keep quiet
  GPS.sendCommand(PGCMD_ANTENNA);

  delay(1000);
  // Ask for firmware version
  // mySerial.println(PMTK_Q_RELEASE);
}

uint32_t timer = millis();
void loop()                     // run over and over again
{

  Serial.print("stage1");
  // display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);

  char c = GPS.read();
  // if you want to debug, this is a good time to do it!
  if ((c) && (GPSECHO))
    Serial.write(c); 

  // if a sentence is received, we can check the checksum, parse it...
  if (GPS.newNMEAreceived()) {


    if (!GPS.parse(GPS.lastNMEA()))   // this also sets the newNMEAreceived() flag to false
      return;  // we can fail to parse a sentence in which case we should just wait for another
  }

  // if millis() or timer wraps around, we'll just reset it
  if (timer > millis())  timer = millis();

  // approximately every 2 seconds or so, print out the current stats
  if (millis() - timer > 2000) { 
    timer = millis(); // reset the timer
    delay(1000);
    Serial.print("\nTime: ");
   
   //*******************************************

    
      display.clearDisplay(); // important
      display.setTextSize(1);
      display.setTextColor(WHITE);
   
    display.setCursor(10,3);
    display.println(GPS.hour, DEC);
    display.setCursor(30,3);
    display.println(GPS.minute, DEC);
    
    display.setCursor(10,20);
    display.println(GPS.day, DEC);
    display.setCursor(30,20);
    display.println(GPS.month, DEC);
    display.setCursor(50,20);
    display.println(GPS.year, DEC);
    
    display.display();
    // display.display();
//*******************************************


    Serial.print((GPS.hour  ) % 24 , DEC); 
    Serial.print(':');
    Serial.print(GPS.minute, DEC); 
    Serial.print(':');
    Serial.print(GPS.seconds, DEC); 
    Serial.print('.');
    Serial.println(GPS.milliseconds);
    Serial.print("Date: ");

    if((GPS.hour + 1 ) % 24 < GPS.hour){
      GPS.day = GPS.day + 1;
    }
 
    Serial.print(GPS.day, DEC); 
    Serial.print('/');
    Serial.print(GPS.month, DEC); 
    Serial.print("/20");
    Serial.println(GPS.year, DEC);
    Serial.print("Fix: "); 
    Serial.print((int)GPS.fix);
    Serial.print(" quality: "); 
    Serial.println((int)GPS.fixquality); 
    if (GPS.fix) {
      Serial.print("Location: ");
      Serial.print(GPS.latitude, 4); 
      Serial.print(GPS.lat);
      Serial.print(", "); 
      Serial.print(GPS.longitude, 4); 
      Serial.println(GPS.lon);

      Serial.print("Speed (Km/h: "); 
      Serial.println(GPS.speed* 1.852);
      Serial.print("Angle: "); 
      Serial.println(GPS.angle);
      Serial.print("Altitude: "); 
      Serial.println(GPS.altitude);
      Serial.print("Satellites: "); 
      Serial.println((int)GPS.satellites);
    }
  }
}

so yeah, currently, the serial.print is not working

Grumpy_Mike:
So how have you wired these up

It looks like you are using the same pins for talking to the GPS as you are for talking to the serial monitor.

It looks like you are using the same pins for talking to the GPS as you are for talking to the serial monitor.

well yeah basically pin 2 and 3 are connected to the GPS modules TX and RX, if i ran the GPS sketch with the same setup, it works fine =)

I tried your sketch on my Uno and GPS and it wouldn't run. So I commented out all of the display code and printed out the free memory. Without the OLED your sketch is using over 1200 bytes of ram. If you're using an Uno like I am that leaves you with about 800 bytes of free memory which isn't enough for the display code. It requires well over 1000 bytes of ram.

I tried your sketch on my Uno and GPS and it wouldn't run. So I commented out all of the display code and printed out the free memory. Without the OLED your sketch is using over 1200 bytes of ram. If you're using an Uno like I am that leaves you with about 800 bytes of free memory which isn't enough for the display code. It requires well over 1000 bytes of ram.

thanks for your reply. btw how do you check the RAM usage? and secondly is there a way i can optimise the code?

You can download a library called MemoryFree from github. Or you can just include this function in your sketch and call it:

int get_free_memory()
{
 extern int __heap_start, *__brkval;
 int v;
 return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}

You will get different results depending on where you place the call so try more than one.

As for reducing RAM usage, there are many things you can do. One is to use the F() macro on all literal strings. For example, change Serial.print("This is some literal text") to Serial.print(F("This is some literal text")). The F() macro prevents the strings from being stored in RAM.

The easiest path for bigger RAM savings is to use a different OLED library. Do you want to display graphics? If so, get the u8glib library. It uses a partial screen buffer and loops to draw the screen in pieces. It trades off some speed for RAM savings. Do you just want to display text? I have a text-only library that uses less than 100 bytes of RAM, although with an I2C interface the Wire library requires an additional 200 bytes or so.

hi, thanks for your reply, i will do the printf now and see how it goes, and yeah im just printing text. what library are you using? cheers mate

LOl what kind of sorcery is this? the Serial.print(F("")); worked! but yeah awesome explanation!! ill post my when i finish the sketch
cheers!!!!