Variable difference between setup and loop

Hi,

I'm getting a bit mad with the issue I have at the moment : on the code below the minVal doesn't want to be anything except 0 !

I have introduced a test at the beginning of the loop to check the first run fo the loop and here is the result :

15:36:43.946 -> 24 24 24 24 24 24 24 24
15:36:43.979 -> Min value : 24
15:36:48.952 -> Min value : 0

So something is happening between the end of the setup and the very beginning of the loop.

Looking for more than an hour now and no clue what is it :frowning:

Can you have a fresh look and let me know what I have done wrong ?



#include <Adafruit_BME280.h>
#define adresseI2CduBME280                0x76          
Adafruit_BME280 bme;

unsigned long action2 = 0;
unsigned long t = 0;
int myArray[8] = {};
int maxVal ;
int minVal ;
int index = 0 ;


/*------------------------------------ Setup ------------------------------------*/
void setup() 
{

  Serial.begin(9600);
  bme.begin(adresseI2CduBME280);

  int maxVal = bme.readTemperature(); // Set up values for the start 
  int minVal = bme.readTemperature();

  for (int i = 0; i < 8; i++) 
  {
  myArray[i] = bme.readTemperature(); // Set all values of the array for the start 
  }

  for (int i = 0; i < 8; i++) 
  {
  Serial.print(myArray[i]);Serial.print(" "); // Check all is well in the array
  }

  Serial.println();
  Serial.print("Min value : "); Serial.println(minVal); // Check if the value is indeed the current temperature 

delay(5000);

}

/*------------------------------------ Loop ------------------------------------*/
void loop() 
{

  Serial.print("Min value : "); Serial.println(minVal); // Check if there is a difference between loop and setup 
  delay(2000);

  t = millis();

   for (int i = 0; i < 8 ; i++)  // Test and attribute min & max 
   {
      if (myArray[i] > maxVal) 
      {
         maxVal = myArray[i];
         Serial.println("salut");
      }
      
      if (myArray[i] < minVal) 
      {
         minVal = myArray[i];
         Serial.println("coucou");
      }
   }


    if ( ( t - action2 ) > 2000 ) // Update value of array every 2 secondes 
    {

    action2 = t;

      index = index+1 ;
      if (index > 8) {index=1;}

      myArray[index] = bme.readTemperature();

        
      Serial.print("Max value is : "); Serial.println(maxVal);
      Serial.print("Min value is : "); Serial.println(minVal);
      Serial.println(index);

        
    }


}

You are defining new LOCAL versions of maxVal and minVal in setup(). loop() is using the GLOBAL variables of the same name and those are initialized to zero.

these lines in setup() create local variables with same names as global variables used in loop.

delete the "int" preceding each variable

:see_no_evil:

Thank you very much for your quick answer :slight_smile:

You have 2 completely different variable named minVal in your sketch

One has global scope and its value will initially be zero. The other one's scope is the setup() function and is assigned the value returned by bme.readTemperature()

Later, in loop() you print minVal but it is the global variable that you are printing, hence its value is zero

Delete the variable type from

  int minVal = bme.readTemperature();

and your code will use the global variable throughout the sketch

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.