const int updateInterval = 10000; // Time between boundary updates in milliseconds
unsigned long timeOfUpdate = 0; // Timestamp for when the boundary update happened
byte sensorOutput;
byte boundaryLow = 131;
byte boundaryHigh = 30;
byte boundaryPeakToPeak;
void setup()
{
Serial.begin(9600);
randomSeed(analogRead(0));
}
void loop()
{
sensorOutput = random(30, 131); // To fake the sensor's min/max output values
Serial.print(sensorOutput); Serial.print(" ");
//scaled = constrain(map(sensorOutput, boundaryLow, boundaryHigh, 0, 23), 0, 23);
updateBoundaries();
delay(300); // Just so 30 and 130 don't occur almost instantly
}
void updateBoundaries()
{
boundaryLow = min(sensorOutput, boundaryLow);
boundaryHigh = max(sensorOutput, boundaryHigh);
Serial.print(boundaryLow); Serial.print(" ");
Serial.println(boundaryHigh); //Serial.print(" ");
boundaryPeakToPeak = boundaryHigh - boundaryLow;
//Serial.println(boundaryPeakToPeak);
// Is it time to update the boundary values?
if (millis() - timeOfUpdate >= updateInterval)
{
boundaryLow = 131;
boundaryHigh = 30;
// Update the timestamp with the time when the boundary update happened
timeOfUpdate = millis();
}
}