[SOLVED] Sequential variables and reading MAX6675

Hi All,

I'm trying to find a way to simplify the code that I have for reading multiple sensors, I'm not sure if what I'm trying to achieve can be done... or if I'm doing it the right way...

I have a variable (sensorsUsed) which I want to use to control a loop which can be set from any value between 0 (one sensor) or 9 (ten sensors)

I can do it through multiple if statements but I want to simplify the code somewhat making it easier to change at a later date, what I'm thinking is something like this: (my C skill is limited!!)

int sensorsUsed = 5;

void setup() {
  if (enableSerialMon){
    Serial.begin(19200);        // enable serial monitor if set in config
    }
}

void loop(){
  int sensor[sensorsUsed]{};
  int thermocouple[sensorsUsed]{};
  int i = 0;
  for (int i = 0; i < sensorsUsed; i++){
    sensor[i] = thermocouple[i].readCelsius();
    Serial.print("Sensor ");
    Serial.print([i]);
    Serial.print(" value is ");
    Serial.println(sensor[i]);
    delay(1000);
  }
}

now this comes up with an error that I don't understand -

request for member 'readCelsius' in 'thermocouple[i]', which is of non-class type 'int'

I just want sensor0 to be loaded with the read from thermocouple0.readCelsius()
then sensor1 to be loaded with the read from thermocouple1.readCelsius()
then sensor2 to be loaded with the read from thermocouple2.readCelsius()
etc.

All defined by the 'sensorsUsed' variable, the rest is just debugging to see if it is working or not.

Do I have the wrong understanding of arrays/loops/programming in general?? I can't help but thinking this is the right way to do it but not sure why it won't work...

now this comes up with an error that I don't understand - request for member 'readCelsius' in 'thermocouple', which is of non-class type 'int'

What don't you understand about that message?

You have an array, thermocouple, of type int. You clearly expect that array type to be something else, but only you know what type you really wanted it to be.

PaulS:
What don't you understand about that message?

You have an array, thermocouple, of type int. You clearly expect that array type to be something else, but only you know what type you really wanted it to be.

I think you may have pointed me in the right direction, if I replace the thermocouple reading with j it all runs completely fine, so I think that I'm heading in the right direction (commented out in the example below). As soon as i put the read back in it decides not to work...

int sensorsUsed = 2;
int runloop = 0;
int sensorStore = 0;
int i = 0;
//int j = 0;
  
void loop(){
int sensor[sensorsUsed]{};
int thermocouple[sensorsUsed]{};
    
  for (int i = 0; i < sensorsUsed; i++){
    sensor[i] = thermocouple[i].readCelsius();
    Serial.print("Sensor ");
    Serial.print(i);
    Serial.print(" value is ");
    Serial.println(sensor[i]);
    sensorStore += sensor[i];
    delay(1000);
    Serial.println(sensorTotal);
    //j = j+10;   
  }
  runloop++;
  Serial.print("loop : ");
  Serial.println(runloop);
}

looking at the library

I can't see what type the array should be... I've tried double and that doesn't work either

Sorry if it's something really dumb, this is the first time properly playing around with arrays

void loop();{
int sensor[sensorsUsed]{};
int thermocouple[sensorsUsed]{};

Why is there a ; after the parentheses of the loop() function?

Why are there curly braces between the ] and the ;?

PaulS:

void loop();{

int sensor[sensorsUsed]{};
int thermocouple[sensorsUsed]{};



Why is there a ; after the parentheses of the loop() function?

Why are there curly braces between the ] and the ;?

The first ; was an editing error when moving stuff around

The curly brackets are (from my understanding) the initialisation values for an array, if left blank {} it should load 0 into all elements of the array (defined by sensorsUsed)

Matra:
The first ; was an editing error when moving stuff around

The curly brackets are (from my understanding) the initialisation values for an array, if left blank {} it should load 0 into all elements of the array (defined by sensorsUsed)

