#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup() {
Serial.begin(9600);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.display();
delay(2000);
display.clearDisplay();
}
void loop() {
// Read battery level (0-100)
int batteryLevel = analogRead(A0); // Connect your battery sensor to A0
// Map the analog reading to a percentage value
int batteryPercent = map(batteryLevel, 0, 4095, 0, 100);//12 bit ADC
// Display battery percentage
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.print("Battery Level:");
display.setTextSize(2);
display.setCursor(0, 16);
display.print(batteryPercent);
display.print("%");
display.display();
delay(1000); // You can adjust the delay according to your needs
}