SD Weather Station with OLED display not working properly

My code involving an SD weather station with an OLED has not worked like I’ve wanted it to. The OLED will not refresh. The SD code without the OLED works fine. Any help would be greatly appreciated. Here is my SD code that works fine:

 #include "Wire.h"
#define P0 1013.25
#include "DHT.h"
#include <SPI.h>
#include <SD.h>
#include <stdlib.h>
#include "Sodaq_DS3231.h"
#include <BME280_MOD-1022.h>

void printFormattedFloat(float x, uint8_t precision) {
char buffer[10];

  dtostrf(x, 7, precision, buffer);
  Serial.print(buffer);

}


// print out the measurements

void printCompensatedMeasurements(void) {

float temp, humidity,  pressure, pressureMoreAccurate;
double tempMostAccurate, humidityMostAccurate, pressureMostAccurate;
char buffer[80];

  temp      = BME280.getTemperature();
  humidity  = BME280.getHumidity();
  pressure  = BME280.getPressure();
 
  pressureMoreAccurate = BME280.getPressureMoreAccurate();  // t_fine already calculated from getTemperaure() above
 
  tempMostAccurate     = BME280.getTemperatureMostAccurate();
  humidityMostAccurate = BME280.getHumidityMostAccurate();
  pressureMostAccurate = BME280.getPressureMostAccurate();
  Serial.println("                Good  Better    Best");
  Serial.print("Temperature  ");
  printFormattedFloat(temp, 2);
  Serial.print("         ");
  printFormattedFloat(tempMostAccurate, 2);
  Serial.println();
 
  Serial.print("Humidity     ");
  printFormattedFloat(humidity, 2);
  Serial.print("         ");
  printFormattedFloat(humidityMostAccurate, 2);
  Serial.println();

  Serial.print("Pressure     ");
  printFormattedFloat(pressure, 2);
  Serial.print(" ");
  printFormattedFloat(pressureMoreAccurate, 2);
  Serial.print(" ");
  printFormattedFloat(pressureMostAccurate, 2);
  Serial.println();
}
#define DHTPIN 9     // what digital pin we're connected to
const int chipSelect = 10;

// Uncomment whatever type you're using!
#define DHTTYPE DHT22   // DHT 22
DHT dht(DHTPIN, DHTTYPE);

