SPI.h & Wire.h required?

/*
    19/08/21
    Much smoother with banner out of setup
    SPI & Wire includes seem not to matter included in libs?
*/

#include <Adafruit_GPS.h>
//#include <SPI.h>             // No difference in or out
//#include <Wire.h>            // I2C needs it? no difference in or out
//#include <Adafruit_GFX.h>    // Not be needed for basic txt
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

#define OLED_CLK   8
#define OLED_MOSI  9
#define OLED_RESET 10
#define OLED_DC    11
#define OLED_CS    12
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
Adafruit_GPS GPS(&Wire);

// Dummy vars for loop
float v = 1708;    // Version
float t = 15.27;   // Temp
float h = 87.81;   // Humidity
int   tp = 0;      // Total Posts

void setup() {
  GPS.begin(0x10);
  Serial.begin(115200);
  Serial.println("GPS data disp on SSD1306");
  display.begin(SSD1306_SWITCHCAPVCC);
  display.display();
  delay(1000);
  display.clearDisplay();
  display.display();
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
}

void loop() {
  char g_data = GPS.read();
  String gpsString[5] = String('g_data');
  Serial.print(F(g_data));
  unsigned int SLen = gpsString[5].length();
  display.setCursor(0, 34), display.print(F("GPS : ")), display.print(g_data);
  display.setCursor(0, 46), display.print(F("SLen: ")), display.print(SLen);
  display.display();
  //  delay(1000);
  display.clearDisplay();
  display.display();
}

Code works without SPI.h and Wire.h included, is this because the GPS and SSD device libs have the required components already ?

Yup.

Line 62 and 63.

1 Like

Thanks very much.. OK crack open the lib on git hub and read,,, Got it... Newbee

You'll sometimes find these unnecessary #include directives because many years ago (<1.6.5) the Arduino IDE's library discovery system only discovered the library dependencies of the sketch. It did not do discovery for the library dependencies of libraries. So you had to put #include directives for every library used by the libraries used by the sketch.

Fortunately, the library discovery system has been greatly improved since those days and not many people are still using the old IDE versions from before those improvement. So there's probably no need for these unnecessary #include directives, but they still hang around. They don't actually break anything, but they also make the sketches appear more complex than necessary.

If you find a sketch with a LOT of #include lines, comment them all out. The compiler will tell you what names are undefined and that will usually be enough to know that a particular #include is needed. Un-comment the necessary #include lines until the sketch compiles. The lines that remain commented out can be deleted. :slight_smile:

Thanks I wondered if it were legacy related. Good to know they do not break anything or impact memory space..

Cheers, good tip, as you say the compiler provide some decent clues....

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.