// 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;
// goforsmoke
typedef float gfsType;
int avgRead;
{
const unsigned int averageOver = 10;
static gfsType runningTotal;
gfsType avgRead;
// adcRead = analogRead( potPin[ potIdx ] );
int adcRead = sensorValue;
// subtract 1/averageOver of the total and add the read
runningTotal -= runningTotal / averageOver + adcRead;
avgRead = runningTotal / averageOver;
}
// Serial.print(0.0);
Serial.print(", ");
Serial.print(newReading);
Serial.print(", ");
// Serial.print(leakyAverage);
// Serial.print(", ");
Serial.print(avgRead);
Serial.print(", ");
// Serial.print(5.0);
Serial.println();
}
// "serialMonitor": { "convertEol": false, "display": "plotter", "newline": "lf" },