/*********
*********/
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// Define the resistance value (8 ohms)
const float resistorResistance = 8.0;
void setup() {
Serial.begin(115200);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
delay(500);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 00);
// Display static text
display.println("NIMH Batt discharger");
display.display();
}
void loop() {
// Data regarding battery B1:
// read the input on analog pin 0:
int sensorValue_B1 = analogRead(A0);
// print out the value you read:
Serial.println(sensorValue_B1); // Print ADC value from A0 to serial monitor
float voltage_B1 = sensorValue_B1 * (5.0 / 1023.0); // Convert ADC value from A0 into measured voltage
// Calculate current (I = V_R / R)
float current_mA_B1 = voltage_B1 / resistorResistance * 1000;
// Use sprintf() command to insert zero's in front of calculated/measured B1 value so that there always is 4 characters
int calculatedNumber_B1 = current_mA_B1; // Make an int containing the calculated B1 current
char formattedNumber_B1[4]; // Buffer to store the formatted number always with 4 characters
sprintf(formattedNumber_B1, "%04d", calculatedNumber_B1);
display.setCursor(0, 10);
display.setTextColor(WHITE, BLACK);
display.println(" "); // Erase line with black spaces, so it's ready to write a new line in white
display.setTextColor(WHITE);
display.setCursor(0, 10);
display.print("B1 ");
display.print(voltage_B1, 3); // Write converted A0 ADC value to OLED with 3 decimals
display.print("V ");
display.print(formattedNumber_B1); // Write formattedNumber_B1 to OLED always with 4 characters
display.println("mA");
display.display();
delay(250); // delay in between reads for stability
}