<?xml version="1.0" encoding="UTF-8"?>        <rss version="2.0"
             xmlns:atom="http://www.w3.org/2005/Atom"
             xmlns:dc="http://purl.org/dc/elements/1.1/"
             xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
             xmlns:admin="http://webns.net/mvcb/"
             xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
             xmlns:content="http://purl.org/rss/1.0/modules/content/">
        <channel>
            <title>
									Trouble with Loops and Variables - ESP32 &amp; ESP8266				            </title>
            <link>https://forum.dronebotworkshop.com/esp32-esp8266/trouble-with-loops-and-variables/</link>
            <description>Discussion board for Robotics, Arduino, Raspberry Pi and other DIY electronics and modules. Join us today!</description>
            <language>en-US</language>
            <lastBuildDate>Fri, 17 Apr 2026 11:51:39 +0000</lastBuildDate>
            <generator>wpForo</generator>
            <ttl>60</ttl>
							                    <item>
                        <title>RE: Trouble with Loops and Variables</title>
                        <link>https://forum.dronebotworkshop.com/esp32-esp8266/trouble-with-loops-and-variables/#post-18990</link>
                        <pubDate>Tue, 22 Jun 2021 12:07:22 +0000</pubDate>
                        <description><![CDATA[@larry-manson
Doh!  I miss the days of ZX80 Basic.
Thanks LarryChris]]></description>
                        <content:encoded><![CDATA[<p>@larry-manson</p>
<p>Doh!  I miss the days of ZX80 Basic.</p>
<p>Thanks Larry<br />Chris</p>]]></content:encoded>
						                            <category domain="https://forum.dronebotworkshop.com/esp32-esp8266/">ESP32 &amp; ESP8266</category>                        <dc:creator>Tigs62</dc:creator>
                        <guid isPermaLink="true">https://forum.dronebotworkshop.com/esp32-esp8266/trouble-with-loops-and-variables/#post-18990</guid>
                    </item>
				                    <item>
                        <title>RE: Trouble with Loops and Variables</title>
                        <link>https://forum.dronebotworkshop.com/esp32-esp8266/trouble-with-loops-and-variables/#post-18952</link>
                        <pubDate>Sun, 20 Jun 2021 20:03:42 +0000</pubDate>
                        <description><![CDATA[Hi Tigs,
 
&quot;It also means that in an array with ten elements, index nine is the last element. &quot;
 
Therefore you need 
 
Float tempSensor(5) 
 
Where there are 5 elements 0, 1 ,2 , 3 ...]]></description>
                        <content:encoded><![CDATA[<p>Hi Tigs,</p>
<p> </p>
<p>"<span>It also means that in an array with ten elements, index nine is the last element. "</span></p>
<p> </p>
<p>Therefore you need </p>
<p> </p>
<p>Float tempSensor(5) </p>
<p> </p>
<p>Where there are 5 elements 0, 1 ,2 , 3 , and 4</p>
<p> </p>
<p>Larry </p>]]></content:encoded>
						                            <category domain="https://forum.dronebotworkshop.com/esp32-esp8266/">ESP32 &amp; ESP8266</category>                        <dc:creator>Larry Manson</dc:creator>
                        <guid isPermaLink="true">https://forum.dronebotworkshop.com/esp32-esp8266/trouble-with-loops-and-variables/#post-18952</guid>
                    </item>
				                    <item>
                        <title>RE: Trouble with Loops and Variables</title>
                        <link>https://forum.dronebotworkshop.com/esp32-esp8266/trouble-with-loops-and-variables/#post-18951</link>
                        <pubDate>Sun, 20 Jun 2021 19:36:29 +0000</pubDate>
                        <description><![CDATA[@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 ...]]></description>
                        <content:encoded><![CDATA[<p>@larry-manson</p>
<p>Hi Larry,</p>
<p>Aah Yes!  This is the crux of the problem clearly.<br />My code sees tempSensor1, tempSensor2 etc as completely different variables, not as an Array.</p>
<p>Whereas my loop is trying to treat them as an Array, which the code cannot find.</p>
<p>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:</p>
<pre>float tempSensor(4);</pre>
<p>Thanks for the "prod" and for the link too.  😀 </p>]]></content:encoded>
						                            <category domain="https://forum.dronebotworkshop.com/esp32-esp8266/">ESP32 &amp; ESP8266</category>                        <dc:creator>Tigs62</dc:creator>
                        <guid isPermaLink="true">https://forum.dronebotworkshop.com/esp32-esp8266/trouble-with-loops-and-variables/#post-18951</guid>
                    </item>
				                    <item>
                        <title>RE: Trouble with Loops and Variables</title>
                        <link>https://forum.dronebotworkshop.com/esp32-esp8266/trouble-with-loops-and-variables/#post-18950</link>
                        <pubDate>Sun, 20 Jun 2021 19:28:43 +0000</pubDate>
                        <description><![CDATA[@davee
Hi Davee,
Yes, when I declare SensorLoop for the first time I set it to 1 (see below):
int SensorLoop = 1]]></description>
                        <content:encoded><![CDATA[<p>@davee</p>
<p>Hi Davee,</p>
<p>Yes, when I declare SensorLoop for the first time I set it to 1 (see below):</p>
<pre>int SensorLoop = 1</pre>
<p> </p>]]></content:encoded>
						                            <category domain="https://forum.dronebotworkshop.com/esp32-esp8266/">ESP32 &amp; ESP8266</category>                        <dc:creator>Tigs62</dc:creator>
                        <guid isPermaLink="true">https://forum.dronebotworkshop.com/esp32-esp8266/trouble-with-loops-and-variables/#post-18950</guid>
                    </item>
				                    <item>
                        <title>RE: Trouble with Loops and Variables</title>
                        <link>https://forum.dronebotworkshop.com/esp32-esp8266/trouble-with-loops-and-variables/#post-18949</link>
                        <pubDate>Sun, 20 Jun 2021 18:47:34 +0000</pubDate>
                        <description><![CDATA[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 sho...]]></description>
                        <content:encoded><![CDATA[<p>Hi Chris,   @tigs62</p>
<p>Also, do you set SensorLoop to an initial value before it hits your while loop for the first time?</p>
<p>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.</p>]]></content:encoded>
						                            <category domain="https://forum.dronebotworkshop.com/esp32-esp8266/">ESP32 &amp; ESP8266</category>                        <dc:creator>DaveE</dc:creator>
                        <guid isPermaLink="true">https://forum.dronebotworkshop.com/esp32-esp8266/trouble-with-loops-and-variables/#post-18949</guid>
                    </item>
				                    <item>
                        <title>RE: Trouble with Loops and Variables</title>
                        <link>https://forum.dronebotworkshop.com/esp32-esp8266/trouble-with-loops-and-variables/#post-18945</link>
                        <pubDate>Sun, 20 Jun 2021 18:15:17 +0000</pubDate>
                        <description><![CDATA[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 ...]]></description>
                        <content:encoded><![CDATA[<p>You are not declaring tempSensor as an array.</p>
<p>you need something like </p>
<p>float tempSensor(6) // Note the first element in an array is always 0.</p>
<p>You will also have to change your code where you take the temperature readings either with a loop  or by tempSensor(1), tempSensor(2), ....</p>
<p>If you used 0 for the first tempSensor you could declare it to have only 5 elements.</p>
<p> </p>
<p>https://www.arduino.cc/reference/en/language/variables/data-types/array/</p>
<p> </p>
<p>Hope this helps,</p>
<p> </p>
<p>Larry</p>
<p> </p>]]></content:encoded>
						                            <category domain="https://forum.dronebotworkshop.com/esp32-esp8266/">ESP32 &amp; ESP8266</category>                        <dc:creator>Larry Manson</dc:creator>
                        <guid isPermaLink="true">https://forum.dronebotworkshop.com/esp32-esp8266/trouble-with-loops-and-variables/#post-18945</guid>
                    </item>
				                    <item>
                        <title>Trouble with Loops and Variables</title>
                        <link>https://forum.dronebotworkshop.com/esp32-esp8266/trouble-with-loops-and-variables/#post-18943</link>
                        <pubDate>Sun, 20 Jun 2021 17:48:40 +0000</pubDate>
                        <description><![CDATA[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 s...]]></description>
                        <content:encoded><![CDATA[<p>Hi Guys,</p>
<p>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.</p>
<p>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 🙂 </p>
<p>I wont bore you with the code that sets up all the libraries etc.</p>
<p>Because my ESP32 is getting temperature readings from 5 x DS18B20 Temp sensors, I have to declare the special variables as "Floating Point" variables.</p>
<pre>// Variables to hold temperature readings<br />float tempSensor1;<br />float tempSensor2;<br />float tempSensor3;<br />float tempSensor4;<br />float tempSensor5;</pre>
<p> </p>
<p>Then I have to declare the addresses of the actual sensors, because they are on a OneWire Bus:</p>
<pre>DeviceAddress sensor1 = { 0x28, 0x84, 0x71, 0x79, 0x97, 0x5, 0x3, 0xFC };<br />DeviceAddress sensor2 = { 0x28, 0x54, 0x47, 0x79, 0x97, 0x5, 0x3, 0x20 };<br />DeviceAddress sensor3 = { 0x28, 0x16, 0x75, 0x79, 0x97, 0x8, 0x3, 0xB5 };<br />DeviceAddress sensor4 = { 0x28, 0x9F, 0x18, 0x79, 0x97, 0x7, 0x3, 0x2C };<br />DeviceAddress sensor5 = { 0x28, 0x9F, 0xF1, 0x79, 0x97, 0x5, 0x3, 0x94 };</pre>
<p> </p>
<p><br />The next bit is where the variables I created earlier are "filled" with the readings from the actual sensors.</p>
<pre>//---------------------------------<br />// Update the temperature variables<br />// This is getting the temperature readings from 5 x DS18B20 Temp sensors<br />tempSensor1 = sensors.getTempC(sensor1);<br />tempSensor2 = sensors.getTempC(sensor2);<br />tempSensor3 = sensors.getTempC(sensor3);<br />tempSensor4 = sensors.getTempC(sensor4);<br />tempSensor5 = sensors.getTempC(sensor5);</pre>
<p> </p>
<p>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.</p>
<pre>//---------------------------------<br />// Print the temperature sensor readings on the Serial Monitor<br />// This way of doing it, DOES work. <br />Serial.print("Sensor 1: ");<br />Serial.println(tempSensor1);<br /><br />Serial.print("Sensor 2: ");<br />Serial.println(tempSensor2);<br /><br />Serial.print("Sensor 3: ");<br />Serial.println(tempSensor3);<br /><br />Serial.print("Sensor 4: ");<br />Serial.println(tempSensor4);<br /><br />Serial.print("Sensor 5: ");<br />Serial.println(tempSensor5);</pre>
<p> </p>
<p>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.</p>
<p>This is where I came unstuck, because my knowledge was obviously lacking.<br />Below is the bit of code that I tried to use instead of the longhand method.</p>
<pre>//---------------------------------<br />while (SensorLoop &lt; 5) {<br />     SensorLoop = SensorLoop + 1;<br />     Serial.print("Sensor ");<br />     Serial.print(SensorLoop);<br />     Serial.print(": ");<br />     Serial.println(tempSensor(SensorLoop));<br />}<br />SensorLoop = 1</pre>
<p> </p>
<p>What i was hoping to see in the Serial Monitor window was something like this:</p>
<p>Sensor 1: 22.50<br />Sensor 2: 22.67<br />Sensor 3: 22.56<br />Sensor 4: 22.36<br />Sensor 5: 22.75</p>
<p><br />But my code just doesn't want to work.  The error message that appears is this:<br />'tempSensor' was not declared in this scope</p>
<p>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.<br />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.</p>
<p>Chris</p>]]></content:encoded>
						                            <category domain="https://forum.dronebotworkshop.com/esp32-esp8266/">ESP32 &amp; ESP8266</category>                        <dc:creator>Tigs62</dc:creator>
                        <guid isPermaLink="true">https://forum.dronebotworkshop.com/esp32-esp8266/trouble-with-loops-and-variables/#post-18943</guid>
                    </item>
							        </channel>
        </rss>
		