// https://forum.arduino.cc/t/turn-on-peristaltic-pump-based-on-ph-reading/1143658
// https://wokwi.com/projects/379655181669026817
# define sampleRate 66 // sample every N milliseconds
// for the leaky integrator
const float alpha = 0.75;
float leakyAverage;
void setup()
{
Serial.begin(115200);
Serial.println("leaky integrator demo\n");
}
const int analogInPin = A0;
int temp;
unsigned long now;
void loop()
{
now = millis();
static unsigned long lastSampleTime;
// grab a sample only every sampleRate milliseconds
if (now - lastSampleTime < sampleRate) return;
lastSampleTime = now;
int sensorValue = analogRead(analogInPin);
float newReading = sensorValue * 5.0 / 1024;
leakyAverage = alpha * leakyAverage + (1 - alpha) * newReading;
Serial.print(0.0);
Serial.print(", ");
Serial.print(newReading);
Serial.print(", ");
Serial.print(leakyAverage);
Serial.print(", ");
Serial.print(5.0);
Serial.println();
}