By sticking it inside the main loop it also helps as it resets/clears the array once finished - saving me another line of code to clear it

The curly brackets are (from my understanding) the initialisation values for an array, if left blank {} it should load 0 into all elements of the array (defined by sensorsUsed)

Look at examples where curly braces are used. Notice the = sign between the brackets and the braces.

Local variables are NOT initialized if you omit the {}. Local variables are not initialized if the {} are empty.

PaulS:
Look at examples where curly braces are used. Notice the = sign between the brackets and the braces.

Local variables are NOT initialized if you omit the {}. Local variables are not initialized if the {} are empty.

In C yes but I thought arduino used C++ too?

C++ has universal initialisation for arrays which means that the = should not be required, as for empty {}, the initializer can even have no values, just the braces:

int arrayName [5] { };

This creates an array of five int values, each initialized with a value of zero. Or at least that's my understanding from this tutorial http://www.cplusplus.com/doc/tutorial/arrays/

That is not my issue now though as the arrays work and I can load data into them with no issues sequentially like I needed to...

My issue is now with the thermocouple[i].readCelsius(); function errors... I have tried declaring the array as every different type under the sun with no luck, I will try converting the array from char to a string to see if that works as I don't understand what type it needs to be from looking at the library.

It's just a name allocated to the sensor during setup which leads me to believe that it could just be read as a string...

EDIT: Tried converting to a string and get a similar error message, I've tried posting on the adafruit forums (as that's where the library was from) to see if they can shed some light on the situation

Where, on the cplusplus page, do you see an array declaration and initialization statement that does NOT have = in it?

Arrays are not initialized if there is not initialization list or if the list is empty, unless the array is global.

That is not my issue now though as the arrays work and I can load data into them with no issues sequentially like I needed to...

The code is wrong, even if the issues haven't (yet) bit you in the ass.

as that's where the library was from

What library?

PaulS:
Where, on the cplusplus page, do you see an array declaration and initialization statement that does NOT have = in it?

Arrays are not initialized if there is not initialization list or if the list is empty, unless the array is global.
The code is wrong, even if the issues haven't (yet) bit you in the ass.

"By default, regular arrays of local scope (for example, those declared within a function) are left uninitialized. This means that none of its elements are set to any particular value; their contents are undetermined at the point the array is declared.
But the elements in an array can be explicitly initialized to specific values when it is declared, by enclosing those initial values in braces {}. For example:

int foo [5] = { 16, 2, 77, 40, 12071 }; "

followed by

"The initializer can even have no values, just the braces:

int baz [5] = { };
This creates an array of five int values, each initialized with a value of zero: "

then followed by

"Finally, the evolution of C++ has led to the adoption of universal initialization also for arrays. Therefore, there is no longer need for the equal sign between the declaration and the initializer. Both these statements are equivalent:
1. int foo[] = { 10, 20, 30 };
2. int foo[] { 10, 20, 30 }; "

So by the logic described in the article, the [local] array is explicitly initialised and all values are zero... or did I completely misunderstand it??

I wanted to keep the array declaration local as it makes more sense with the code and utilised the 'for loop parameter' (i)

PaulS:
What library?

The one that I mentioned in my second post that is used for reading the sensor

"looking at the library
MAX6675-library/max6675.h at master · adafruit/MAX6675-library · GitHub
I can't see what type the array should be... I've tried double and that doesn't work either
Sorry if it's something really dumb, this is the first time properly playing around with arrays"

initially I thought it was an array problem (and probably still is) but now it's all based around the read celsius function. It doesn't like what 'type' of data that I seem to use :frowning:

I can't see what type the array should be

The type, obviously, should be MAX6675.THAT is the class that has the method you are trying to use.

Of course, creating an array of MAX6675 objects is not as simple as supplying an empty initializer list.

PaulS:
The type, obviously, should be MAX6675.THAT is the class that has the method you are trying to use.

