// Tugas UTS
// Almas Gilang Ramadhan
//21410300489
float datax[13] = {953,841,802,713,644,508,420,368,308,263,205,200,115 }; // contoh data x
float dataY[13] = {3.93,5.29,8.55,21.28,32.28,39.56,48.70,52.45,57.18,50.9,59.7,57.51,64.45};  // contoh data y
float mx[1];//ukuran untuk memori regresi dari nilai X
float my[1];// ukuran hasil nilai Y
float adc;
float Temperature;
float slope;
float intercept;

const int analogPin = A0;  // Analog pin ke temperature sensor
void setup() {
  Serial.begin(9600);
  pinMode(A0,INPUT);
  float sumX = 0, sumY = 0, sumXY = 0, sumX2 = 0;// nilai untuk data X & Y

  for (int i = 0; i < 13; i++) {
    sumX += datax[i];
    sumY += dataY[i];
    sumXY += datax[i] * dataY[i];
    sumX2 += datax[i] * datax[i];
  }
  float slope = (13 * sumXY - sumX * sumY) / (13 * sumX2 - sumX * sumX);//perhitungan slope
  float intercept = (sumY - slope * sumX) / 13;//perhitungan intercept
  Serial.print("Slope: ");
  Serial.println(slope);// display slope
  Serial.print("Intercept: ");
  Serial.println(intercept);// display intercept
  mx[0]=slope;//  data perhitungan slope ke dalam mx
  my[0]=intercept;// data perhitungan intercept ke dalam my
}

void loop() {
  adc = analogRead(A0);
  Temperature = adc *mx[0] + my[0];
  Serial.print(adc);
  Serial.print(" ,");
  Serial.print("Temperature =");
  Serial.println(Temperature);
  delay(500);
}