Hi! I made a constant current load with an Arduino Nano, SD card, MCP4725 DAC, INA219 current meter and an 128x64 SPI oled screen. After some time sampling the power from the INA219, the oled shows a lot of artifacts. I've used different oled libraries and all of them showed artifacts after a while, but writing to the SD card does behave like normal. I found a function online that should show you how much SRAM is available, which showed 415 bytes of 2kb is available, but artifacts still happen.
Without the SD card library, the oled behaves as expected.
What could be the problem?
#include <Arduino.h>
#include <U8g2lib.h>
#include <Wire.h>
#include <Adafruit_INA219.h>
#include <Adafruit_MCP4725.h>
#include <SdFat.h>
#include <EEPROM.h>
#define SD_CS_PIN 7
#include <SPI.h>
#define inputCLK 3
#define inputDT 2
word counter = 0;
byte currentStateCLK;
byte previousStateCLK;
unsigned long Tijd = 0; //maximaal 604.800 seconden
unsigned long start;
unsigned long starten;
char fileName[] = "Gesampled000.txt";
float vermogen;
SdFat SD;
File myFile;
Adafruit_INA219 ina219;
Adafruit_MCP4725 dac;
#define DAC_RESOLUTION (9)
//U8G2_SSD1306_128X64_NONAME_1_4W_HW_SPI(/* cs=*/ 0, /* dc=*/ 10, /* reset=*/ 9); // Arduboy (DevKit)
//U8G2_SSD1306_128X64_NONAME_1_4W_HW_SPI u8g2(U8G2_R0, 10, 9)
//U8G2_UC1701_DOGS102_1_4W_SW_SPI u8g2(U8G2_R0, /* clock=*/ 13, /* data=*/ 11, /* cs=*/ 10, /* dc=*/ 9, /* reset=*/ 8);
U8X8_SSD1306_128X64_NONAME_4W_HW_SPI u8x8(10, 9, 6);
//cs = 10//dc = 9
void RotaryEncoder(){
currentStateCLK = digitalRead(inputCLK);
if (currentStateCLK != previousStateCLK){
if (digitalRead(inputDT) != currentStateCLK) {
counter -= 1;
} else {
counter += 1;
}
previousStateCLK = currentStateCLK;
}
Serial.println(counter);
}
ISR(TIMER1_OVF_vect){
Tijd++;
myFile = SD.open(fileName, FILE_WRITE);
myFile.println("");
myFile.print("Seconden: ");
myFile.println(Tijd);
myFile.print("vermogen: ");
myFile.println(vermogen, 3);
myFile.close();
vermogen = 0;
TCNT1 = 40000;
}
void vermogenBerekenen(){
unsigned int waardeTijd = micros() - starten;
starten = micros();
float spanning = (ina219.getShuntVoltage_mV()/1000) + ina219.getBusVoltage_V() - 0.008; //shunt moet naar V, nu in mVwe
float current = ina219.getCurrent_mA()*0.002; //Shunt = 50 mOhm
if(current < 0.003){current = 0.00001;}
if(spanning < 0){spanning = 0;}
vermogen += current * spanning * (waardeTijd*pow(10,-6));
//mWh = (Watt * waardeTijd)*pow(10,-6);
//mWhDisplay += mWh/3.6;
}
void setup(void){
Wire.begin();
dac.begin(0x62);
pinMode (inputCLK,INPUT);
pinMode (inputDT,INPUT);
previousStateCLK = digitalRead(inputCLK);
attachInterrupt(digitalPinToInterrupt(3), RotaryEncoder, CHANGE);
u8x8.setFont(u8x8_font_chroma48medium8_r);
u8x8.begin();
u8x8.setPowerSave(0);
if (! ina219.begin()) {
u8x8.drawString(0,10,"INA219 FAIL");
while (1) {}
}
if(!dac.begin(0x60)){
u8x8.drawString(0,10,"MCP FAIL");
while(1){}
}
if (!SD.begin(SD_CS_PIN)) {
while(1){
u8x8.drawString(0,10,"Sd FAIL");
}
}
else{
byte n = EEPROM.read(0) + 1;
EEPROM.write(0, n+1);
sprintf(fileName, "Sample0%02d.txt",n);
}
u8x8.drawString(0,10,"OK!");
delay(3000);
u8x8.clearDisplay();
start = millis();
//Time interrupt elke 1 sec.
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 40000;
TCCR1B |= (1<<CS12); //prescaler
TIMSK1 |= (1 << TOIE1); //Enable timer interrupt, (TIMSK1 &= ~(1<<TOIE1) = disable timer)
}
int availableMemory() {
// Use 1024 with ATmega168
int size = 2048;
byte *buf;
while ((buf = (byte *) malloc(--size)) == NULL);
free(buf);
return size;
}
void loop(){
vermogenBerekenen();
//u8x8.setCursor(80, 0);
// u8x8.println(counter);
dac.setVoltage(counter, false);
if(millis() - start > 200){
u8x8.clearDisplay();
u8x8.setCursor(0, 20);
int SRAM = availableMemory();
u8x8.println(SRAM);
u8x8.setCursor(0, 0);
u8x8.println(Tijd);
start = millis();
}
}