Of course, creating an array of MAX6675 objects is not as simple as supplying an empty initializer list.

I don't understand... especially the 'obviously' bit...

Using the example included with the library and what information is available from other people's efforts the sensors are all setup before the setup() function

/** Set up MAX6675 device **/
int thermoDO = 2;               // DO/SO/MISO pin on MAX6675
int thermoCLK = 3;
int thermoCS0 = 4; 
int thermoCS1 = 5;
int thermoCS2 = 6;
int thermoCS3 = 7;
int thermoCS4 = 8;
int thermoCS5 = 9;
int thermoCS6 = 10;
int thermoCS7 = 11;
int thermoCS8 = 12;
int thermoCS9 = 13;
MAX6675 thermocouple0(thermoCLK, thermoCS0, thermoDO);
MAX6675 thermocouple1(thermoCLK, thermoCS1, thermoDO);
MAX6675 thermocouple2(thermoCLK, thermoCS2, thermoDO);
MAX6675 thermocouple3(thermoCLK, thermoCS3, thermoDO);
MAX6675 thermocouple4(thermoCLK, thermoCS4, thermoDO);
MAX6675 thermocouple5(thermoCLK, thermoCS5, thermoDO);
MAX6675 thermocouple6(thermoCLK, thermoCS6, thermoDO);
MAX6675 thermocouple7(thermoCLK, thermoCS7, thermoDO);
MAX6675 thermocouple8(thermoCLK, thermoCS8, thermoDO);
MAX6675 thermocouple9(thermoCLK, thermoCS9, thermoDO);

then to read a sensor in celsius

 sensor0 = thermocouple0.readCelsius();
    Serial.print("Sensor0 = "); 
    Serial.print(sensor0);

to read multiple sensors I found that by just polling the second sensor it would work (providing I had a delay after all readings before sampling the first sensor again - which is a requirement of the sensor anyway)

 sensor0 = thermocouple0.readCelsius();
    Serial.print("Sensor0 = "); 
    Serial.print(sensor0);
  
      sensor1 = thermocouple1.readCelsius();
      Serial.print("   Sensor1 = "); 
      Serial.print(sensor1);
    sensorAverage = (sensor0 + sensor1)/2;
    Serial.print("   Average = "); 
    Serial.println(sensorAverage);
    }


      sensorAverage = sensor0;
      Serial.print(" Temp Read = "); 
      Serial.println(sensorAverage);

What does work is:
(psuedo code)
sample sensor0
if sensorsUsed >=2
sample sensor1
if sensorsUsed >=3
sample sensor2
...
etc. (for all 10 sensors)

this is rather lengthy and inefficient (in my eyes) and I was trying to increment the value after 'sensor' and 'thermocouple' to do the readings within a for loop

Using my code from previous posts (yes... even without the = and nothing between {}) I can manually load data into sensors[i] through another variable and increment that within the loop, I can also read it back with no problems, I can see that thermocouple[i] also increments as expected BUT something doesn't like it when I do

thermocouple[i].readCelsius

that's where all the type errors are and that is the part that I need to fix - but I can't see how (and thus asked for help). Maybe my original explanations were not that clear...? My terminology may also not have been correct (still learning!). So apologies if there has been any confusion.

EDIT: dammit just realised that 'i' in [] causes everything to go italic so some of the previous posts are missing that bit :frowning: next time I'll use n as that won't break BBCode

also increments as expected BUT something doesn't like it when I do

You'll need to tell us WHAT doesn't like that code, and how it expresses it's dislike. You also need to post all of your code that generates the error.

You CAN create an array of MAX6675 objects, called thermocouple, so that is not the problem. The problem must be in HOW you do it.

PaulS:
You CAN create an array of MAX6675 objects, called thermocouple, so that is not the problem. The problem must be in HOW you do it.