void setup()
{
  Serial.begin(9600);
  Wire.begin();
  rtc.begin();
  Serial.print("Initializing SD card...");

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
  Serial.println("BMP DHT22 Measurement");
  dht.begin();
}
void loop()
{
  delay(300000);

  uint8_t chipID;
 
  Serial.println("Welcome to the BME280 MOD-1022 weather multi-sensor test sketch!");
  Serial.println("Embedded Adventures (www.embeddedadventures.com)");
  chipID = BME280.readChipId();
 
  // find the chip ID out just for fun
  Serial.print("ChipID = 0x");
  Serial.print(chipID, HEX);
 
 
  // need to read the NVM compensation parameters
  BME280.readCompensationParams();
 
  // Need to turn on 1x oversampling, default is os_skipped, which means it doesn't measure anything
  BME280.writeOversamplingPressure(os1x);  // 1x over sampling (ie, just one sample)
  BME280.writeOversamplingTemperature(os1x);
  BME280.writeOversamplingHumidity(os1x);
 
  // example of a forced sample.  After taking the measurement the chip goes back to sleep
  BME280.writeMode(smForced);
  while (BME280.isMeasuring()) {
    Serial.println("Measuring...");
    delay(50);
  }
  Serial.println("Done!");
 
  DateTime now = rtc.now();

  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);
 
  if (isnan(h) || isnan(t) || isnan(f))
  {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // Compute heat index in Fahrenheit (the default)
  float hif = dht.computeHeatIndex(f, h);
  // Compute heat index in Celsius (isFahreheit = false)
  float hic = dht.computeHeatIndex(t, h, false);

      BME280.readMeasurements();
      Serial.print(BME280.getTemperature());  // must get temp first
      Serial.print(",");
      Serial.print(BME280.getHumidity());
      Serial.print(",");
      Serial.print(BME280.getPressure());
      Serial.print(",");
      Serial.print(BME280.getPressureMoreAccurate());  // use int64 calculcations
      Serial.print(",");
      Serial.print(BME280.getTemperatureMostAccurate() * 9 / 5 + 32);
      Serial.print(",");
      Serial.print(BME280.getHumidityMostAccurate()); // use double calculations
      Serial.print(",");
      Serial.print(BME280.getPressureMoreAccurate() * exp (556 / (29.3 * (BME280.getTemperatureMostAccurate() + 273.15))) * 0.0295301); // use double calculations
      Serial.print(",");
      Serial.print(now.month(), DEC);
      Serial.print('/');
      Serial.print(now.date(), DEC);
      Serial.print('/');
      Serial.print(now.year(), DEC);
      Serial.print(",");
      Serial.print(now.hour(), DEC);
      Serial.print(':');
      Serial.print(now.minute(), DEC);
      Serial.print(':');
      Serial.print(now.second(), DEC);
      Serial.print(",");
      Serial.print("Temperature: ");
      Serial.print(f);
      Serial.print(" *F, ");
      Serial.print("Humidity: ");
      Serial.print(BME280.getHumidityMostAccurate());
      Serial.print(" %, ");
      Serial.print("Temp C: ");
      Serial.print(t);
      Serial.print(" Dew Point:  ");
      Serial.println(243.04*(log(BME280.getHumidityMostAccurate()/100)+((17.625*t)/(243.04+t)))/(17.625-log(BME280.getHumidityMostAccurate()/100)-((17.625*t)/(243.04+t)))*1.8+32);

 
      // open the file. note that only one file can be open at a time,
      // so you have to close this one before opening another.
      File dataFile = SD.open("dhtbmptc.txt", FILE_WRITE);

      // if the file is available, write to it:
      if (dataFile)
      {
        dataFile.print(now.month(), DEC);
        dataFile.print('/');
        dataFile.print(now.date(), DEC);
        dataFile.print('/');
        dataFile.print(now.year(), DEC);
        dataFile.print(",");
        dataFile.print(now.hour(), DEC);
        dataFile.print(':');
        dataFile.print(now.minute(), DEC);
        dataFile.print(':');
        dataFile.print(now.second(), DEC);
        dataFile.print(",");
        dataFile.print(f);
        dataFile.print(",");
        dataFile.print(BME280.getHumidityMostAccurate());
        dataFile.print(",");
        dataFile.print(BME280.getPressureMoreAccurate() * exp (556 / (29.3 * (t + 273.15))) * 0.0295301);
        dataFile.print(",");
        dataFile.println(243.04*(log(BME280.getHumidityMostAccurate()/100)+((17.625*t)/(243.04+t)))/(17.625-log(BME280.getHumidityMostAccurate()/100)-((17.625*t)/(243.04+t)))*1.8+32);
     
     
      dataFile.close();

      }   else
    {
      Serial.println("Error.");
    }
}

Using the U8glib.h library, how can I add the code to my previous code needed to display this onto my OLED?

u8g.setFont(u8g_font_6x12);
  u8g.setPrintPos(4, 10);
  u8g.print("Temp: ");
  u8g.print(f);
  u8g.print("\xB0");
  u8g.setPrintPos(4, 20);
  u8g.print("Hum: ");
  u8g.print(BME280.getHumidityMostAccurate());
  u8g.print("%");
  u8g.setPrintPos(4, 30);
  u8g.print("Dew Point: ");
  u8g.print(243.04*(log(BME280.getHumidityMostAccurate()/100)+((17.625*t)/(243.04+t)))/(17.625-log(BME280.getHumidityMostAccurate()/100)-((17.625*t)/(243.04+t)))*1.8+32);
  u8g.print("\xB0");
  u8g.setPrintPos(4, 40);
  u8g.print("Pressure: ");
  u8g.print(BME280.getPressureMoreAccurate() * exp (556 / (29.3 * (t + 273.15))) * 0.0295301);
  u8g.print("in");
}

Additionally, how can I display the time and date on my OLED as shown with the SD part of the code?

 dataFile.print(now.month(), DEC);
        dataFile.print('/');
        dataFile.print(now.date(), DEC);
        dataFile.print('/');
        dataFile.print(now.year(), DEC);
        dataFile.print(",");
        dataFile.print(now.hour(), DEC);
        dataFile.print(':');
        dataFile.print(now.minute(), DEC);
        dataFile.print(':');
        dataFile.print(now.second(), DEC);

