// This file shows an example implementation of a low-pass filter on an Arduino.
// Note that there are many potential improvements to this code.

// Sellen key topology LPF
// Muhammad Alditya Azizi
// 22/492752/TK/53943
float xn1 = 0;
float yn1 = 0;
float yn2 = 0;
int k = 0;
const float omega0 = 1000; //10Hz
const float Ts = 0.001; //1 ksps
const float myExp = pow(2.7183,-omega0*Ts);
const float myExp2 = pow(2.7183,-2*omega0*Ts);
void setup() {
  Serial.begin(115200);
}

void loop() {
  // Test signal
  float t = micros()/1.0e6;
  float xn = sin(2*PI*2*t) + 0.2*sin(2*PI*50*t);

  // Compute the filtered signal
  //float yn = myExp*yn1 + omega0*xn;
  float yn = 1000000*Ts*myExp*xn1 + 2*myExp*yn1-myExp2*yn2;
  float myYn = Ts*yn;

  delay(1);//iterasi berikutnya tertunda 1ms
  xn1 = xn;
  yn2 = yn1;
  yn1 = yn;//geser sinyal

  if(k % 3 == 0){
    // This extra conditional statement is here to reduce
    // the number of times the data is sent through the serial port
    // because sending data through the serial port
    // messes with the sampling frequency
  
    // Output
    Serial.print(xn);
    Serial.print(" ");
    Serial.println(myYn);
  }
  k = k+1;
}