// Example sketch for the MPU-6050.
//
// Version 1, 9 August 2021, by Koepel with help from Wokwi community.
//
// Start the simulation and click on the MPU-6050 module to change the values.
//
// Note:
//   This example does not use a library with quaternations for the 3D orientation.
//   The values are directly read from the MPU-6050 and are used to rotate a cube.
//   It is just a fun example. The rotation of the cube is not according to the
//   physical values of the accelerometer and the gyro.
//
// Based on:
//   (1)
//     3D_Cube for Arduino OLED module by Colin Ord, 9/1/2015
//     A port of my (Colin Ord) original JustBasic Cube_3D demo to the Arduino Uno using U8G library.
//     http://colinord.blogspot.com/2015/01/arduino-oled-module-with-3d-demo.html
//     (no known copyrights)
//
//   (2)
//     MPU-6050 Short Example Sketch
//     By Arduino User JohnChi
//     August 17, 2014
//     Public Domain
//     https://playground.arduino.cc/Main/MPU-6050/#short
// 
//   (3)
//     The Adafruit GFX library with the SSD1306 driver.
//

#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>

Adafruit_SSD1306 display( 128, 64);   // 128 pixels width, 64 pixels height

int potpina = A1;
int potpinb = A2;
int potpinc = A3;
// Set the origin in the middle of the display
int y = 0;
int x = 0;
int a = 0;
int b = 0;
int c = 0;

int rangernd = 1;
int range = 1;
const float viewDistance = 150.0;     // higher for less perspective, lower for more.


void setup()
{
  Serial.begin( 115200);

  // Initialize the OLED display and test if it is connected.
  if( !display.begin( SSD1306_SWITCHCAPVCC, 0x3C)) 
  {
    Serial.println(F( "SSD1306 allocation failed"));
    for(;;);                                   // halt the sketch if error encountered
  }

display.setTextColor(SSD1306_WHITE,SSD1306_BLACK);
// bar1a = random(0,22);
display.clearDisplay();

}

void randplus(){
  rangernd = 1+random(0,5);
}

void randminus(){
  rangernd = 1-random(0,5);
}

void loop()
{
display.clearDisplay();
//display.drawCircle(64, 0, range, SSD1306_WHITE );  

for(int i = 0; i <= 128; i++){
  a = analogRead(potpina);     
  a = map(a, 0, 1023, 1, 10000);

  b = analogRead(potpinb);     
  b = map(b, 0, 1023, 0, 1000);

  c = analogRead(potpinc);     
  c = map(c, 0, 1023, 0, 100);

x = 1;
y = 1;

switch (random(1,2)) {
        //select size of phrase
        case 1:randplus(); break;
        case 2:randminus(); break;
        }
//good= a 7683, b 127, c 61;

x = sq(i-c);
y = (a/(b+x));
y = y +rangernd;

display.drawPixel(i, (y+2), SSD1306_WHITE );
display.drawFastVLine(i,0,y,SSD1306_WHITE );

display.setCursor(16,64);
display.print(a);
display.setCursor(32,64);
display.print(b);
display.setCursor(48,64);
display.print(c);
//display.drawCircle(64, 32, rangernd, SSD1306_WHITE );
//display.drawCircle(64, 32, i, SSD1306_WHITE );

//Serial.print("range for ");
//Serial.print(range);
//Serial.print(" rangevl ");
//Serial.print(rangevl);
Serial.print(" a ");
Serial.print(a);
Serial.print(" b ");
Serial.print(b);
Serial.print(" c ");
Serial.print(c);
Serial.print(" x ");
Serial.print(x);
Serial.print(" i ");
Serial.print(i);

Serial.print(" y ");
Serial.println(y);
//Serial.print(" dive ");
//Serial.println(dive1);


/*if(dive <= 0){
  dive=1;
}
else if(dive >=32){
  dive=32;
}
*/
i=i+2;
//delay(100);
}

display.display();

}