const int trigPin = 5;
const int echoPin = 18;
#define SOUND_SPEED 0.034 // konversi satuan suara
#define CM_TO_INCH 0.393701 // konversi satuan cm ke inch

long duration;
float distanceCm;
float distanceInch;

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
Adafruit_MPU6050 mpu;

void setup(void) {
  Serial.begin(115200);
  Serial.println("A31-Nadzrin Husnul");
while (!mpu.begin()) { // Coding memastikan apakan MPU terkoneksi or not
Serial.println("MPU6050 not connected!");
delay(1000);
}
Serial.println("MPU6050 ready!");
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT);
      if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
        Serial.println(F("SSD1306 allocation failed"));
        for(;;);
}
delay(500);
    display.clearDisplay();
    display.setTextSize(0.5);
    display.setTextColor(WHITE);
}

sensors_event_t event;

void loop() {

    mpu.getAccelerometerSensor()->getEvent(&event);
  Serial.print("[");
  Serial.print(millis());
  Serial.print("] X: ");
  Serial.print(event.acceleration.x);
  Serial.print(", Y: ");
  Serial.print(event.acceleration.y);
  Serial.print(", Z: ");
  Serial.print(event.acceleration.z);
  Serial.println(" m/s^2");
  delay(1000);
    display.clearDisplay();
    display.setCursor(0,0);
    display.println("ACCELERATION");
    display.print("[");
    display.print(millis());
    display.println("]");
    display.print("X: ");
    display.println(event.acceleration.x);
    display.print("Y: ");
    display.println(event.acceleration.y);   
    display.print("Z: ");
    display.print(event.acceleration.z);  
    display.print(" m/s^2");
    display.display();

    mpu.getGyroSensor()->getEvent(&event);
    Serial.print("[");
    Serial.print(millis());
    Serial.print("] X: ");
    Serial.print(event.gyro.x);
    Serial.print(", Y: ");
    Serial.print(event.gyro.y);
    Serial.print(", Z: ");
    Serial.print(event.gyro.z);
    Serial.println(" rad/s");
    delay(1000);
        display.clearDisplay();
        display.setCursor(0,0);
        display.println("GYROSCOPE");
        display.print("[");
        display.print(millis());
        display.println("]");
        display.print("X: ");
        display.println(event.gyro.x);
        display.print("Y: ");
        display.println(event.gyro.y);   
        display.print("Z: ");
        display.print(event.gyro.z);  
        display.print(" rad/s");
        display.display();

      mpu.getTemperatureSensor()->getEvent(&event);
  Serial.print("[");
  Serial.print(millis());
  Serial.print("] Temp: ");
  Serial.print(event.temperature);
  Serial.println("C");
  delay(1000);
       display.clearDisplay();
       display.setCursor(0,0);
       display.println("TEMPERATURE");
       display.print("[");
       display.print(millis());
       display.println("]");
       display.print("Temp: ");
       display.print(event.temperature);
       display.print(" C");
       display.display();

// program ultrasonik
  digitalWrite(trigPin, LOW); //untuk memberikan sinyal digital pada trigpin dengan nilai low
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);// Sets the trigPin on HIGH state for 10 micro seconds
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);  // Reads the echoPin, returns the sound wave travel time in microseconds
  distanceCm = duration * SOUND_SPEED/2;   // Calculate the distance
  distanceInch = distanceCm * CM_TO_INCH;  // Convert to inches
  Serial.print("Distance (cm): ");  // Prints the distance in the Serial Monitor
  Serial.println(distanceCm);
  Serial.print("Distance (inch): ");
  Serial.println(distanceInch);
  delay(1000);
      display.clearDisplay();
      display.setCursor(0, 0);
      display.print(" Nilai Jarak ");
      display.print(distanceCm);
      display.print(" cm");
      display.display();
  }