Suddenly a problem with my code, or the something else...

Hi there,

I’m trying to display the current and voltage of my motorhomes second battery by using the INA219 from Adafruit and theyre OLED. Both are using I2C but with different adresses.
As I was experimenting my Arduino suddenly stopped working the next day. The code was runnig fine since then.
The display keeps black as it’s new favorite color.
So I tried to run the sample code but nothing worked. I tried a second board, reconected everything, bought a new display. And nothing worked at all. So I took the Laptop from my girlfriend and suddenly the sample code worked again. But not my code. But since then the sample code is working fine on my laptop again. I’m confused. I also putted in some Serial.print(“Are you there?”); in the setup and in the loop, but no answer. Does someone knows whats going on there?

Here’s some code, mayby I made a mistake. I’m also not a pro at all so you can also send some improvements :slight_smile:

Thanks and greetings

#include <INA219.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <SPI.h>
#include <Wire.h>

INA219 ina219;

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET 4
#define R_SHUNT 0.001
#define V_SHUNT_MAX 0.05
#define I_MAX_EXPECTED 50
#define V_BUS_MAX 16

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

const float full = 15.5; //Batteriespannung wenn geladen
const float normal = 12.6; //Batteriespannung wenn normal
const float empty = 11.9; //Batteriespannung wenn leer
const float maxzeile = 50;

float current_A = 0;
float shuntvoltage = 0;
float busvoltage = 0;
float difference = 0;
float anzahlzeile;

int anzahlzeileint;
int x;

void setup(void)
{
uint32_t currentFrequency;

ina219.begin();
ina219.configure(INA219::RANGE_16V, INA219::GAIN_1_40MV, INA219::ADC_16SAMP, INA219::ADC_16SAMP, INA219::CONT_SH_BUS);
ina219.calibrate(R_SHUNT, V_SHUNT_MAX, V_BUS_MAX, I_MAX_EXPECTED);

Serial.begin(9600);

display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.println(F(“Los geht’s!”));
display.display();
Serial.print(“Los gehts!”);
delay(1000);
}

void loop(void)
{
busvoltage = ina219.busVoltage();
current_A = ina219.shuntCurrent();
anzahlzeile = 0;
anzahlzeileint = 0;
x = 0;

difference = full - empty;
anzahlzeile = (busvoltage * difference) - 11;

int anzahlzeileint=(int)anzahlzeile;

x = 57 - anzahlzeileint;

for (int i=57; i>x; i–) {
display.drawLine(7,i,42,i,SSD1306_WHITE);
}

Serial.println(busvoltage);
display.drawRect(5,10,40,50,SSD1306_WHITE); display.fillRect(10,5,5,5,SSD1306_WHITE); display.fillRect(35,5,5,5,SSD1306_WHITE);
display.setTextSize(2); display.setTextColor(SSD1306_WHITE); display.setCursor(50,5); display.print(busvoltage, 1); display.println(“V”);
display.setTextSize(2); display.setTextColor(SSD1306_WHITE); display.setCursor(50,25); display.print(current_A, 1); display.println(“A”);
display.clearDisplay();
display.display();

/ina219.recalibrate();
ina219.reconfig();
/

delay(50);
}

Ok, never mind.

I don't know how but I experimented a little bit and now it's working. Please don't ask why :smiley: