#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
#include<LiquidCrystal.h>
LiquidCrystal lcd(33,26,27,14,12,13);

Adafruit_MPU6050 mpu;

#define samples 50
#define maxVal 10 // max change limit
#define minVal -10 // min change limit
#define pinBuzzer 25

int xsample = 0;
int ysample = 0;
int zsample = 0;
int buzzer  = 1;
int xValue, yValue, zValue, value1, value2, value3;

sensors_event_t event;

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

  while (!mpu.begin()) {
    Serial.println("Alat Deteksi Gempa Siap connected!");
    delay(1000);
  }
  Serial.println("Alat Deteksi Gempa Siap !");
  Serial.println("=========================");
  Serial.println("");

  mpu.getAccelerometerSensor()->getEvent(&event);
  pinMode(pinBuzzer, OUTPUT);
  tone(pinBuzzer, 10);
  noTone(pinBuzzer);

  for(int i=0; i < samples; i++) {
  xsample+=event.acceleration.x;
  ysample+=event.acceleration.y;
  zsample+=event.acceleration.z;
  }
 
  xsample/=samples; // taking avg for x
  ysample/=samples; // taking avg for y
  zsample/=samples; // taking avg for z

  startLCD2();

}

void soundBuzzer(){
  tone(pinBuzzer, 100);
  buzzer = 0;
}

void stopBuzzer(){
  buzzer = 0;
  if (buzzer == 0){
    noTone(pinBuzzer);
  }
}

void startLCD1() {
  lcd.begin(16,2);
  delay(1000);
  lcd.print("Pendeteksi ");
  lcd.setCursor(0,1);
  lcd.print("Gempa ");
  delay(2000);
  lcd.clear();
  lcd.print("Calibrating.....");
  lcd.setCursor(0,1);
  lcd.print("Please wait...");
}

void startLCD2(){
  delay(3000);
  lcd.clear();
  lcd.print("Calibrated");
  delay(1000);
  lcd.clear();
  lcd.print("Device Ready");
  delay(1000);
  lcd.clear();
  lcd.print(" X Y Z ");
}

void WriteLCD(){
  lcd.setCursor(0,1);
  lcd.print(value1);
  lcd.setCursor(6,1);
  lcd.print(value2);
  lcd.setCursor(12,1);
  lcd.print(value3);
  delay(100);
}

void WriteGempa(){
  Serial.print("[");
  Serial.print(millis());
  Serial.print("]");
  Serial.print(" GEMPA LUURRR !!!");
  Serial.print(", X: ");
  Serial.print(value1);
  Serial.print(", Y: ");
  Serial.print(value2);
  Serial.print(", Z: ");
  Serial.print(value3);
  Serial.println(" m/s^2");
}

void WriteAman(){
  Serial.print("[");
  Serial.print(millis());
  Serial.print("]");
  Serial.print(" Aman BOS !!!");
  Serial.print(", X: ");
  Serial.print(value1);
  Serial.print(", Y: ");
  Serial.print(value2);
  Serial.print(", Z: ");
  Serial.print(value3);
  Serial.println(" m/s^2");
}

void loop() {

  mpu.getAccelerometerSensor()->getEvent(&event);

  value1 = event.acceleration.x; // reading x out
  value2 = event.acceleration.y; // reading y out
  value3 = event.acceleration.z; // reading z out
  
  xValue = xsample-value1; // finding change in x
  yValue = ysample-value2; // finding change in y
  zValue = zsample-value3; // finding change in z

  if(xValue < minVal || xValue > maxVal || yValue < minVal || yValue > maxVal || zValue < minVal || zValue > maxVal) {
    WriteGempa();
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Peringatan Gempa");
    WriteLCD();
    soundBuzzer();
  } else {
    WriteAman();
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Deteksi Gempa ");
    WriteLCD();
    stopBuzzer();
  }
  delay(1000);

}