#define POTENTIOMETER_PIN 12 // ESP32 pin GIOP36 (ADC0) connected to Potentiometer pin
#define LED_PIN           21 // ESP32 pin GIOP21 connected to LED's pin
#define ANALOG_THRESHOLD  1000

#include <bits/stdc++.h>
using namespace std;

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

// Function to find the product of y (sun radiation)
float countY(float y[])
{
    float total = 0;
    for (int x = 0; x < 12; x++) {
        total = total + y[x];
    }
    return total;
}
 
// Function to find the product of ycos (sun radiation)

float Ycos (float y[], float t[])
{
    float total = 0;
    for (int x = 0; x < 12; x++) {
        total = total + (y[x]*cos(t[x]*(M_PI/180)));
    }
    return total;
} 

// Function to find the product of ysin (sun radiation)
float Ysin (float y[], float t[])
{
    float total = 0;
    for (int x = 0; x < 12; x++) {
        total = total + (y[x]*sin(t[x]*(M_PI/180)));
    }
    return total;
}

  float t[] = {0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330};
  float y[] = {144, 188, 245, 311, 351, 359, 308, 287, 260, 211, 159, 131};    

void setup() {
  Wire.begin(18, 19);
  lcd.init();
  lcd.backlight();
  lcd.setCursor(1, 0);
  lcd.print("Tugas Komnum");
  lcd.setCursor(2, 1);
  lcd.print("Kelompok 2");
  delay(2000);
  lcd.clear();
  pinMode(LED_PIN, OUTPUT); // set ESP32 pin to output mode
  Serial.begin(115200);
}

void loop() {

  int analogValue = analogRead(POTENTIOMETER_PIN); // read the input on analog pin
  {
    
    // number of inputs given
    int n = 12;
    //int data = 225;                
    int data = (analogValue/11.375);
    float A0 = countY(y)/n;
    float A1 = 2*Ycos(y,t)/n;
    float A2 = 2*Ysin(y,t)/n;
    float yhasil= A0 + A1*cos(data*(M_PI/180)) + A2*sin(data*(M_PI/180));

    // printing the value
    cout << "\ny = " << A0 << " + " << A1 << "cos(" << (data*(M_PI/180)) << ") + " 
              << A2 << "sin(" << (data*(M_PI/180)) << ")";
    cout << "\nNilai A0 yang didapat adalah "
               << A0 << endl;
    cout << "Nilai A1 yang didapat adalah "
               << A1 << endl;
    cout << "Nilai A2 yang didapat adalah "
               << A2 << endl;
    cout << "Prediksi di hari ke-" << data << " adalah "
               << yhasil << "\n" << endl;
    lcd.setCursor(0, 0);
    lcd.print("Hari : ");
    lcd.setCursor(6, 0);
    lcd.print(data,0);
    lcd.setCursor(0, 1);
    lcd.print("Pred : ");
    lcd.setCursor(2, 1);
    //lcd.print(data,0);
    lcd.setCursor(4, 1);
    lcd.print(" : ");
    lcd.setCursor(6, 1);
    lcd.print(yhasil,3);} 

    delay(1000);
  }