#include <U8g2lib.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#define ldr A7
int buttonPin = 2; // Digital pin where the button is connected.
int ledPin = 13; // Digital pin where the LED is connected.
bool buttonState = false;
const int blinkInterval = 200;
unsigned long statusTimer = millis();
#define ALIGN_CENTER(t) ((128 - u8g2.getUTF8Width(t) )/ 2)
#define ALIGN_LEFT 0
U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
volatile int encoderPos = 0;
int lastEncoderPos = 0;
unsigned long lastInterruptTime = 0;
unsigned long debounceDelay = 20; // Vertraging tegen debouncing
int item_selected = 0; // which item in the menu is selected
int currentPage = 0; // เพจปัจจุบัน
bool speedTeststatus = false;
bool breakInrun = false;
bool pauseState = false;
void setup() {
u8g2.begin();
Serial.begin(9600);
u8g2.setPowerSave(0);
pinMode(ledPin, OUTPUT); // LED pin as an output.
pinMode(buttonPin, INPUT); // Button pin as an input.
pinMode(ldr, INPUT);
}
void loop() {
u8g2.clearBuffer(); // required for page drawing mode for u8g2 library
u8g2.setBitmapMode(0);
u8g2.setFont(u8g2_font_courB08_tr);
u8g2.firstPage();
do {
switch (currentPage) {
case 0: //Speed Test UI
mainUI();
battDisplay();
speedTestUI();
testRun();
break;
}
u8g2.sendBuffer(); // send buffer from RAM to display controller
} while ( u8g2.nextPage() );
}
void mainUI() {
u8g2.setFont(u8g2_font_helvB08_tr);
u8g2.setDrawColor(1);
u8g2.drawStr(2,11,"nerdRUNNER");
u8g2.drawBox(104, 4, 2, 4);
u8g2.drawRFrame(106, 1, 18, 10, 2);
u8g2.drawBox(0,13,84,25);
u8g2.drawBox(85,13,40,25);
u8g2.drawBox(0,39,84,25);
u8g2.drawBox(85,39,40,25);
u8g2.setFont(u8g2_font_helvB08_tr);
u8g2.setDrawColor(0);
u8g2.drawStr(3,32,"RPM");
u8g2.drawStr(3,58,"Torque");
u8g2.drawStr(114,32,"V");
u8g2.drawStr(114,58,"A");
u8g2.setFont(u8g2_font_helvB12_tr);
//u8g2.drawStr(33,32,"30000");
u8g2.drawStr(90,32,"3.0");
u8g2.drawStr(45,58,"2.0");
u8g2.drawStr(90,58,"1.5");
}
void speedTestUI() {
unsigned int RPM_data = analogRead(ldr);
int RPM_out = RPM_data * 10 ;
String RPM_s = String(RPM_out);
u8g2.setFont(u8g2_font_helvB12_tr);
u8g2.setDrawColor(0);
u8g2.drawStr(ALIGN_CENTER(String(RPM_out).c_str())-13 ,32, "30000");
}
void battDisplay() {
//int sensorValue = analogRead(A0); // อ่านค่าแรงดันจากขาอะนาล็อก A0
//float voltage = sensorValue * (5.0 / 1023.0); // แปลงค่า ADC เป็นแรงดันไฟฟ้า
//int battPercent = (voltage / 3.7) * 100; // แปลงแรงดันไฟฟ้าเป็นเปอร์เซ็นต์แบตเตอรี่
int battPercent = 100;
u8g2.setDrawColor(1);
if (battPercent > 100) battPercent = 100; // แก้ไขค่าเกิน 100%
if (battPercent < 0) battPercent = 0; // แก้ไขค่าต่ำกว่า 0%
//u8g2.drawRFrame(106, 1, 18, 10, 2);
if (battPercent > 0) {
u8g2.drawBox(108, 3, 4, 6); // วาดกล่องแรก (0% to 32%)
}
if (battPercent > 32) {
u8g2.drawBox(113, 3, 4, 6); // วาดกล่องที่สอง (33% to 65%)
}
if (battPercent > 65) {
u8g2.drawBox(118, 3, 4, 6); // วาดกล่องที่สาม (66% to 100%)
}
}
void testRun() {
buttonState = digitalRead(buttonPin);
// Check if the button is pressed
if (buttonState == HIGH){
digitalWrite(ledPin, HIGH); // Turn LED on.
if(millis()-statusTimer >=blinkInterval){
u8g2.setDrawColor(1);
u8g2.setFont(u8g2_font_9x15_t_symbols);
u8g2.drawGlyph(92, 11, 0x25b8);
statusTimer=millis();
}
//Test Run
} else {
digitalWrite(ledPin, LOW); // Turn LED off.
}
}