Notifications
Clear all

Trouble with Loops and Variables

7 Posts
3 Users
1 Likes
1,593 Views
Tigs62
(@tigs62)
Member
Joined: 3 years ago
Posts: 20
Topic starter  

Hi Guys,

First of all, I am sorry if this post is in the wrong subsection.  I AM using an ESP32 in this project, but this question is more about the Arduino IDE code.

I have had a pretty successful day today, but when I decided to "clean up" my code and make it look a bit neater, it all went wrong.  Below are my code snippets.  It is not the whole code, as it is getting pretty long now, and for the most part, it is still working 🙂 

I wont bore you with the code that sets up all the libraries etc.

Because my ESP32 is getting temperature readings from 5 x DS18B20 Temp sensors, I have to declare the special variables as "Floating Point" variables.

// Variables to hold temperature readings
float tempSensor1;
float tempSensor2;
float tempSensor3;
float tempSensor4;
float tempSensor5;

 

Then I have to declare the addresses of the actual sensors, because they are on a OneWire Bus:

DeviceAddress sensor1 = { 0x28, 0x84, 0x71, 0x79, 0x97, 0x5, 0x3, 0xFC };
DeviceAddress sensor2 = { 0x28, 0x54, 0x47, 0x79, 0x97, 0x5, 0x3, 0x20 };
DeviceAddress sensor3 = { 0x28, 0x16, 0x75, 0x79, 0x97, 0x8, 0x3, 0xB5 };
DeviceAddress sensor4 = { 0x28, 0x9F, 0x18, 0x79, 0x97, 0x7, 0x3, 0x2C };
DeviceAddress sensor5 = { 0x28, 0x9F, 0xF1, 0x79, 0x97, 0x5, 0x3, 0x94 };

 

The next bit is where the variables I created earlier are "filled" with the readings from the actual sensors.

//---------------------------------
// Update the temperature variables
// This is getting the temperature readings from 5 x DS18B20 Temp sensors
tempSensor1 = sensors.getTempC(sensor1);
tempSensor2 = sensors.getTempC(sensor2);
tempSensor3 = sensors.getTempC(sensor3);
tempSensor4 = sensors.getTempC(sensor4);
tempSensor5 = sensors.getTempC(sensor5);

 

Then, I wanted to view the results on the Serial Monitor.  So, initially I slowly built up the readings one at a time until I could see all five readings.  In "long-hand" this worked and I was very chuffed.

//---------------------------------
// Print the temperature sensor readings on the Serial Monitor
// This way of doing it, DOES work.
Serial.print("Sensor 1: ");
Serial.println(tempSensor1);

Serial.print("Sensor 2: ");
Serial.println(tempSensor2);

Serial.print("Sensor 3: ");
Serial.println(tempSensor3);

Serial.print("Sensor 4: ");
Serial.println(tempSensor4);

Serial.print("Sensor 5: ");
Serial.println(tempSensor5);

 

After a bit of tinkering with the void loop and the milli function to call these readings every 5 minutes (which also worked - ON A ROLL!).  I decided that although it worked, i could probably reduce my code down a bit if I could write my readings in a loop.

This is where I came unstuck, because my knowledge was obviously lacking.
Below is the bit of code that I tried to use instead of the longhand method.

//---------------------------------
while (SensorLoop < 5) {
     SensorLoop = SensorLoop + 1;
     Serial.print("Sensor ");
     Serial.print(SensorLoop);
     Serial.print(": ");
     Serial.println(tempSensor(SensorLoop));
}
SensorLoop = 1

 

What i was hoping to see in the Serial Monitor window was something like this:

Sensor 1: 22.50
Sensor 2: 22.67
Sensor 3: 22.56
Sensor 4: 22.36
Sensor 5: 22.75

But my code just doesn't want to work.  The error message that appears is this:
'tempSensor' was not declared in this scope

I know that the issue is because I have tried to replace the tempSensor variables number with the number that is the loop, but I don't know the proper way to do it.
I have the feeling that this is fundamental coding, but I am lacking in the knowledge and need a little prod in the right direction please.

Chris

You are never too old to learn.
You are never too old to teach.


   
Quote
(@larry-manson)
Member
Joined: 3 years ago
Posts: 18
 

You are not declaring tempSensor as an array.

you need something like 

float tempSensor(6) // Note the first element in an array is always 0.

You will also have to change your code where you take the temperature readings either with a loop  or by tempSensor(1), tempSensor(2), ....

If you used 0 for the first tempSensor you could declare it to have only 5 elements.

 

https://www.arduino.cc/reference/en/language/variables/data-types/array/

 

Hope this helps,

 

Larry

 


   
Tigs62 reacted
ReplyQuote
(@davee)
Member
Joined: 3 years ago
Posts: 1547
 

Hi Chris,   @tigs62

Also, do you set SensorLoop to an initial value before it hits your while loop for the first time?

Of course, as you are showing snippets, you may do, but it is not shown it here, whilst you have shown it being set after the while loop, which is unusual. And as mentioned above, you probably want to set it to zero rather than one.


   
ReplyQuote
Tigs62
(@tigs62)
Member
Joined: 3 years ago
Posts: 20
Topic starter  

@davee

Hi Davee,

Yes, when I declare SensorLoop for the first time I set it to 1 (see below):

int SensorLoop = 1

 

You are never too old to learn.
You are never too old to teach.


   
ReplyQuote
Tigs62
(@tigs62)
Member
Joined: 3 years ago
Posts: 20
Topic starter  

@larry-manson

Hi Larry,

Aah Yes!  This is the crux of the problem clearly.
My code sees tempSensor1, tempSensor2 etc as completely different variables, not as an Array.

Whereas my loop is trying to treat them as an Array, which the code cannot find.

As i have 5 physical sensors, and as you say the first element of an array is always 0, then presumably I will need to declare it as:

float tempSensor(4);

Thanks for the "prod" and for the link too.  😀 

You are never too old to learn.
You are never too old to teach.


   
ReplyQuote
(@larry-manson)
Member
Joined: 3 years ago
Posts: 18
 

Hi Tigs,

 

"It also means that in an array with ten elements, index nine is the last element. "

 

Therefore you need 

 

Float tempSensor(5) 

 

Where there are 5 elements 0, 1 ,2 , 3 , and 4

 

Larry 


   
ReplyQuote
Tigs62
(@tigs62)
Member
Joined: 3 years ago
Posts: 20
Topic starter  

@larry-manson

Doh!  I miss the days of ZX80 Basic.

Thanks Larry
Chris

You are never too old to learn.
You are never too old to teach.


   
ReplyQuote