#include <Adafruit_INA219.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include <Wire.h>
void DisplayCheck(); //check presents of display
void InaCheck(); //check presents of INA
void bCase(); //display case (only display purpose)
void bLvl(); // display commas
void bPer(); //display % value
void bVoltage(); //display Voltage value
void AmpCurrent(); //display Current value
void AmpWatts(); //display Watts value
void Charging(); //display Charging animation (if D12 is active)
float rawval; //raw output
int per; // % value
float valV; // Voltage value
float valC; // Current value
float valW; // Watt value
const int perVallay[8] = {12.5, 25, 37.5, 50, 62.5, 70, 87.5 , 99}; //showing percent
Adafruit_SSD1306 oled(128, 64, &Wire, -1);
Adafruit_INA219 ina219;
void DisplayCheck(){
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
while(true);
}
delay(2000);
oled.clearDisplay();
}
void InaCheck() {
if (!ina219.begin()) {
Serial.println(F("Failed to find INA219 chip"));
while (true);
}
delay(2000);
}
void bCase(){
byte bcase[10][4] = {
{6, 4, 64, 2},
{6, 42, 64, 2},
{3, 7, 2, 34},
{71, 7, 2, 34},
{4, 5, 2, 2},
{4, 41, 2, 2},
{70, 5, 2, 2},
{70, 41, 2, 2},
{73, 15, 3, 18},
{76, 17, 1, 14}
};
for (uint16_t x = 0; x < 10; x++) {
oled.fillRect (bcase[x][0], bcase[x][1], bcase[x][2], bcase[x][3], WHITE);
};
}
void setup() {
Serial.begin(9600);
pinMode(A0, INPUT);
pinMode(12, INPUT);
DisplayCheck();
//InaCheck();
}
void loop() {
rawval = analogRead(A0);
valV = (rawval / 1023) * 5;
per = (valV - 3.00) / (4.20 - 3.00) * 100; //for 3 - 4,2V Lion
valC = 1; //ina219.getCurrent_mA();
if(digitalRead(12) == 1) valW = 19 * valC; //19V input
else valW = (valV * 4) * valC; //P = V * I //*4S battery
bCase();
bPer();
bVoltage();
AmpCurrent();
AmpWatts();
if(digitalRead(12) == 1) Charging(); //if D12 Active Charging
if(digitalRead(12) == 0) bLvl(); //if D12 Deactive current battery lvl
oled.display();
delay(20);
oled.clearDisplay();
}
////////DISPLAY VALUES/////////
void bPer() {
oled.setTextSize(2);
oled.setTextColor(WHITE);
oled.setCursor(79, 17);
oled.print(per);
oled.print("%");
}
void bVoltage() {
oled.setTextSize(1);
oled.setTextColor(WHITE);
oled.setCursor(5, 50);
oled.print(valV * 4); //4S 5*4
oled.print("V");
}
void AmpCurrent() {
oled.setTextSize(1);
oled.setTextColor(WHITE);
oled.setCursor(50, 50);
oled.print(valC);
oled.print("A");
}
void AmpWatts() {
oled.setTextSize(1);
oled.setTextColor(WHITE);
oled.setCursor(90, 50);
oled.print(valW);
oled.print("W");
}
///////////////////////////////
//battery lvl
void bLvl(){
if (per < 12.5 && per > 0) {
delay(750);
oled.fillRect(7, 8, 6, 32, WHITE);
oled.display();
delay(1000);
oled.fillRect(7, 8, 6, 32, BLACK);
oled.display();
}
byte defval = 7;
for(uint8_t i = 0; i < 8; i++) {
if (per > perVallay[i]) {
oled.fillRect(defval, 8, 6, 32, WHITE);
defval = defval + 8;
}
}
defval = 7;
}
//charging
void Charging() {
//if fully charge starts blinking
if (per >= 100) {
delay(750);
for(byte i = 0, defval = 7; i < 8; i++) {
oled.fillRect(defval, 8, 6, 32, WHITE);
defval = defval + 8;
}
oled.display();
delay(1000);
oled.fillRect(7, 8, 62, 32, BLACK);
oled.display();
}
//else charging
else {
byte defval = 7;
for(uint8_t i = 0; i < 8; i++) {
if(digitalRead(12) == 0) break;
delay(750);
oled.fillRect(defval, 8, 6, 32, WHITE);
oled.display();
defval = defval + 8;
}
defval = 7;
oled.fillRect(7, 8, 64, 32, BLACK);
}
}