#include <Servo.h>
#include <U8g2lib.h>
#include <Wire.h>
#include <avr/pgmspace.h>
#define SERVO_PIN 4
#define BUTTON_PIN 2
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE);
const unsigned char epd_bitmap_Power1 [] PROGMEM = {
0xf8, 0xff, 0x3f, 0x00, 0x04, 0x00, 0x40, 0x00, 0x02, 0x00, 0x80, 0x00, 0x01, 0x00, 0x00, 0x01,
0x01, 0x10, 0x00, 0x01, 0x01, 0x38, 0x00, 0x01, 0x01, 0x38, 0x00, 0x01, 0x81, 0x39, 0x03, 0x01,
0xc1, 0x39, 0x07, 0x01, 0xe1, 0x38, 0x0e, 0x01, 0x61, 0x38, 0x0c, 0x01, 0x61, 0x38, 0x0c, 0x01,
0x61, 0x10, 0x0c, 0x01, 0x61, 0x00, 0x0c, 0x01, 0x61, 0x00, 0x0c, 0x01, 0x61, 0x00, 0x0c, 0x01,
0xe1, 0x00, 0x0e, 0x01, 0xc1, 0x01, 0x07, 0x01, 0x81, 0x83, 0x03, 0x01, 0x01, 0xff, 0x01, 0x01,
0x01, 0x7c, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x03, 0x00, 0x80, 0x01,
0x06, 0x00, 0xc0, 0x00, 0xfc, 0xff, 0x7f, 0x00, 0xf8, 0xff, 0x3f, 0x00
};
// 'Power_hvr1', 25x27px
const unsigned char epd_bitmap_Power_hvr1 [] PROGMEM = {
0xf8, 0xff, 0x3f, 0x00, 0xfc, 0xff, 0x7f, 0x00, 0xfe, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x01,
0xff, 0xef, 0xff, 0x01, 0xff, 0xc7, 0xff, 0x01, 0xff, 0xc7, 0xff, 0x01, 0x7f, 0xc6, 0xfc, 0x01,
0x3f, 0xc6, 0xf8, 0x01, 0x1f, 0xc7, 0xf1, 0x01, 0x9f, 0xc7, 0xf3, 0x01, 0x9f, 0xc7, 0xf3, 0x01,
0x9f, 0xef, 0xf3, 0x01, 0x9f, 0xff, 0xf3, 0x01, 0x9f, 0xff, 0xf3, 0x01, 0x9f, 0xff, 0xf3, 0x01,
0x1f, 0xff, 0xf1, 0x01, 0x3f, 0xfe, 0xf8, 0x01, 0x7f, 0x7c, 0xfc, 0x01, 0xff, 0x00, 0xfe, 0x01,
0xff, 0x83, 0xff, 0x01, 0xff, 0xff, 0xff, 0x01, 0xff, 0xff, 0xff, 0x01, 0xff, 0xff, 0xff, 0x01,
0xfe, 0xff, 0xff, 0x00, 0xfc, 0xff, 0x7f, 0x00, 0xf8, 0xff, 0x3f, 0x00
};
Servo servo;
int servoPosition = 90;
int targetPosition = 90;
unsigned long lastDebounceTime = 0;
const unsigned long debounceDelay = 50;
bool buttonState = HIGH;
bool lastButtonState = HIGH;
void drawXBM_P(const unsigned char* image, int x, int y, int width, int height) {
u8g2.drawXBMP(x, y, width, height, (const uint8_t*)image);
}
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
servo.attach(SERVO_PIN);
servo.write(servoPosition);
u8g2.begin();
Serial.begin(9600);
}
void loop() {
bool reading = digitalRead(BUTTON_PIN);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == LOW) {
targetPosition = (targetPosition == 0) ? 180 : 0;
}
}
}
lastButtonState = reading;
if (servoPosition != targetPosition) {
servoPosition += (targetPosition > servoPosition) ? 1 : -1;
servo.write(servoPosition);
}
u8g2.clearBuffer();
u8g2.setFontMode(1);
u8g2.setBitmapMode(1);
if (servoPosition == 0) {
u8g2.drawXBMP(epd_bitmap_Power1, 93, 7, 25, 27);
} else {
u8g2.drawXBMP(epd_bitmap_Power_hvr1, 93, 7, 25, 27);
}
u8g2.drawFrame(2, 4, 124, 1);
u8g2.drawFrame(2, 59, 124, 1);
u8g2.setFont(u8g2_font_profont11_tr);
u8g2.drawStr(6, 47, "SERVO ANGLE :");
u8g2.drawStr(6, 23, "SERVO STATUS :");
u8g2.drawRFrame(93, 37, 25, 13, 4);
u8g2.setFont(u8g2_font_haxrcorp4089_tr);
u8g2.drawStr(97, 47, "10%");
u8g2.sendBuffer();
}