#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// ขนาดหน้าจอ OLED
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
// กำหนดพินและที่อยู่ของจอ OLED
#define OLED_RESET -1
#define SCREEN_ADDRESS 0x3C // Address ของ OLED I2C
// สร้างจอ OLED
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// ขนาดรูปภาพ 'fire'
const int fireWidth = 50;
const int fireHeight = 71;
// ข้อมูลรูปภาพ 'fire' ขนาด 50x71 พิกเซล
const unsigned char epd_bitmap_fire [] PROGMEM = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xf7,
0x7f, 0xff, 0xff, 0xc0, 0xff, 0xbf, 0xff, 0x7f, 0xfe, 0xff, 0xc0, 0xff, 0xff, 0xef, 0x7f, 0xf7,
0xff, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xdf, 0xc0, 0xff, 0xff, 0xde, 0xff, 0xdf, 0xdf, 0xc0,
0xff, 0xff, 0xde, 0xff, 0xbf, 0xdf, 0xc0, 0xff, 0xff, 0xdd, 0xff, 0x7f, 0xff, 0xc0, 0xff, 0xff,
0xdc, 0x07, 0x7f, 0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0x00, 0x03,
0xf7, 0xc0, 0xff, 0xd8, 0xff, 0x40, 0x07, 0xff, 0xc0, 0xff, 0xda, 0x7f, 0x40, 0xc3, 0xff, 0xc0,
0xff, 0xbf, 0xff, 0xff, 0xdf, 0xf7, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff, 0xc0, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xfb, 0xff, 0xff, 0xff, 0xff,
0xc0, 0xff, 0xff, 0x3f, 0xff, 0x8f, 0xff, 0xc0, 0xff, 0xff, 0x80, 0x60, 0x0f, 0xff, 0xc0, 0xff,
0xff, 0x90, 0x62, 0x1f, 0xff, 0xc0, 0xff, 0xff, 0x80, 0xe0, 0x1f, 0xff, 0xc0, 0xff, 0xff, 0xc0,
0xf0, 0x3f, 0xff, 0xc0, 0xff, 0xff, 0xc1, 0xf0, 0x3f, 0xff, 0xc0, 0xff, 0xff, 0xc3, 0xf0, 0x7f,
0xff, 0xc0, 0xff, 0xff, 0xff, 0xfd, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xc0
};
// ฟังก์ชัน setup
void setup() {
// เริ่มต้นการเชื่อมต่อ I2C กับ OLED
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
}
// ฟังก์ชัน loop
void loop() {
// ตัวอย่างค่าแรงดัน
float voltage = 3.2;
// ล้างหน้าจอ
display.clearDisplay();
// แสดงค่า Voltage ทางด้านซ้าย (ลดขนาดตัวอักษรลงเป็นขนาดเล็กสุด)
display.setTextSize(1);
display.setCursor(0, 0);
display.print("Voltage: ");
display.print(voltage, 2);
display.println(" V");
// วาด Progress Bar ระหว่างข้อความ "Voltage" และ "Binary"
int voltageInt = voltage * 100;
int progress = map(voltageInt, 0, 500, 0, 100);
int barWidth = map(progress, 0, 100, 0, SCREEN_WIDTH - 20);
display.drawRect(10, 12, SCREEN_WIDTH - 20, 8, SSD1306_WHITE); // วาดกรอบ Progress Bar
display.fillRect(10, 12, barWidth, 8, SSD1306_WHITE); // เติม Progress Bar
// แสดงค่า Binary ทางด้านซ้าย
String binaryStr = String(voltageInt, BIN);
display.setCursor(0, 24); // ย้าย Binary ลงมา
display.print("Binary: ");
display.println(binaryStr);
// แสดงค่า Decimal ทางด้านซ้าย
display.setCursor(0, 36); // ย้าย Decimal ลงมา
display.print("Decimal: ");
display.println(voltageInt);
// วาดรูปภาพ 'fire' ทางด้านขวา
display.drawBitmap(78, 0, epd_bitmap_fire, fireWidth, fireHeight, SSD1306_WHITE);
// แสดงผลบนหน้าจอ
display.display();
delay(2000); // หน่วงเวลา 2 วินาทีก่อนทำซ้ำลูป
}