Using the U8glib.h library, how can I add the code to my previous code needed to display this onto my OLED?

You can't. The printFormattedFloat() function is hardcoded to print to the serial port.

You could redesign (and rename) the function to do the formatting and return a string (or write to a global string) and then print the global or returned string to the OLED.

Thank you very much for responding. I'm still not that familiar with how to do a lot of this. Could you give me an example of what exactly I need to do to display temperature on the screen while still having the data write to an SD card and then I try figuring out the rest? Thank you.

Also, I’ve tried the Adafruit library for a BME 280 sensor, and it’s a common theme I’ve had with Adafruit libraries. I get an error compiling message. I add these libraries just like other ones I download, using the add zip function in Arduino. When that hasn’t worked I’ve done it manually and get the same problems. What could be the problem there? Here is the example code and error messages:

/***************************************************************************
  This is a library for the BME280 humidity, temperature & pressure sensor

  Designed specifically to work with the Adafruit BME280 Breakout
  ----> http://www.adafruit.com/products/2650

  These sensors use I2C or SPI to communicate, 2 or 4 pins are required
  to interface.

  Adafruit invests time and resources providing this open source code,
  please support Adafruit andopen-source hardware by purchasing products
  from Adafruit!

  Written by Limor Fried & Kevin Townsend for Adafruit Industries.
  BSD license, all text above must be included in any redistribution
 ***************************************************************************/

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>

#define BME_SCK 13
#define BME_MISO 12
#define BME_MOSI 11
#define BME_CS 10

#define SEALEVELPRESSURE_HPA (1013.25)

Adafruit_BME280 bme; // I2C
//Adafruit_BME280 bme(BME_CS); // hardware SPI
//Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO,  BME_SCK);

void setup() {
  Serial.begin(9600);
  Serial.println(F("BME280 test"));

  if (!bme.begin()) {
    Serial.println("Could not find a valid BME280 sensor, check wiring!");
    while (1);
  }
}

void loop() {
    Serial.print("Temperature = ");
    Serial.print(bme.readTemperature());
    Serial.println(" *C");

    Serial.print("Pressure = ");

    Serial.print(bme.readPressure() / 100.0F);
    Serial.println(" hPa");

    Serial.print("Approx. Altitude = ");
    Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
    Serial.println(" m");

    Serial.print("Humidity = ");
    Serial.print(bme.readHumidity());
    Serial.println(" %");

    Serial.println();
    delay(2000);
}

Arduino: 1.6.7 (Windows 10), Board: “Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)”

