#include <U8glib.h>
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_DEV_0|U8G_I2C_OPT_NO_ACK|U8G_I2C_OPT_FAST);
int pot_val = 0;
char buffer[20];
int string_width;
float pixel_x = 0;
float pixel_y = 0;
float line_x = 0;
float line_y = 0;
float text_x = 0;
float text_y = 0;
int center_x = 64; //shift from original x= 0 (0,0)
int center_y = 108; //shift from original y= 0 (0,0)
int radius_pixel = 92;
int radius_line = 87;
int radius_text = 75;
int angle;
int tick_val;
void setup()
{
Serial.begin(9600);
u8g.setFont(u8g_font_tpssb);
u8g.setColorIndex(1);
}
void loop()
{
u8g.firstPage();
do
{
u8g.setColorIndex(1);
u8g.setFont(u8g_font_6x10r);
for(int i=-48; i<=48; i=i+3) //origin start postion of angle = -48 and end position of angle = 48
{
angle = i + ((pot_val * 3) / 10 ) % 3; //i + offset value by ((pot_value * 3) / 10) % 3
tick_val = round((pot_val / 10.0) + angle / 3.0);
pixel_x = sin(radians(angle)) * radius_pixel + center_x;
pixel_y = -cos(radians(angle)) * radius_pixel + center_y;
if(pixel_x > 0 && pixel_x < 128 && pixel_y > 0 && pixel_y < 64) //draw graphic only within screen size 128x64
{
if(tick_val % 10 == 0) //********large tick with number
{
line_x = sin(radians(angle)) * radius_line + center_x;
line_y = -cos(radians(angle)) * radius_line + center_y;
u8g.drawLine(pixel_x, pixel_y, line_x, line_y);
text_x = sin(radians(angle)) * radius_text + center_x;
text_y = -cos(radians(angle)) * radius_text + center_y;
itoa(tick_val, buffer, 10);
string_width = u8g.getStrWidth(buffer);
u8g.drawStr(text_x - string_width / 2, text_y, buffer); //align center by subtract width / 2
}
else //******** small tick
{
u8g.drawPixel(pixel_x, pixel_y);
}
}
}
u8g.setFont(u8g_font_8x13r);
dtostrf(pot_val /10.0 ,1 , 1, buffer);
sprintf(buffer, "%s%s", buffer, "%");
string_width = u8g.getStrWidth(buffer);
u8g.setColorIndex(1);
u8g.drawRBox(64 - (string_width+4)/2, 0, string_width + 6, 13, 2);
u8g.drawTriangle(64-3, 11, 64+4, 11, 64, 15);
u8g.setColorIndex(0);
u8g.drawStr(64 - string_width / 2, 10, buffer);
}
while(u8g.nextPage());
pot_val = map(analogRead(A0), 0, 1023, 1000,0);
Serial.print("Potentiometer Value:");
Serial.print(" - ");
Serial.println(pot_val);
}