#include <Arduino.h>
#include <U8g2lib.h> // u8g2 library is used to draw graphics on the OLED display
#include <Wire.h> // library required for IIC communication
//Docs: https://github.com/olikraus/u8g2/wiki/u8g2reference#drawbox
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE); // initialization for the 128x64px OLED display
const int potPin = A0; // analog pin for potentiometer
int heading = 100; // predefined heading marker at 100 degrees
void setup() {
u8g2.begin();
}
void loop() {
int sensorValue = analogRead(potPin);
int degrees = map(sensorValue, 0, 1023, 0, 360);
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_ncenB08_tr); // choose a suitable font
u8g2.setCursor(50, 10);
u8g2.print(degrees);
//u8g2.print(" '");
u8g2.drawLine(0, 15, 128, 15); // horizontal line for compass ticks
// Draw compass ticks and heading marker
for (int i = -90; i <= 90; i += 10) {
int angle = degrees + i;
if (angle < 0) angle += 360;
if (angle >= 360) angle -= 360;
int x = map(i, -90, 90, 0, 128);
if (angle == heading) {
//Syntex: u8g2.drawBox(x,y,w,h);
u8g2.drawBox(x - 1, 20, 5, 5); // draw heading marker (small filled square)
} else {
//u8g2.drawLine(x, 15, x, 15);
u8g2.drawPixel(x, 31); // draw compass tick
}
//So if(i%30==0) means if i is divided by 30 and the remainder is equals
//to 0 then the statement is true else it is false.
if (i % 30 == 0) { // draw degree labels every 30 degrees
u8g2.setCursor(x - 6, 45);
u8g2.print(angle);
}
}
u8g2.sendBuffer(); // transfer internal memory to the display
delay(20); // add a small delay to avoid flickering
}