C:\Program Files (x86)\Arduino\arduino-builder -dump-prefs -logger=machine -hardware “C:\Program Files (x86)\Arduino\hardware” -hardware “C:\Users\Daniel\AppData\Local\Arduino15\packages” -tools “C:\Program Files (x86)\Arduino\tools-builder” -tools “C:\Program Files (x86)\Arduino\hardware\tools\avr” -tools “C:\Users\Daniel\AppData\Local\Arduino15\packages” -built-in-libraries “C:\Program Files (x86)\Arduino\libraries” -libraries “C:\Users\Daniel\Documents\Arduino\libraries” -fqbn=arduino:avr:mega:cpu=atmega2560 -ide-version=10607 -build-path “C:\Users\Daniel\AppData\Local\Temp\buildfe94265d485a864cd78b14c764be7a87.tmp” -warnings=none -prefs=build.warn_data_percentage=75 -verbose “C:\Users\Daniel\Documents\Arduino\libraries\Adafruit_BME280_Library-master\examples\bme280test\bme280test.ino”
C:\Program Files (x86)\Arduino\arduino-builder -compile -logger=machine -hardware “C:\Program Files (x86)\Arduino\hardware” -hardware “C:\Users\Daniel\AppData\Local\Arduino15\packages” -tools “C:\Program Files (x86)\Arduino\tools-builder” -tools “C:\Program Files (x86)\Arduino\hardware\tools\avr” -tools “C:\Users\Daniel\AppData\Local\Arduino15\packages” -built-in-libraries “C:\Program Files (x86)\Arduino\libraries” -libraries “C:\Users\Daniel\Documents\Arduino\libraries” -fqbn=arduino:avr:mega:cpu=atmega2560 -ide-version=10607 -build-path “C:\Users\Daniel\AppData\Local\Temp\buildfe94265d485a864cd78b14c764be7a87.tmp” -warnings=none -prefs=build.warn_data_percentage=75 -verbose “C:\Users\Daniel\Documents\Arduino\libraries\Adafruit_BME280_Library-master\examples\bme280test\bme280test.ino”
WARNING: Spurious .github folder in ‘Adafruit BME280 Library’ library
“C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avr-g++” -c -g -Os -w -std=gnu++11 -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -w -x c++ -E -CC -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR “-IC:\Users\Daniel\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.11\cores\arduino” “-IC:\Users\Daniel\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.11\variants\mega” “C:\Users\Daniel\AppData\Local\Temp\buildfe94265d485a864cd78b14c764be7a87.tmp\sketch\bme280test.ino.cpp” -o “nul”
“C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avr-g++” -c -g -Os -w -std=gnu++11 -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -w -x c++ -E -CC -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR “-IC:\Users\Daniel\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.11\cores\arduino” “-IC:\Users\Daniel\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.11\variants\mega” “-IC:\Users\Daniel\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.11\libraries\Wire\src” “C:\Users\Daniel\AppData\Local\Temp\buildfe94265d485a864cd78b14c764be7a87.tmp\sketch\bme280test.ino.cpp” -o “nul”
“C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avr-g++” -c -g -Os -w -std=gnu++11 -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -w -x c++ -E -CC -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR “-IC:\Users\Daniel\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.11\cores\arduino” “-IC:\Users\Daniel\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.11\variants\mega” “-IC:\Users\Daniel\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.11\libraries\Wire\src” “-IC:\Users\Daniel\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.11\libraries\SPI\src” “C:\Users\Daniel\AppData\Local\Temp\buildfe94265d485a864cd78b14c764be7a87.tmp\sketch\bme280test.ino.cpp” -o “nul”
“C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avr-g++” -c -g -Os -w -std=gnu++11 -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -w -x c++ -E -CC -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR “-IC:\Users\Daniel\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.11\cores\arduino” “-IC:\Users\Daniel\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.11\variants\mega” “-IC:\Users\Daniel\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.11\libraries\Wire\src” “-IC:\Users\Daniel\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.11\libraries\SPI\src” “C:\Users\Daniel\AppData\Local\Temp\buildfe94265d485a864cd78b14c764be7a87.tmp\sketch\bme280test.ino.cpp” -o “C:\Users\Daniel\AppData\Local\Temp\buildfe94265d485a864cd78b14c764be7a87.tmp\preproc\ctags_target_for_gcc_minus_e.cpp”
C:\Users\Daniel\Documents\Arduino\libraries\Adafruit_BME280_Library-master\examples\bme280test\bme280test.ino:20:29: fatal error: Adafruit_Sensor.h: No such file or directory

#include <Adafruit_Sensor.h>

^

compilation terminated.

Using library Wire at version 1.0 in folder: C:\Users\Daniel\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.11\libraries\Wire
Using library SPI at version 1.0 in folder: C:\Users\Daniel\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.11\libraries\SPI
exit status 1
Error compiling.

Could you give me an example of what exactly I need to do to display temperature on the screen while still having the data write to an SD card and then I try figuring out the rest?

No. You seem determined to write functions that get and print data without returning anything.

You MUST write functions that GET the data from the sensor WITHOUT PRINTING IT. Make the function return a value. Think about how your functions differ from analogRead(), for instance. With you in charge, the function would have been called printAnalogRead() and would not have returned a value. It's a good thing you are NOT in charge.

it's a common theme I've had with Adafruit libraries.

Adafruit does not appear to hire professional programmers.

On the other hand,

C:\Users\Daniel\Documents\Arduino\libraries\Adafruit_BME280_Library-master\examples\bme280test\bme280test.ino:20:29: fatal error: Adafruit_Sensor.h: No such file or directory

CLEARLY says that the problem exists between the keyboard and the chair.