#include <U8glib.h>
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE | U8G_I2C_OPT_DEV_0);
int xmax = 128;
int ymax = 64;
int xcenter = xmax / 2;
int ycenter = ymax / 2 + 10;
int arc = ymax / 2;
int angle = 0;
char* label[] = {"Km/h", "COOLANT", "INTAKE", "VOLT"};
int labelXpos[] = {53, 45, 49, 53};
#define irSensorPin 2 // Digital pin for IR sensor
#define minRotationSpeed 0 // Minimum rotation speed (adjust as needed)
int p, w, m, a = 10;
u8g_uint_t xx = 0;
void gauge(uint8_t angle) {
u8g.drawCircle(xcenter, ycenter, arc + 3, U8G_DRAW_UPPER_RIGHT);
u8g.drawCircle(xcenter, ycenter, arc + 1, U8G_DRAW_UPPER_RIGHT);
u8g.drawCircle(xcenter, ycenter, arc + 3, U8G_DRAW_UPPER_LEFT);
u8g.drawCircle(xcenter, ycenter, arc + 1, U8G_DRAW_UPPER_LEFT);
float x1 = sin(2 * angle * 2 * 3.14 / 360);
float y1 = cos(2 * angle * 2 * 3.14 / 360);
u8g.drawLine(xcenter, ycenter, xcenter + arc * x1, ycenter - arc * y1);
u8g.drawDisc(xcenter, ycenter, 5, U8G_DRAW_UPPER_LEFT);
u8g.drawDisc(xcenter, ycenter, 5, U8G_DRAW_UPPER_RIGHT);
u8g.setFont(u8g_font_chikita);
u8g.drawStr(24, 42, "0");
u8g.drawStr(25, 30, "10");
u8g.drawStr(27, 20, "20");
u8g.drawStr(36, 12, "30");
u8g.drawStr(47, 7, "40");
u8g.drawStr(60, 5, "50");
u8g.drawStr(73, 7, "60");
u8g.drawStr(84, 12, "70");
u8g.drawStr(92, 20, "80");
u8g.drawStr(98, 30, "90");
u8g.drawStr(100, 42, "100");
u8g.setPrintPos(labelXpos[0], 32);
u8g.print(label[0]);
u8g.setFont(u8g_font_profont22);
u8g.setPrintPos(54, 60);
if (w < 0) {
u8g.print("0");
}
if (w > 99) {
u8g.setPrintPos(47, 60);
}
u8g.print(w);
}
void drawBatteryIndicator(int batteryLevel) {
int barWidth = 2; // Width of each bar in pixels
int numBars = 4; // Number of bars in the battery indicator
int spacing = 1; // Spacing between bars
int startY = 58; // Y position for battery indicator
int totalWidth = (barWidth * numBars) + ((numBars - 1) * spacing); // Total width of the indicator
// Calculate the starting X position to center the indicator
int startX = (xmax - totalWidth) / 2;
// Calculate the height of the indicator
int indicatorHeight = 5;
// Draw the frame of the indicator
u8g.drawFrame(startX, startY, totalWidth, indicatorHeight);
// Calculate the width of each bar segment
int segmentWidth = barWidth + spacing;
// Draw the filled bars based on the battery level
for (int i = 0; i < numBars; i++) {
int barX = startX + (i * segmentWidth);
int barY = startY + 1;
if (i * 100 / numBars <= batteryLevel) {
// Draw a filled bar segment
u8g.drawBox(barX, barY, barWidth, indicatorHeight - 2);
} else {
// Draw an empty bar segment
u8g.drawFrame(barX, barY, barWidth, indicatorHeight - 2);
}
}
}
void setup(void) {
pinMode(irSensorPin, INPUT); // Set IR sensor pin as input
u8g.setFont(u8g_font_chikita);
u8g.setColorIndex(1);
if (u8g.getMode() == U8G_MODE_R3G3B2) {
u8g.setColorIndex(255);
} else if (u8g.getMode() == U8G_MODE_GRAY2BIT) {
u8g.setColorIndex(3);
} else if (u8g.getMode() == U8G_MODE_BW) {
u8g.setColorIndex(1);
} else if (u8g.getMode() == U8G_MODE_HICOLOR) {
u8g.setHiColorByRGB(255, 255, 255);
}
}
void loop(void) {
// Read the state of the IR sensor
int irSensorValue = digitalRead(irSensorPin);
// Calculate the rotation speed based on IR sensor input
if (irSensorValue == LOW) {
// Object detected, increment rotation speed
w++;
} else {
// No object detected, decrement rotation speed
w--;
}
// Ensure rotation speed stays within defined limits
w = constrain(w, minRotationSpeed, 100);
// Calculate the angle for gauge display
m = map(w, 0, 100, 0, 90);
xx = m;
if (xx < 45) {
xx = xx + 135;
} else {
xx = xx - 45;
}
// Display the gauge and battery indicator
u8g.firstPage();
do {
gauge(xx);
drawBatteryIndicator(w); // Draw the battery indicator
} while (u8g.nextPage());
}