#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Wire.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define BATTERY_IN_12V A1
#define BATTERY_IN_3V7 A0
void DrawBattery12V(int y) {
int value12V = analogRead(BATTERY_IN_12V);
float voltage12V = (value12V / 1023.0) * 5.0; // Assuming 5.0V as the reference voltage
int percent12V = 0; // Initialize percentage to 0
// Only calculate the percentage if voltage is detected on A1
if (voltage12V > 0) {
percent12V = map(value12V, 0, 1023, 0, 100); // Map the 0-1023 range to 0-100%
}
int batteryWidth12V = map(percent12V, 0, 100, 2, 20); // Smaller battery size, adjusted
int batteryOutlineWidth12V = 20;
int batteryX12V = SCREEN_WIDTH - batteryOutlineWidth12V - 10; // Position from the right
display.drawRect(batteryX12V, y, batteryOutlineWidth12V, 10, SSD1306_WHITE); // Battery outline
// Calculate position for the battery connector icon (close to the battery outline)
int connectorX12V = batteryX12V - 3; // Adjust for positioning close to the outline
int connectorY12V = y + 2; // Adjust for positioning close to the outline
display.fillRect(connectorX12V, connectorY12V, 2, 6, SSD1306_WHITE); // Battery connector
display.fillRect(batteryX12V, y, batteryWidth12V, 10, SSD1306_WHITE); // Fill based on the percentage, within the outline
display.setTextSize(1.4);
display.setTextColor(SSD1306_WHITE);
// Calculate position for the percentage text (left side of the battery connector, with a gap)
int percentX12V = batteryX12V - 30; // Adjust for positioning to the left with more gap
int percentY12V = y + 2; // Adjust for positioning close to the connector
// Only display the percentage value if voltage is detected on A1
if (voltage12V > 0) {
display.setCursor(percentX12V, percentY12V);
display.print(percent12V);
display.print("%");
}
}
void DrawBattery3V7(int y) {
int value3V7 = analogRead(BATTERY_IN_3V7);
int voltage3V7 = (value3V7 / 1023.0) * 5.0; // Assuming 5.0V as the reference voltage
int percent3V7 = map(value3V7, 0, 1023, 0, 100); // Map the 0-1023 range to 0-100%
// Check if the voltage is less than 3.7V and set the percentage to 0%
if (voltage3V7 <= 3.7) {
percent3V7 = 0;
}
// Check if the voltage is greater than or equal to 4.2V and set the percentage to 100%
else if (voltage3V7 >= 4.2) {
percent3V7 = 100;
}
int batteryWidth3V7 = map(percent3V7, 0, 100, 2, 20); // Smaller battery size, adjusted
int batteryOutlineWidth3V7 = 20;
int batteryX3V7 = SCREEN_WIDTH - batteryOutlineWidth3V7 - 10; // Position from the right
display.drawRect(batteryX3V7, y, batteryOutlineWidth3V7, 10, SSD1306_WHITE); // Battery outline
// Calculate position for the battery connector icon (close to the battery outline)
int connectorX3V7 = batteryX3V7 - 3; // Adjust for positioning close to the outline
int connectorY3V7 = y + 2; // Adjust for positioning close to the outline
display.fillRect(connectorX3V7, connectorY3V7, 2, 6, SSD1306_WHITE); // Battery connector
display.fillRect(batteryX3V7, y, batteryWidth3V7, 10, SSD1306_WHITE); // Fill based on the percentage, within the outline
display.setTextSize(1.4); // Larger text size
display.setTextColor(SSD1306_WHITE);
// Calculate position for the percentage text (left side of the battery connector, with a gap)
int percentX3V7 = batteryX3V7 - 30;
int percentY3V7 = y + 2; // Adjust for positioning close to the connector
display.setCursor(percentX3V7, percentY3V7);
display.print(percent3V7);
display.print("%");
}
void setup() {
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;) ;
}
}
void loop() {
display.clearDisplay();
// Draw the border around the OLED display
display.drawRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, SSD1306_WHITE);
DrawBattery12V(5);
DrawBattery3V7(20);
display.display();
}