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)