// simple project using Arduino UNO and 128x64 SSD1306 IIC OLED Display, created by upir, 2023
// youtube channel: https://www.youtube.com/upir_upir
// YOUTUBE VIDEO: https://youtu.be/Eyvzw_ujcS0
// More videos with Arduino UNO and OLED screens: https://www.youtube.com/playlist?list=PLjQRaMdk7pBZ1UV3IL5ol8Qc7R9k-kwXA
// Links from the video:
// Lopaka editor: https://lopaka.app/
// 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
#include <Arduino.h>
#include <U8g2lib.h>
#include <Wire.h> // library requires for IIC communication
char buffer[32]; // helper buffer to construct a string to be displayed
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE); // initialization for the used OLED display
static const unsigned char image_battery_100_bits[] U8X8_PROGMEM = {0x00,0x00,0x00,0xf0,0xff,0x7f,0x08,0x00,0x80,0xa8,0xaa,0xaa,
0xae,0xaa,0xaa,0xa1,0xaa,0xaa,0xa1,0xaa,0xaa,0xa1,0xaa,0xaa,0xa1,0xaa,0xaa,0xa1,0xaa,0xaa,0xae,0xaa,0xaa,0xa8,0xaa,0xaa,0x08,0x00,0x80,0xf0,0xff,0x7f,0x00,0x00,0x00,0x00,0x00,0x00};
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_50_2_bits[] U8X8_PROGMEM = {0x00,0x00,0x00,0xf0,0xff,0x7f,0x08,0x00,0x80,0x08,0xa0,0xaa,0x0e,0xa0,0xaa,0x01,0xa0,0xaa,0x01,0xa0,0xaa,0x01,0xa0,0xaa,0x01,0xa0,0xaa,0x01,0xa0,0xaa,0x0e,0xa0,0xaa,0x08,0xa0,0xaa,0x08,0x00,0x80,0xf0,0xff,0x7f,0x00,0x00,0x00,0x00,0x00,0x00};
static const unsigned char image_battery_10_1_bits[] U8X8_PROGMEM = {0x00,0x00,0x00,0xf0,0xff,0x7f,0x08,0x00,0x80,0x08,0x00,0xa0,0x0e,0x00,0xa0,0x01,0x00,0xa0,0x01,0x00,0xa0,0x01,0x00,0xa0,0x01,0x00,0xa0,0x01,0x00,0xa0,0x0e,0x00,0xa0,0x08,0x00,0xa0,0x08,0x00,0x80,0xf0,0xff,0x7f,0x00,0x00,0x00,0x00,0x00,0x00};
static const unsigned char image_battery_0_bits[] U8X8_PROGMEM = {0x00,0x00,0x00,0xf0,0xff,0x7f,0x08,0x00,0x80,0x08,0x02,0x82,0x0e,0x04,0x81,0x01,0x88,0x80,0x01,0x50,0x80,0x01,0x20,0x80,0x01,0x50,0x80,0x01,0x88,0x80,0x0e,0x04,0x81,0x08,0x02,0x82,0x08,0x00,0x80,0xf0,0xff,0x7f,0x00,0x00,0x00,0x00,0x00,0x00};
static const unsigned char image_battery_20_bits[] U8X8_PROGMEM = {0x00,0x00,0x00,0xf0,0xff,0x7f,0x08,0x00,
0x80,0x08,0x00,0xa8,0x0e,0x00,0xa8,0x01,0x00,0xa8,0x01,0x00,0xa8,0x01,0x00,0xa8,0x01,0x00,0xa8,0x01,0x00,0xa8,0x0e,0x00,0xa8,0x08,0x00,0xa8,0x08,0x00,0x80,0xf0,0xff,0x7f,0x00,0x00,0x00,0x00,0x00,0x00};
int value; //save analog value
void setup() {
// put your setup code here, to run once:
u8g2.begin(); // start the u8g2 library
u8g2.setFontMode(1);
u8g2.setBitmapMode(1);
}
void loop() {
u8g2.clearBuffer(); // clear the internal memory
u8g2.sendBuffer();
value = analogRead(A0); //Read and save analog value from potentiometer
value = map(value, 0, 1023, 0, 420);
u8g2.setFont(u8g2_font_4x6_tr);
u8g2.drawStr(84, 16, value);
if(value>=300 && value<400){
u8g2.clearBuffer();
u8g2.drawXBMP(53, 22, 24, 16,image_battery_50_2_bits);
u8g2.drawStr(50,10, "Battery half");
u8g2.sendBuffer();
delay(2200);
}
if(value>=400){
u8g2.clearBuffer();
u8g2.drawXBMP(53, 22, 24, 16,image_battery_100_bits);
u8g2.setFont(u8g2_font_4x6_tr);
u8g2.drawStr(50, 10, "Battery full");
u8g2.sendBuffer();
delay(2200);
}
if(value>250 && value <=300){
u8g2.clearBuffer();
u8g2.drawXBMP(53, 22, 24, 16,image_battery_20_bits);
u8g2.setFont(u8g2_font_4x6_tr);
u8g2.drawStr(50,10, " Battery low");
u8g2.sendBuffer();
delay(2000);
}
if(value<=250 && value>=200){
u8g2.clearBuffer();
u8g2.drawXBMP(53, 22, 24, 16,image_battery_10_1_bits);
u8g2.setFont(u8g2_font_4x6_tr);
u8g2.drawStr(50, 10, "Battery low");
u8g2.sendBuffer();
delay(2200);
}
if(value<200 ){
u8g2.clearBuffer();
u8g2.drawXBMP(53, 22, 24, 16,image_battery_charging_bits);
u8g2.setFont(u8g2_font_4x6_tr);
u8g2.drawStr(50, 10, "Recharge Battery");
u8g2.sendBuffer();
delay(2000);
}
}