#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
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE); // initialization for the 128x64px OLED display
//U8G2_SSD1306_128X32_UNIVISION_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE); // initialization for the 128x32px OLED display, [full framebuffer, size = 512 bytes]
int str_width; // calculated width of the string
char buffer[20]; // helper buffer for displaying strings on the display
void setup(void) {
u8g2.begin(); // start the u8g2 library
pinMode(A0, INPUT); // set the analog input pin A0 as input
Serial.begin(9600); // open the serial port at 9600 bps:
}
void loop(void) {
int val = analogRead(0);
int oil = map(val, 0, 1023, -45, 225);
Serial.print(oil);
Serial.print(" ");
double sinus = sin(radians(oil));
double cosin = cos(radians(oil));
double sinuspfeil1 = sin(radians(oil+90));
double sinuspfeil2 = sin(radians(oil-90));
double cosinpfeil1 = cos(radians(oil+90));
double cosinpfeil2 = cos(radians(oil-90));
Serial.print(sinus);
Serial.print(" ");
Serial.println(cosin);
u8g2.setDrawColor(1); // set the drawing color to white
u8g2.clearBuffer();
u8g2.setFontMode(1);
u8g2.setBitmapMode(1);
u8g2.setFont(u8g2_font_ncenB08_tr);
sprintf(buffer, "%d", oil); // convert compass degree integer to string, add the ' symbol that (somehow) looks like degree symbol (degree symbol is not present in the font)
str_width = u8g2.getStrWidth(buffer); // calculate the string width
u8g2.drawStr(64 - str_width / 2, 60, buffer); // draw centered string
// u8g2.drawStr(56, 60, oil);
u8g2.drawEllipse(62, 31, 31, 31);
//u8g2.drawLine(62, 31, 62, 5);
u8g2.drawLine(62, 31, 62+(-cosin*27), 31-(sinus*27));
u8g2.drawLine(62, 31, 62+(-cosinpfeil1*2), 31-(sinuspfeil1*2));
u8g2.drawLine(62, 31, 62+(-cosinpfeil2*2), 31-(sinuspfeil2*2));
u8g2.drawLine(62+(-cosinpfeil1*2), 31-(sinuspfeil1*2), 62+(-cosin*27), 31-(sinus*27));
u8g2.drawLine(62+(-cosinpfeil2*2), 31-(sinuspfeil2*2), 62+(-cosin*27), 31-(sinus*27));
u8g2.sendBuffer();
}