[RESOLVED]Question with a millis() Timer

Hey everyone, I have a quick question as to why my delaying function isn't working. I have isolated just the relevant part.

const long interval2 = 5000; //5 second update interval
unsigned long previousMillis2 = 0; //last time updated

unsigned long currentMillis2 = millis(); //get the current millis count

if (currentMillis2 - previousMillis2 >= interval2){ 
    previousMillis2 = currentMillis2; //last update == this update
    dataRx.RxTemperature = dht.readTemperature(); // very slow temperature update, need to only update when absolutley needed
    Serial.println(dataRx.RxTemperature); //print the temperature
  }

This code is for a project which involves writing a pwm signal to an esc, so I want it to be as fast as possible so the throttle response is smooth, so I want to update the temperature every 5 seconds. However, with this code, when I open the serial monitor it just spews out the temperature as fast as it can, which is not what I want, it should be every 5 seconds. I'm not sure what i'm missing, Thanks.

If this is all in one function, you need to make previousMillis2 a static variable.

Because you only post a snippet, we all have to make assumptions how it all fits together. Lesson learned, don't post snippets :wink:

In which part of the code are the interval2, currentMillis2, previousMillis2 declared.
where is the code you are showing? in an interrupt? in the main loop?
what else could mess with previousMillis2 or interval2? are you overflowing buffers somewhere...

very often what you think is relevant is not... :slight_smile: would be good to see the full code...

just side note, for being safe you should declare

const unsigned long interval2 = 5000UL; //5 second update interval

Ah ok! Yeah the full code is quite messy, but sure here it is:
I defined previousMillis2 and interval2 inside the loop function where they are used, because from what I was taught, Global variables are the devil!

#include <SPI.h>
#include <Wire.h>
#include "RF24.h";
#include "Sodaq_DS3231.h"
#include "DHT.h"
DHT dht(9, DHT11);
RF24 radio(7,8);
const uint64_t pipe[1] = {0x3259657830};

struct packageTx
{
 int TxSlider = 0;
};

struct packageRx
{
 char RxTime[10];
 int RxVoltage = 0;
 int RxTemperature = 0;
};

typedef struct packageTx PackageTx;
PackageTx dataTx;

typedef struct packageRx PackageRx;
PackageRx dataRx;

int fadeVal = 0;//used to fadeled as placeholder for motor

void setup() {
  
  Serial.begin(115200);
  pinMode(3,OUTPUT);
  Wire.begin();
  dht.begin();
  rtc.begin();
  radio.begin();
  radio.setRetries(15, 5);
  radio.setChannel(115);
  radio.setPALevel(RF24_PA_LOW);
  radio.setDataRate(RF24_250KBPS);
  radio.enableAckPayload();           
  radio.enableDynamicPayloads(); 
  radio.openReadingPipe(1,pipe[0]);
  radio.startListening();
  getTime(); 
  dataRx.RxTemperature = dht.readTemperature();
  delay(1000);
  radio.writeAckPayload(1,&dataRx,sizeof(dataRx));
}


void loop() {
 
  const long interval = 1000; //1 second interval to update time
  const long interval2 = 5000; //5 second update interval
  unsigned long previousMillis = 0; 
  unsigned long previousMillis2 = 0; //last time updated
  
  unsigned long currentMillis = millis();
  
  if (currentMillis - previousMillis >= interval){
    previousMillis = currentMillis;
    
    dataRx.RxVoltage = analogRead(7);
    getTime();  
  }
  
  unsigned long currentMillis2 = millis(); //get the current millis count
  
  if (currentMillis2 - previousMillis2 >= interval2){ 
    previousMillis2 = currentMillis2; //last update == this update
    dataRx.RxTemperature = dht.readTemperature(); // very slow temperature update, need to only update when absolutley needed
    Serial.println(dataRx.RxTemperature); //print the temperature
  }
  
  
  while(radio.available()){              
      radio.read(&dataTx,sizeof(dataTx));                                    
      radio.writeAckPayload(1,&dataRx,sizeof(dataRx));   
   }
  writeToEsc();
}

void getTime() { 
  
  String hString = "";
  String mString ="";
  String Time = "";
  int hInt = 0;
  int mInt = 0;
  int hNewFormat = 0;
  String meridian = "";
  
  DateTime now = rtc.now(); //get the current date-time 
  hInt = now.hour();
  mInt = now.minute();
  hString = String(hInt);
  mString = String(mInt);
  
  if (hInt > 12) {
    hNewFormat = hInt - 12;  
  }
  else {
    hNewFormat = hInt;
  } 
  if (hInt < 10) {
    hString = ("0" + String(hNewFormat));
  }
  if (mInt < 10) {
    mString = ("0" + String(mInt));
  }
  if (hInt <= 12) {
    meridian = "AM";
  }
  else {
    meridian = "PM";
  }
  Time = (hString + ":" + mString + " " + meridian);
  Time.toCharArray(dataRx.RxTime,10);
   
}

void writeToEsc(){

  fadeVal = map(dataTx.TxSlider,0,1023,0,255);
  analogWrite(3,fadeVal);
}

So make them static.

Yes Global variables are not great but they are not reset at each loop :slight_smile:

either make them global or use the keyword static to ensure they are not reset

sterretje:
So make them static.

Aha! There we go, I see. I tried making them "const" from what you said before becuase I thought static was the same thing as "const", but they clearly aren't. Yeah I didn't realise that local variables were reset each time through the loop, thanks both of you guys for the help!

Always good to learn something new! we have all been there (and sometime also forget about it)