My code is able to recalibrate my photocell every 5 minutes but I want it to take 5 sec to recalibrate each time not instantly because that way the reading is more reliable. How do I do that?
void loop()
{
const unsigned long fiveMinutes = 5 * 60 * 1000UL;
static unsigned long lastSampleTime = 0 - fiveMinutes; // initialize such that a reading is due the first time through loop()
unsigned long now = millis();
if (now - lastSampleTime >= fiveMinutes)
{
Serial.println("recalibrate");
lastSampleTime += fiveMinutes;
// record the maximum sensor value
if (sensorValue > sensorMax) {
sensorMax = sensorValue;
}
// record the minimum sensor value
if (sensorValue < sensorMin) {
sensorMin = sensorValue;
}
}
teresay5g:
My code is able to recalibrate my photocell every 5 minutes but I want it to take 5 sec to recalibrate each time not instantly because that way the reading is more reliable. How do I do that?
I suspect I don't understand what you want to do and the code you posted does not make any attempt at it which would have given me some guidance.
There is no reason why you cannot change the timing interval between the value in fiveMinutes and fiveSeconds on every alternative run through. Something like this
if (now - lastSampleTime >= interval ) {
if (interval == fiveMinutes) {
interval = fiveSeconds;
}
else {
interval = fiveMinutes;
}
// rest of the stuff
By the way, this can't work
static unsigned long lastSampleTime = 0 - fiveMinutes;
because an unsigned long cannot have a negative value.
I would leave the variables as unsigned long and perform the first calibration as the last thing in setup()