#include <Arduino.h>
#include <U8g2lib.h> // u8g2 library for drawing on OLED display - needs to be installed in Arduino IDE first
#include <Wire.h>
U8G2_SSD1306_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0); // set the OLED display to 128x64px, HW IIC, no rotation, used in WOKWI
int center_x = 35; // display x center, 64px for the 128x128px display
int center_y = 40; // display y center, 64px for the 128x128px display
int speed;
int speed1;
void setup(void) { // Arduino setup
u8g2.begin(); // begin the u8g2 library
u8g2.setContrast(255); // set display contrast/brightness
}
void loop(void) {
u8g2.firstPage();
do {
speed=analogRead(A0);
speed1=map(speed,0,1023,0,9000);
speed=map(speed,0,1024,225,360+45*3);
draw_background();
draw_hand_thin(speed,30,0);
u8g2.setFont(u8g2_font_ncenB14_tr);
u8g2.setCursor(85, 45);
u8g2.print(speed1);
// draw the center circle to cover the center part of the hands
u8g2.setColorIndex(0); // black color
u8g2.drawDisc(center_x, center_y, 4);
u8g2.setColorIndex(1); // white color
u8g2.drawCircle(center_x, center_y, 4);
} while ( u8g2.nextPage() );
}
void draw_background() {
float xpos;
float ypos;
float xpos2;
float ypos2;
u8g2.setDrawColor(1); // set the drawing color to white
u8g2.drawCircle(center_x, center_y, 33, U8G2_DRAW_ALL); // draw fullscreen circle
// draw 60 dots (pixels) around the circle, one for every minute/second
for (int i=0; i<60; i++) { // draw 60 pixels around the circle
xpos = round(center_x + sin(radians(i * 6)) * 35); // calculate x pos based on angle and radius
ypos = round(center_y - cos(radians(i * 6)) * 35); // calculate y pos based on angle and radius
u8g2.drawPixel(xpos,ypos); // draw white pixel on position xpos and ypos
}
for (int i=0; i<8; i++) { // draw 60 pixels around the circle
xpos = round(center_x + sin(radians(i *45)) * 31); // calculate x pos based on angle and radius
ypos = round(center_y - cos(radians(i *45)) * 31); // calculate y pos based on angle and radius
u8g2.drawPixel(xpos,ypos); // draw white pixel on position xpos and ypos
}
for (int i=0; i<8; i++) { // draw 60 pixels around the circle
xpos = round(center_x + sin(radians(i *45)) * 30); // calculate x pos based on angle and radius
ypos = round(center_y - cos(radians(i *45)) * 30); // calculate y pos based on angle and radius
u8g2.drawPixel(xpos,ypos); // draw white pixel on position xpos and ypos
}
for (int i=0; i<8; i++) { // draw 60 pixels around the circle
xpos = round(center_x + sin(radians(i *45)) * 29); // calculate x pos based on angle and radius
ypos = round(center_y - cos(radians(i *45)) * 29); // calculate y pos based on angle and radius
u8g2.drawPixel(xpos,ypos); // draw white pixel on position xpos and ypos
}
for (int i=0; i<8; i++) { // draw 60 pixels around the circle
xpos = round(center_x + sin(radians(i *45)) * 28); // calculate x pos based on angle and radius
ypos = round(center_y - cos(radians(i *45)) * 28); // calculate y pos based on angle and radius
u8g2.drawPixel(xpos,ypos); // draw white pixel on position xpos and ypos
}
u8g2.setFont(u8g2_font_5x7_mn); // set the u8g2 font
u8g2.drawStr(10,60,"0");
u8g2.drawStr(8,43,"1.5");
u8g2.drawStr(18,26,"3");
u8g2.drawStr(29,18,"4.5");
u8g2.drawStr(50,26,"6");
u8g2.drawStr(50,43,"7.5");
u8g2.drawStr(50,60,"9");
u8g2.setFont(u8g2_font_5x7_mf);
u8g2.drawStr(22,64,"K_RPM");
u8g2.setFont(u8g2_font_t0_11b_mr);
u8g2.drawStr(110,64,"RPM");
}
void draw_hand_thin (int hand_angle, int hand_lenght_long, int hand_legth_short) {
float xpos;
float ypos;
float xpos2;
float ypos2;
// calculate starting and ending position of the second hand
xpos = round(center_x + sin(radians(hand_angle)) * hand_lenght_long); // calculate x pos based on angle and radius
ypos = round(center_y - cos(radians(hand_angle)) * hand_lenght_long); // calculate y pos based on angle and radius
xpos2 = round(center_x + sin(radians(hand_angle + 180)) * hand_legth_short); // calculate x pos based on angle and radius
ypos2 = round(center_y - cos(radians(hand_angle + 180)) * hand_legth_short); // calculate y pos based on angle and radius
u8g2.drawLine(xpos, ypos, xpos2, ypos2); // draw the main line
u8g2.setDrawColor(0); // black color
u8g2.drawDisc(xpos2, ypos2, 3); // draw small filled black circle
u8g2.setDrawColor(1); // white color
u8g2.drawCircle(xpos2, ypos2, 3, U8G2_DRAW_ALL); // draw small outline white circle
}