#include "U8glib.h"

U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_DEV_0 | U8G_I2C_OPT_NO_ACK | U8G_I2C_OPT_FAST); // Fast I2C / TWI

static const unsigned char basel [] U8G_PROGMEM = {0x00,0x00,0x08,0x00,0x00,0x00,0x00,0x14,0x00,0x00,0x00,0x70,0x04,0x07,0x00,0x00,0x0c,0x14,0x18,0x00,0x00,0x02,0x08,0x20,0x02,0x10,0x01,0x00,0x40,0x00,0x80,0x00,0x08,0x80,0x00,0x40,0x00,0x00,0x00,0x01,0x20,0x00,0x00,0x20,0x02,0x10,0x01,0x00,0x00,0x04,0x08,0x00,0x00,0x00,0x08,0x08,0x00,0x00,0x00,0x08,0x04,0x00,0x00,0x00,0x10,0x04,0x00,0x00,0x00,0x10,0x04,0x00,0x00,0x00,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x00,0x00,0x00,0x30,0x04,0x00,0x00,0x00,0x50,0x22,0x00,0x00,0x00,0x31,0x04,0x00,0x00,0x00,0x50,0x03,0x00,0x00,0x00,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x10,0x04,0x00,0x00,0x00,0x10,0x04,0x00,0x00,0x00,0x10,0x08,0x00,0x00,0x00,0x08,0x08,0x00,0x00,0x00,0x08,0x10,0x00,0x00,0x40,0x04,0x20,0x02,0x00,0x00,0x02,0x40,0x00,0x00,0x00,0x01,0x80,0x00,0x08,0x80,0x00,0x00,0x01,0x00,0x40,0x04,0x20,0x02,0x14,0x20,0x00,0x00,0x0c,0x2c,0x18,0x00,0x00,0x70,0x2c,0x07,0x00,0x00,0x00,0x2c,0x00,0x00,0x00,0x00,0x14,0x00,0x00};
long display_value = 65000;
float angle = 0;
int potentiometer_pin = 2;
int smooth_movement = 0; 

void draw(void) {
 u8g.setColorIndex(1);

u8g.drawXBMP(9, 12, 39, 39, basel);

float x_0 = -sin(angle * 3.141592654 / 180.0)*13; // радиус 13
float y_0 = cos(angle * 3.141592654 / 180.0)*13;

float x_1 = -sin((angle+10) * 3.141592654 / 180.0)*10; //радиус 10
float y_1 = cos((angle+10) * 3.141592654 / 180.0)*10;

float x_2 = -sin((angle-10) * 3.141592654 / 180.0)*10; // радиус 10
float y_2 = cos((angle-10) * 3.141592654 / 180.0)*10;

float x_3 = -sin(angle * 3.141592654 / 180.0)*11; // радиус 11
float y_3 = cos(angle * 3.141592654 / 180.0)*11;


x_0 = 28 + x_0;
y_0 = 31 + y_0;
x_1 = 28 + x_1;
y_1 = 31 + y_1;
x_2 = 28 + x_2;
y_2 = 31 + y_2;
x_3 = 28 + x_3;
y_3 = 31 + y_3;

x_0 = (int)(x_0+0.5);
y_0 = (int)(y_0+0.5);
x_1 = (int)(x_1+0.5);
y_1 = (int)(y_1+0.5);
x_2 = (int)(x_2+0.5);
y_2 = (int)(y_2+0.5);
x_3 = (int)(x_3+0.5);
y_3 = (int)(y_3+0.5);


// MPH needle
  u8g.drawLine(x_0, y_0, x_1, y_1);
  u8g.drawLine(x_0, y_0, x_2, y_2);
  u8g.drawLine(x_0, y_0, x_3, y_3);


}




void setup(void) {
  u8g.setContrast(255); // set maximum contrast for the display
  Serial.begin(9600);   // start serial connection, used only for debugging on the PC, can be commented out once the project is complete
}



void loop(void) {
  // picture loop
  u8g.firstPage();  
  do {
    draw();

  } while( u8g.nextPage() );
  
  display_value++;
  int sensorValue = analogRead(A2);
  angle = map(sensorValue, 0, 1024, 0, 360);  // remap the potentiometer value from 0-670 to 45-315°. It does not go all the way to 1024 because I´m only using 3.3V 
  


}