Issue between SD.h Libary and 0.96" OLED display

Hello guys
I am programming a Powermeter, i use the currentsensor "ina219". I am trying to write informtions on the 0.96" oled display and write informations on a mircro sd card.
I use the aruino Nano

the programm works without the display perfectly. but using the oled display AND the SD card does not work because of the liabarys conflict between the SD.h libary and the Adafruit_SSD1306.h libary.

Can someone help me with this conflict / do someone know a solution for this problem

I know that there is also the SdFat.h libary, but i don´t get my sketch running with this libary.

This is the program without the display, this is running perfectly so far

#include <SD.h>
#include <Wire.h>
#include <Adafruit_INA219.h>
#include <SPI.h>

Adafruit_INA219 ina219;
// file name to use for writing
const char filename[] = "Time.txt";

// File object to represent file
File TimeFile;

// string to buffer output
String buffer;

unsigned long lastMillis = 0;
float current_mA = 0;
float current = 0;
float voltage = 0;
float energy = 0;

void setup() {
  ina219.begin();
  Serial.begin(9600);
  while (!Serial);

  // reserve 1kB for String used as a buffer
  buffer.reserve(1024);

  // set LED pin to output, used to blink when writing
  pinMode(LED_BUILTIN, OUTPUT);

  // init the SD card
  if (!SD.begin()) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    while (1);
  }

  // If you want to start from an empty file,
  // uncomment the next line:
   SD.remove(filename);

  // try to open the file for writing
  TimeFile = SD.open(filename, FILE_WRITE);
  if (!TimeFile) {
    Serial.print("error opening ");
    Serial.println(filename);
    while (1);
  }

  // add some new lines to start
  TimeFile.println();
  TimeFile.println("start");
}

void loop() {
  // check if it's been over 10 ms since the last line added
  unsigned long now = millis();
  if ((now - lastMillis) >= 10) {
    current_mA = ina219.getCurrent_mA(); //Current measurementa
  current = current_mA / 1.25;
  voltage = (current/1000)* 6.1; // calculate the voltage with current * resistor, here the total resistance is 6.1ohm
  energy = energy + (voltage* current) /3600; // calculating the energy
  
    // add a new line to the buffer
    buffer += now;
    buffer += voltage;
    buffer += current;
    buffer += "\r\n";

    lastMillis = now;
  }

  // check if the SD card is available to write data without blocking
  // and if the buffered data is enough for the full chunk size
  unsigned int chunkSize = TimeFile.availableForWrite();
  if (chunkSize && buffer.length() >= chunkSize) {
    // write to file and blink LED
    digitalWrite(LED_BUILTIN, HIGH);
    TimeFile.write(buffer.c_str(), chunkSize);
    digitalWrite(LED_BUILTIN, LOW);

    // remove written data from buffer
    buffer.remove(0, chunkSize);
  }
}
  

Now i am trying to add the 0.96" oled display to this code but i don't know how i can solve the libary conflict. do someone has a solution for this problem

Here the schematics
Solartracker_Powermeter.pdf (46,5 KB)

What specifically is the conflict?

The major problem I see is this

Global variables use 1167 bytes (56%) of dynamic memory, leaving 881 bytes for local variables. Maximum is 2048 bytes.

For a 128x64 OLED display, the Adafruit_SSD1306 library allocates 1024 bytes of RAM at run-time, there is not enough RAM on top of any conflict with the libraries.

There are other OLED libraries that require much less RAM.

The String you are using as a buffer for the SD card writes is unnecessary, the SD library will accumulate whatever is printed/written in its own buffer and only write to the SD card when needed, or when the code specifically flushes the buffer or closes the file. I see that you are trying to avoid waiting for the SD card since you need 10mS intervals for the data. Personally I would use a char array instead of String, but either way you will need to plan on at least another 512 bytes of RAM usage for the double buffering, leaving basically nothing for a display buffer.

The conflict is that either the display is working or the sd card writing is possible.

I tried already the ssd1306Ascii libary, that didn't work though, but i had the feeling that this libary must be the libary to use.

I tried the sketch without the nonblockwriting part (you can find the basic sketch in the example sketches : SD - > nonblockwriting), but it will not work constantly.

In the sketch above i made a mistake, i normaly use 100ms for the intervall, not 10ms

Do you have an idea how i can compress the data for the ram, so that display and SD will work?

Thanks

There are a couple of places you can use the F() macro for printing text literals, but other than that there is not a lot of ways to save memory in your sketch. The Adafruit_INA219 library takes a considerable amount of RAM, and the SD library needs the 512 byte buffer.

What are you intending to display on the OLED? There are some OLED libraries that require very little RAM, but are generally limited to text-only. The U8g2 library can use either the full 1024 byte display buffer, or a much smaller paged buffer, and does allow full graphics and text.

The 100mS interval helps considerably because the OLED can be quite slow to update and might be prohibitive with a 10mS interval. It might also be necessary to stagger the SD card write and the OLED update so that they do not occur within the same 100mS interval.

I want to display the voltage, current, power and energy. Only text. On the sd card i want to write the time, voltage and current.

Do you think my program would run if i use a arduino mega? (8kb ram memory)