SORTED!!! Recall Millis() outside the void loop

Hi all,

Does millis() work when is called outside the void loop function? Trying to print a couple of variables at the end of the period it but does not print.

I defined the variable "unsigned long currentTime = 0;" at the beginning.

On the loop, the "currentTime = millis()" and I call a function "NewDay();"

The "NewDay()" uses the millis to calculate the intervals, at the same moment to print the variables but those does not print.

Code below...
Thnks

int Day = 0;     // new day will increase the counter
const long DayMillis = 10000; // millis in 1 day. 1,000 millis = 1 sec, 60 secs = 1min, 1hour=60min, 1day=24hours
unsigned long lastDay = 0 ;  // will store the millis that lastDay was updated
const float TempMax = 42;   // Temperatura Maxima water
float maxTemp = 4;          // maxima temperatura of the day, will reset each day
float Temp = 1;
// unsigned long currentTime = millis() , esta variable ira en el loop para resetear maxTemp cuando millis (no se resetea, va contando hasta 49,71 dias) alcance Day valor
unsigned long currentTime = 0;
int LED = D4;
int LEDstate = HIGH;

const long interval = 1000;  // Millis at which the LED will blink
unsigned long LEDTime = 0;   // variable to for the LED function
unsigned long LEDprevious = 0; // will store last time the LED was updated



void setup() {
  Serial.begin(9600);
  pinMode(LED, OUTPUT);
  digitalWrite(LED, HIGH);

}

void loop() {
  unsigned long currentTime = millis();
  //  unsigned long LEDTime = millis();
  NewDay();

  //  if (Serial.available() > 0) { // Serial monitor Temperature entry
  //Temp = Serial.read();
  //    Serial.print("Temp");
  //Serial.println(Temp);
  //  }
  //  if (Temp > maxTemp) {        // records new maxTemp
  //    maxTemp = Temp;
  //  }

  //  if (maxTemp <= TempMax) {
  //    actionLED();
  //  }
}

void NewDay() {                            // funcion to reset all timers: Day, maxTemp
  if (currentTime - lastDay >= DayMillis) {
    lastDay = millis();                    // lastDay toma el valor de millis actuales
    maxTemp = 0;                           // maxTemp se resetea
    Day = ++Day;                           // contador de dias
    Serial.print("last Day...");
    Serial.println(lastDay);
    Serial.print("maxTemp...");
    Serial.println(maxTemp);
    Serial.print("Day...");
    Serial.println(Day);
    Serial.print("Temp...");
    Serial.println(Temp);
  }
}
/*/
  void actionLED() {                                 //still unfinished
  if (LEDTime - LEDprevious >= interval) {
    LEDprevious = LEDTime;
    LEDstate = !LEDstate;
  }

  }/*/
void loop() {
  unsigned long currentTime = millis();

Local variable masks global.

Try

void loop() {
  currentTime = millis();

Excellent, it worked... Thnks!!!

Does this mean that I was defining the same variable twice? and this conflicted the system on where to store this value?

What is strange that the compilation did not give any errors on this?!

It isn't an error. It is perfectly allowable to have variables with the same name but different scopes. Scope is an important concept in programming. It's up to you to keep track of which one you're using at any time.

Steve

Does this mean that I was defining the same variable twice?

You were defining two variables with the same name, in different memory locations, with differing scope. That is not the same thing as defining the same variable twice, so that is not what you were doing. That isn't even possible.

and this conflicted the system on where to store this value?

No, the Arduino was not conflicted. It knew exactly what you did, and exactly where to store the data. That it was not where you expected caused you conflict.