// simple project using Arduino UNO and 128x64 SSD1306 IIC OLED Display to show battery charging indicator
// created by upir, 2023
// youtube channel: https://www.youtube.com/upir_upir
// YOUTUBE VIDEO: https://youtu.be/caHcaUoQ2kg
// SOURCE files: https://github.com/upiir/arduino_oled_battery_indicator
// WOKWI sketch: https://wokwi.com/projects/376479299848582145
// Links from the video:
// 72x40 SSD1306 OLED 0.42" Display: https://s.click.aliexpress.com/e/_Ddq0EwJ
// 128x64 SSD1306 OLED Display 1.54": https://s.click.aliexpress.com/e/_DCYdWXb
// 128x64 SSD1306 OLED Display 0.96": https://s.click.aliexpress.com/e/_DCKdvnh
// 128x64 SSD1306 OLED Display 2.42": https://s.click.aliexpress.com/e/_DFdMoTh
// Arduino UNO: https://s.click.aliexpress.com/e/_AXDw1h
// Arduino breadboard prototyping shield: https://s.click.aliexpress.com/e/_ApbCwx
// Image2cpp (convert array to image): https://javl.github.io/image2cpp/
// Photopea (online graphics editor like Photoshop): https://www.photopea.com/
// Related videos with Arduino UNO and 128x64 OLED screen:
// Arduino + OLED displays playlist: https://www.youtube.com/playlist?list=PLjQRaMdk7pBZ1UV3IL5ol8Qc7R9k-kwXA
#include <Arduino.h>
#include <U8g2lib.h> // u8g2 library used for drawing on the OLED display
#include <Wire.h> // library requires for IIC communication
// I´m using two different displays, 128x64px in the WOKWI emulation and 72x40px for real Arduino
// Please uncomment the correct initialization line and comment out the other one
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE); // initialization for the 128x64px display
//U8G2_SSD1306_72X40_ER_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);// 72x40px display [full framebuffer, size = 360 bytes]
// images below are generated using the image2cpp website
int progress = 0; // progress of the progressbar
int anim_frame = 0; // animation frame for the blob / metaballs animation
int xoff = 124;//0;//29; // x offset for the battery
int yoff = 12;//0;//24; // y offset for the battery
static const unsigned char image_battery_charging_bits[] U8X8_PROGMEM = {0x00,0x40,0x00,0xf0,0x27,0x7f,0x08,0x30,0x80,0x08,0x10,0x80,0x0e,0x18,0x80,0x01,0x0c,0x80,0x01,0xfc,0x81,0x01,0xfe,0x80,0x01,0xc0,0x80,0x01,0x60,0x80,0x0e,0x20,0x80,0x08,0x30,0x80,0x08,0x10,0x80,0xf0,0xcb,0x7f,0x00,0x08,0x00,0x00,0x00,0x00};
static const unsigned char image_battery_empty_bits[] U8X8_PROGMEM = {0x00,0x00,0x00,0xf0,0xff,0x7f,0x08,0x00,0x80,0x08,0x00,0x80,0x0e,0x00,0x80,0x01,0x00,0x80,0x01,0x00,0x80,0x01,0x00,0x80,0x01,0x00,0x80,0x01,0x00,0x80,0x0e,0x00,0x80,0x08,0x00,0x80,0x08,0x00,0x80,0xf0,0xff,0x7f,0x00,0x00,0x00,0x00,0x00,0x00};
#define button 2
void setup(void) {
u8g2.begin(); // start the u8g2 library
pinMode(button, INPUT_PULLUP);
}
void loop(void) {
u8g2.clearBuffer();
u8g2.setFontMode(1);
if(digitalRead(button)==0){
u8g2.drawXBMP(103, 0, 24, 16, image_battery_charging_bits);
}
else{
u8g2.drawXBMP(103, 0, 24, 16, image_battery_empty_bits);
int fill_width = map(progress, 0, 17, 2, 17); // width of the fill rectangle
u8g2.drawRBox(xoff-fill_width, 3, fill_width,9, 1); // battery fill, rounded filled rectangle
}
//u8g2.drawBox(123, 3, battery_fill_perc, 1);
u8g2.sendBuffer();
// increase the progress value to go over 0-100
progress = progress + 1;
if (progress > 17) {
progress = 0;
}
}