This is the sketch I'm running, with 3 Red Leds each one with his own resistor from pin 9 to GND. The values displayed on serial monitor and OLED are very unstable, basically unusable, need some help, please.
#include <U8g2_for_Adafruit_GFX.h>
#include <Wire.h>
#include <Adafruit_INA219.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
Adafruit_INA219 ina219;
int ledPin = 9; // LED connected to digital pin 9
int analogPin = A0; // potentiometer connected to analog pin A0
int val = 0; // variable to store the read value
U8G2_FOR_ADAFRUIT_GFX u8g2_for_adafruit_gfx;
void setup() {
// put your setup code here, to run once:
pinMode(ledPin, OUTPUT); // sets the pin as output
Serial.begin(9600);
ina219.begin();
ina219.setCalibration_16V_400mA();
u8g2_for_adafruit_gfx.begin(display); // connect u8g2 procedures to Adafruit GFX
display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
Serial.println("Measuring voltage, current, and power with INA219 ...");
}
void loop() {
// put your main code here, to run repeatedly:
val = analogRead(analogPin); // read the input pin
analogWrite(ledPin, val / 4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
Serial.println(val);
Serial.println(val/4);
float shuntvoltage = 0;
float busvoltage = 0;
float current_mA = 0;
float loadvoltage = 0;
float power_mW = 0;
shuntvoltage = ina219.getShuntVoltage_mV();
busvoltage = ina219.getBusVoltage_V();
current_mA = ina219.getCurrent_mA();
power_mW = ina219.getPower_mW();
loadvoltage = busvoltage + (shuntvoltage / 1000);
Serial.print("Bus Voltage: "); Serial.print(busvoltage); Serial.println(" V");
Serial.print("Shunt Voltage: "); Serial.print(shuntvoltage); Serial.println(" mV");
Serial.print("Load Voltage: "); Serial.print(loadvoltage); Serial.println(" V");
Serial.print("Current: "); Serial.print(current_mA); Serial.println(" mA");
Serial.print("Power: "); Serial.print(power_mW); Serial.println(" mW");
Serial.println("");
//delay(1000);
display.clearDisplay();
//display.setTextSize(2);
u8g2_for_adafruit_gfx.setFontMode(1); // use u8g2 transparent mode (this is default)
u8g2_for_adafruit_gfx.setFontDirection(0); // left to right (this is default)
u8g2_for_adafruit_gfx.setForegroundColor(BLACK); // apply Adafruit GFX color
u8g2_for_adafruit_gfx.setFont(u8g2_font_6x13_tf); // select u8g2 font from here: https://github.com/olikraus/u8g2/wiki/fntlistall
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.print("Load_voltage "); display.print(loadvoltage); display.print(" V");
//display.setTextSize(1);
display.setCursor(0, 25);
//display.print((vin));
display.print("Current "); display.print(current_mA); display.print(" mA");
display.display();
//delay(3000);
}