#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED width, in pixels
#define SCREEN_HEIGHT 32 // OLED height, in pixels
#define GRAPH_X 60
#define GRAPH_Y 8
#define GRAPH_WIDTH 36
#define FILL_WIDTH 32
#define GRAPH_HEIGHT 15
// Create an OLED display object connected to I2C
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
const float Vref = 5.0; // VCC Board
void setup() {
Serial.begin(9600); // Initialize serial communication
delay(1000);
oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
oled.clearDisplay(); // Clear the display buffer
oled.display(); // Display the cleared buffer
oled.setTextSize(2); // Set text size
oled.setTextColor(WHITE); // Set text color
oled.setCursor(1, 8); // Set position to display (x,y)
oled.println("ATG LOADER"); // Set text
oled.display(); // Display on OLED
delay(2000); // Wait 2 seconds for loading screen
}
void loop() {
int batteryLevel = analogRead(A0); // Battery Voltage
int lower = 640; //raw A0 voltage reading
int upper = 840; //raw A0 voltage reading
if (batteryLevel > upper) batteryLevel = upper;
else if (batteryLevel < lower) batteryLevel = lower;
int batteryPercent = map(batteryLevel, lower, upper, 0, 100);
float v = (float) (analogRead(A0) * Vref / 1023); // calculate voltage
int graphWidth = batteryPercent / 100.0 * FILL_WIDTH;
oled.clearDisplay();
// Battery Indicator
oled.drawRect(GRAPH_X, GRAPH_Y, GRAPH_WIDTH, GRAPH_HEIGHT, SSD1306_WHITE);
oled.fillRect(62, 10, graphWidth, 11, SSD1306_WHITE); // Battery Indicator
oled.fillRect(96, 12, 4, 7, SSD1306_WHITE); // Battery Indicator
// Battery Percent
oled.setTextColor(SSD1306_WHITE);
oled.setTextSize(2);
oled.setCursor(1, 9);
oled.print(batteryPercent);
oled.print("%");
oled.setCursor(48, 9);
oled.print("|");
// Battery Voltage
oled.setTextSize(1);
oled.setCursor(104, 12);
oled.setTextColor(SSD1306_WHITE);
oled.print(v,1);
oled.print("v");
oled.display();
delay(5000);
}