Apparently this is the issue, I need to create an array of the same type as the individual variables used for the sensor (int)

so I just need to work out how to make an array of MAX6675 objects.

I've realised that the CS pins could be in an array

int CS[10] = {4,5,6,7,8,9,10,11,12,13};

MAX6675 thermocouple1(CLK, CS[1], DO);
MAX6675 thermocouple2(CLK, CS[2], DO);
MAX6675 thermocouple3(CLK, CS[3], DO);
MAX6675 thermocouple4(CLK, CS[4], DO);
MAX6675 thermocouple5(CLK, CS[5], DO);
MAX6675 thermocouple6(CLK, CS[6], DO);
MAX6675 thermocouple7(CLK, CS[7], DO);
MAX6675 thermocouple8(CLK, CS[8], DO);
MAX6675 thermocouple9(CLK, CS[9], DO);
MAX6675 thermocouple10(CLK, CS[10], DO);

I was then thinking that I could put all of the thermocouple names in an array but then that could potentially cause issues later down the line e.g.

int thermocouple[10] = {
  (CLK, CS[1], DO),
  (CLK, CS[2], DO),
  (CLK, CS[3], DO),
  (CLK, CS[4], DO),
  (CLK, CS[5], DO),
  (CLK, CS[6], DO),
  (CLK, CS[7], DO),
  (CLK, CS[8], DO),
  (CLK, CS[9], DO),
  (CLK, CS[10], DO)};

I probably have the complete wrong end of the stick here?? I found this which I don't know if it will be any help... not sure it's quite the same thing... http://www.cplusplus.com/doc/tutorial/structures/

beginning to regret not using the mass of if statements now :stuck_out_tongue:

I probably have the complete wrong end of the stick here?

You are close, but no cigar.

What is the type of thermocouple1? What is the type of thermocouple[0]? Why are they NOT the same type?

MAX6675 thermocouples[10] =
{
   MAX6675(CLK, CS[1], DO),
   MAX6675(CLK, CS[2], DO),
   MAX6675(CLK, CS[3], DO),
   MAX6675(CLK, CS[4], DO),
   MAX6675(CLK, CS[5], DO),
   MAX6675(CLK, CS[6], DO),
   MAX6675(CLK, CS[7], DO),
   MAX6675(CLK, CS[8], DO),
   MAX6675(CLK, CS[9], DO),
   MAX6675(CLK, CS[10], DO)
};

PaulS:
You are close, but no cigar.

What is the type of thermocouple1? What is the type of thermocouple[0]? Why are they NOT the same type?

MAX6675 thermocouples[10] =

{
  MAX6675(CLK, CS[1], DO),
  MAX6675(CLK, CS[2], DO),
  MAX6675(CLK, CS[3], DO),
  MAX6675(CLK, CS[4], DO),
  MAX6675(CLK, CS[5], DO),
  MAX6675(CLK, CS[6], DO),
  MAX6675(CLK, CS[7], DO),
  MAX6675(CLK, CS[8], DO),
  MAX6675(CLK, CS[9], DO),
  MAX6675(CLK, CS[10], DO)
};

That code makes sense, thanks for your help so far, it's very much appreciated and I see where I went wrong on that.

By creating that array everything now compiles ok (which is a HUGE improvement) so I will dig out the sensors and plug them in and see if they are read/addressed correctly

Well, there is one problem. Array indices start at 0, so that snippet should be using CS[ 0 ] to CS[ 9 ], not CS[ 1 ] to CS[ 10 ];

Took a while to write this post as I had another error but I actually fixed it before I finished typing it all!

It actually makes complete sense now and I understand where I went wrong.

Finally got there and sorry if my question asking skills were a bit rubbish and thanks for sticking with me! I now have a better understanding of arrays.
:slight_smile:

have some karma from me, will add another one later when I can for your help yesterday too. That was the 'hard bit' of my code done now, the rest is just tidy-up and well within my skill level.