Arduino temperature sensor sampling in every 5 minutes

Hello everyone.

I work with an Arduino Uno ATmega 328P, and a temperature sensor LM335, in my project.
I would like to build a thermostat controller, now i have a simple function for sampling temperature avreage from sensor( param1 is sampling number, param2 is analog input pin )

void loop()
{
otherOperationFUN3(aa,aa);

  Serial.printn( getTempAvreageInCelsius( SAMPLING_NB, SENSOR_PIN ) ) ;// running function with millis() delay
 
 otherOperation1FUNC(aa,aa);

otherOperation2FUNCTION(aa,ah);

}

float getTempAvrageInCelsius(int count, int pin) {

//delay with millis() comes here//

  float temperaturaMediata = 0;
  float sumaTemperatura;

  for (int i =0; i<count; i++) {
    int reading = analogRead(pin);
 
 
    float temperatureCelsius = (5.0 * reading * 100.0)/1024.0;
    sumaTemperatura = sumaTemperatura + temperatureCelsius;
  }
  

//end delay with millis() //

  return (float)(sumaTemperatura / (float)count);

}

I would like to sampling from sensor in every 5 minutes, without using the delay() function, and i want to solve this with millis() .
Is the millis() also suspend the Arduino board( the loop function) like delay() ?
Can Arduino board performing calculations, while the millis() running in the background?

I am sorry for my English.

You don't really have to use the millis() function. Just AnalogRead() a pin every delay(time). The millis() function is useful when you want to get the time that has elapsed, like for example how long did it take for the temperature to rise from 14 to 25 degrees. Instead you want to get readings every 5 minutes so use delay() function.

void loop() {
  int val = analogRead(14);
  Serial.print("Sensor One: ");
  Serial.println (val);   
  delay(1000); // delay not millis()
}

For (temporally) Storing previous readings you could use the array function that write the newest temperature reading into the first position and moves the rest of the readings to the next array position. Such as this example:

for (int i = array.length-1; i > 0; i--) {
  array[i] = array[i-1];
  }

Then you could take all the variables and round them.
Note: this is from my Processing code, the .length attribute i think isn't supported in Arduino IDE, but I think you get the idea :slight_smile:

Bad advice IMHO
The OP may not want to do anything else except sample a temperature every 5 minutes in this program but he will at some time so it is better to embrace the use of use millis() from the outset.

Using millis() as a timer to trigger events does not block the operation of other functions as long as they themselves are non blocking.

Yes, you can do that. Here is an outline of the code you need:

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)
  {
     lastSampleTime += fiveMinutes;
     // add code to take temperature reading here
  }
  // add code to do other stuff here
}
1 Like

Thank you, it works great :slight_smile:

I like to do it like this:

#define FIVEMIN (1000UL * 60 * 5)
unsigned long rolltime = millis() + FIVEMIN;
void loop() {
  
  // signed comparison for proper handling of timer rollover
  
  if((long)(millis() - rolltime) >= 0) {

    //  Do your five minute roll stuff here

    rolltime += FIVEMIN;

  }
  
}

Works great. I needed this too. Thanks.

hello all

i wanted to ask a question related to this 5 min delay thing im trying this same this which is mentioned in last comments of the page but its not working can any one tell me where im making a mistake thank you

nomi111222:
can any one tell me where im making a mistake thank you

I see two mistakes:

  1. Hijacking someone else's (really old) thread.

  2. Not posting (in code tags) the code that is causing you problems.

its not working

Please post "it"

Have you read and understood Using millis() for timing. A beginners guide ?