<div style="color:black;font: 10pt Arial, Helvetica, sans-serif;">Bitwise conditionals have different rules than conditionals that evaluate an expression prior to applying the logic depending on the language.
<div><br>
</div>
<div>Rud may have nailed it. I've been bitten by that difference in Matlab, Octave and FreeBASIC.</div>
<div><br>
</div>
<div>John Swindle<br>
<br>
<div style="font-family:arial,helvetica;font-size:10pt;color:black"><font size="2">-----Original Message-----<br>
From: Rud Merriam via DPRGlist <dprglist@lists.dprg.org><br>
To: dprglist@lists.dprg.org<br>
Cc: Rud Merriam <rudmerriam@gmail.com><br>
Sent: Fri, Dec 17, 2021 2:35 pm<br>
Subject: Re: [Dprglist] Arduino Code Troubles<br>
<br>
<div id="yiv3997825307">
<div>
<div>Obviously somethine strange happening. Grasping at a straw try
replacing the 'or' in the conditional with '||'. It shouldn't make
a difference but.... <br clear="none">
</div>
<div>Started to make othere suggestions to localize the problem but to
much verbiage. Here is what I would try:<br clear="none">
</div>
<pre> for (uint8_t x = 0; x < (SONAR_NUM + IR_NUM); x++) {
Serial.print("cm[");
Serial.print(x);
Serial.print("] = ");
Serial.print(cm[x]);
Serial.print(" ");
if ((cm[x] == 0) || (cm[x] > maxDistance)) {
Serial.print(cm[x]); //
Serial.print(" ");
cm[x] = 999;
Serial.print(cm[x]); //
Serial.print(" ");
}
Serial.println(cm[x]);
}</pre>
<div><br clear="none">
</div>
<div class="yiv3997825307moz-signature"><font face="Comic Sans MS" color="#000080"> <br clear="none">
-73 - <br clear="none">
<b>Rud Merriam K5RUD</b> <br clear="none">
<a rel="nofollow noopener noreferrer" shape="rect" target="_blank" href="http://mysticlakesoftware.com/"> <i>Mystic Lake
Software</i> </a> <br clear="none">
<br clear="none">
</font>
</div>
<div id="yiv3997825307yqtfd15399" class="yiv3997825307yqt5533604298">
<div class="yiv3997825307moz-cite-prefix">On 12/17/21 10:13 AM, Pat Caron via
DPRGlist wrote:<br clear="none">
</div>
<blockquote type="cite">
</blockquote></div>
</div>
<div id="yiv3997825307yqtfd69348" class="yiv3997825307yqt5533604298">
<div>
<div dir="ltr">
<div style="font-size:small;" class="yiv3997825307gmail_default"><br clear="none">
<div style="font-size:small;" class="yiv3997825307gmail_default">What I
meant to say last night is the array size is cm[8]. The
values for SONAR_NUM == 3 and IR_NUM == 2<br clear="none">
This should not be overrunning the array as it only loops
through 5 times.<br clear="none">
I have tried setting maxDistance as uint32_t without any
difference.</div>
<div style="font-size:small;" class="yiv3997825307gmail_default">I have
commented out the sendData() function and added some
Serial.print statements prior to reassigning the values.
(shown in output)</div>
<div style="font-size:small;" class="yiv3997825307gmail_default"><br clear="none">
</div>
<div style="font-size:small;" class="yiv3997825307gmail_default">#define
SONAR_NUM 3<br clear="none">
#define IR_NUM 2<br clear="none">
uint32_t cm[8] = {0,0,0,0,0,0,0,0}; // Create array<br clear="none">
uint16_t maxDistance = 200; // Also used with NewPing.h
libraray<br clear="none">
char *position[] = {"us_Left", "us_Mid", "us_Right",
"ir_Left", "ir_Right", "cm[5]", "cm[6]", "cm[7]"};<br clear="none">
.<br clear="none">
. // Other code here<br clear="none">
.<br clear="none">
<br clear="none">
void oneSensorCycle() { // Sensor ping cycle complete, do
something with the results.<br clear="none">
for (uint8_t x = 0; x < (SONAR_NUM + IR_NUM); x++) {<br clear="none">
Serial.print("cm[");<br clear="none">
Serial.print(x);<br clear="none">
Serial.print("] = ");<br clear="none">
Serial.println(cm[x]);<br clear="none">
if ((cm[x] == 0) or (cm[x] > maxDistance)) {<br clear="none">
cm[x] = 999;<br clear="none">
}<br clear="none">
}<br clear="none">
if (troubleshoot) {<br clear="none">
for (uint8_t i = 0; i < (SONAR_NUM + IR_NUM); i++) {<br clear="none">
Serial.print("| ");<br clear="none">
Serial.print(position[i]);<br clear="none">
Serial.print(" = ");<br clear="none">
Serial.print(cm[i]);<br clear="none">
Serial.print("cm ");<br clear="none">
}<br clear="none">
Serial.print("| ");<br clear="none">
Serial.print("Time = ");<br clear="none">
Serial.print(elapsedMillis);<br clear="none">
Serial.print("mS");<br clear="none">
Serial.println();<br clear="none">
}<br clear="none">
//else {<br clear="none">
// sendData();<br clear="none">
//}<br clear="none">
}<br clear="none">
<br clear="none">
<br clear="none">
Loop() {<br clear="none">
</div>
<div style="font-size:small;" class="yiv3997825307gmail_default"> .// other
code<br clear="none">
</div>
<div style="font-size:small;" class="yiv3997825307gmail_default">
oneSensorCycle()<br clear="none">
</div>
<div style="font-size:small;" class="yiv3997825307gmail_default"> . // more
code<br clear="none">
</div>
<div style="font-size:small;" class="yiv3997825307gmail_default">}<br clear="none">
<br clear="none">
Serial output:<br clear="none">
<br clear="none">
-- Miniterm on /dev/ttyUSB-sensors 115200,8,N,1 ---<br clear="none">
--- Quit: Ctrl+C | Menu: Ctrl+T | Help: Ctrl+T followed by
Ctrl+H ---<br clear="none">
cm[0] = 0<br clear="none">
cm[1] = 95<br clear="none">
cm[2] = 91<br clear="none">
cm[3] = 85<br clear="none">
cm[4] = 246<br clear="none">
| us_Left = 999cm | us_Mid = 999cm | us_Right = 999cm |
ir_Left = 999cm | ir_Right = 999cm | Time = 44mS<br clear="none">
<br clear="none">
...Pat C<br clear="none">
<br clear="none">
</div>
</div>
</div>
<br clear="none">
<div class="yiv3997825307gmail_quote">
<div dir="ltr" class="yiv3997825307gmail_attr">On Fri, Dec 17, 2021 at 10:07
AM Chris Netter <<a rel="nofollow noopener noreferrer" shape="rect" ymailto="mailto:netterchris@gmail.com" target="_blank" href="mailto:netterchris@gmail.com">netterchris@gmail.com</a>> wrote:<br clear="none">
</div>
<blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex;" class="yiv3997825307gmail_quote">
<div lang="EN-US" style="">
<div class="yiv3997825307gmail-m_6843183720359827069WordSection1">
<div class="yiv3997825307MsoNormal">The for loop stops at x < 8, so it
won’t overrun.</div>
<div class="yiv3997825307MsoNormal"> </div>
<div class="yiv3997825307MsoNormal">What if you remove the sendData()
call? Does that help?</div>
<div class="yiv3997825307MsoNormal"> </div>
<div class="yiv3997825307MsoNormal">Also, make sure you have declared
any global variables which are modified by interrupts or
other tasks as “volatile”, otherwise the compile may
decide to optimize away parts of your code.</div>
<div class="yiv3997825307MsoNormal"> </div>
<div class="yiv3997825307MsoNormal">Chris</div>
<div class="yiv3997825307MsoNormal"> </div>
<div style="border-color:rgb(225,225,225) currentcolor currentcolor;border-style:solid none none;border-width:1pt medium medium;padding:3pt 0in 0in;">
<div style="border:medium none;padding:0in;" class="yiv3997825307MsoNormal"><b>From: </b><a rel="nofollow noopener noreferrer" shape="rect" ymailto="mailto:paradug@gmail.com" target="_blank" href="mailto:paradug@gmail.com">Doug Paradis</a><br clear="none">
<b>Sent: </b>Thursday, December 16, 2021 11:46 PM<br clear="none">
<b>To: </b><a rel="nofollow noopener noreferrer" shape="rect" ymailto="mailto:netterchris@gmail.com" target="_blank" href="mailto:netterchris@gmail.com">Chris Netter</a><br clear="none">
<b>Cc: </b><a rel="nofollow noopener noreferrer" shape="rect" ymailto="mailto:patcaron@mail.com" target="_blank" href="mailto:patcaron@mail.com">Pat Caron</a>;
<a rel="nofollow noopener noreferrer" shape="rect" ymailto="mailto:dprglist@lists.dprg.org" target="_blank" href="mailto:dprglist@lists.dprg.org">dprglist@lists.dprg.org</a><br clear="none">
<b>Subject: </b>Re: [Dprglist] Arduino Code Troubles</div>
</div>
<div class="yiv3997825307MsoNormal"> </div>
<div>
<div class="yiv3997825307MsoNormal">Pat,</div>
<div>
<div class="yiv3997825307MsoNormal"> for (uint8_t x = 0; x <
(SONAR_NUM + IR_NUM); x++) {</div>
</div>
<div>
<div class="yiv3997825307MsoNormal"> </div>
</div>
<div>
<div class="yiv3997825307MsoNormal"> If SONAR_NUM _ IR_NUM = 8
then you are overrunning the array which goes 0 to
7.</div>
</div>
<div>
<div class="yiv3997825307MsoNormal"> </div>
</div>
<div>
<div class="yiv3997825307MsoNormal">Regards,</div>
</div>
<div>
<div class="yiv3997825307MsoNormal">Doug P.</div>
</div>
</div>
<div class="yiv3997825307MsoNormal"> </div>
<div>
<div>
<div class="yiv3997825307MsoNormal">On Thu, Dec 16, 2021 at 8:53 PM
Chris Netter via DPRGlist <<a rel="nofollow noopener noreferrer" shape="rect" ymailto="mailto:dprglist@lists.dprg.org" target="_blank" href="mailto:dprglist@lists.dprg.org">dprglist@lists.dprg.org</a>>
wrote:</div>
</div>
<blockquote style="border-color:currentcolor currentcolor currentcolor rgb(204,204,204);border-style:none none none solid;border-width:medium medium medium 1pt;padding:0in 0in 0in 6pt;margin-left:4.8pt;margin-right:0in;">
<div>
<div>
<div class="yiv3997825307MsoNormal">So Markus and Karim already
pointed out that the original code snipped works
as designed.</div>
<div class="yiv3997825307MsoNormal"> </div>
<div class="yiv3997825307MsoNormal">It’s also been pointed out
already that you are mixing data types. I don’t
think that’s the issue, but worth trying to make
both the array and maxDistance uint16_t</div>
<div class="yiv3997825307MsoNormal"> </div>
<div class="yiv3997825307MsoNormal">As for this new code snippet:</div>
<div class="yiv3997825307MsoNormal"> </div>
<div class="yiv3997825307MsoNormal">I don’t see any obvious issue
with the for loop. Are you positive that <span style="font-size:12pt;">SONAR_NUM + IR_NUM == 8
? if not, you are overshooting the array and
that can cause all kinds of non-obvious
issues.</span></div>
<div class="yiv3997825307MsoNormal"><span style="font-size:12pt;"> </span></div>
<div class="yiv3997825307MsoNormal"><span style="font-size:12pt;">Also,
what does sendData() do? Could it have some
side-effects? Maybe its causing a stack
overflow, array bounds overflow, or similar,
which in turn causes non-obvious issues?</span></div>
<div class="yiv3997825307MsoNormal"> </div>
<div class="yiv3997825307MsoNormal"> </div>
<div class="yiv3997825307MsoNormal"> </div>
<div style="border-color:rgb(225,225,225) currentcolor currentcolor;border-style:solid none none;border-width:1pt medium medium;padding:3pt 0in 0in;">
<div class="yiv3997825307MsoNormal"><b>From: </b><a rel="nofollow noopener noreferrer" shape="rect" ymailto="mailto:dprglist@lists.dprg.org" target="_blank" href="mailto:dprglist@lists.dprg.org">Pat
Caron via DPRGlist</a><br clear="none">
<b>Sent: </b>Thursday, December 16, 2021 9:04
PM<br clear="none">
<b>To: </b><a rel="nofollow noopener noreferrer" shape="rect" ymailto="mailto:dprglist@lists.dprg.org" target="_blank" href="mailto:dprglist@lists.dprg.org">dprglist@lists.dprg.org</a><br clear="none">
<b>Subject: </b>Re: [Dprglist] Arduino Code
Troubles</div>
</div>
<div class="yiv3997825307MsoNormal"> </div>
<div>
<div>
<div class="yiv3997825307MsoNormal"><span style="font-size:12pt;">Hi guys, here is a
better example to show what is happening
taken from actual code:</span></div>
</div>
<div>
<div class="yiv3997825307MsoNormal"><span style="font-size:12pt;"> </span></div>
</div>
<div>
<div class="yiv3997825307MsoNormal"><span style="font-size:12pt;">.... other code<br clear="none">
for (uint8_t x = 0; x < (SONAR_NUM +
IR_NUM); x++) { // SONAR_NUM + IR_NUM = 8<br clear="none">
if ((cm[x] == 0) || (cm[x] >
maxDistance)) {<br clear="none">
cm[x] = 999;<br clear="none">
}</span></div>
</div>
<div>
<div class="yiv3997825307MsoNormal"><span style="font-size:12pt;"> sendData();</span></div>
</div>
<div>
<div class="yiv3997825307MsoNormal"><span style="font-size:12pt;"> }<br clear="none">
<br clear="none">
Printed values cm[0] to cm[5] (us_Left to
ir_Right) without the above if statement<br clear="none">
| us_Left = 0cm | us_Mid = 95cm | us_Right
= 51cm | ir_Left = 84cm | ir_Right = 252cm
| Time = 45mS<br clear="none">
<br clear="none">
Printed values cm[0] to cm[5] with the
above if statement<br clear="none">
\| us_Left = 999cm | us_Mid = 999cm |
us_Right = 999cm | ir_Left = 999cm |
ir_Right = 999cm | Time = 44mS<br clear="none">
<br clear="none">
...Pat C</span></div>
</div>
</div>
<div class="yiv3997825307MsoNormal"> </div>
<div>
<div>
<div class="yiv3997825307MsoNormal">On Thu, Dec 16, 2021 at
8:45 PM Karim Virani <<a rel="nofollow noopener noreferrer" shape="rect" ymailto="mailto:pondersome64@gmail.com" target="_blank" href="mailto:pondersome64@gmail.com">pondersome64@gmail.com</a>>
wrote:</div>
</div>
<blockquote style="border-color:currentcolor currentcolor currentcolor rgb(204,204,204);border-style:none none none solid;border-width:medium medium medium 1pt;padding:0in 0in 0in 6pt;margin:5pt 0in 5pt 4.8pt;">
<div>
<div class="yiv3997825307MsoNormal">It's doing what it's
coded to do</div>
</div>
<div class="yiv3997825307MsoNormal"> </div>
<div>
<div>
<div class="yiv3997825307MsoNormal">On Thu, Dec 16, 2021
at 7:42 PM Pat Caron via DPRGlist <<a rel="nofollow noopener noreferrer" shape="rect" ymailto="mailto:dprglist@lists.dprg.org" target="_blank" href="mailto:dprglist@lists.dprg.org">dprglist@lists.dprg.org</a>>
wrote:</div>
</div>
<blockquote style="border-color:currentcolor currentcolor currentcolor rgb(204,204,204);border-style:none none none solid;border-width:medium medium medium 1pt;padding:0in 0in 0in 6pt;margin:5pt 0in 5pt 4.8pt;">
<div>
<div>
<div class="yiv3997825307MsoNormal"><span style="font-size:12pt;">Hi Murray,
I did try that with the same
results. NewPing.h library is
looking for a uint16_t although it
didn't complain when I tried that.</span></div>
</div>
<div>
<div class="yiv3997825307MsoNormal"><span style="font-size:12pt;"> </span></div>
</div>
<div>
<div class="yiv3997825307MsoNormal"><span style="font-size:12pt;">...Pat C</span></div>
</div>
</div>
<div class="yiv3997825307MsoNormal"> </div>
<div>
<div>
<div class="yiv3997825307MsoNormal">On Thu, Dec 16,
2021 at 8:35 PM Murray Altheim via
DPRGlist <<a rel="nofollow noopener noreferrer" shape="rect" ymailto="mailto:dprglist@lists.dprg.org" target="_blank" href="mailto:dprglist@lists.dprg.org">dprglist@lists.dprg.org</a>>
wrote:</div>
</div>
<blockquote style="border-color:currentcolor currentcolor currentcolor rgb(204,204,204);border-style:none none none solid;border-width:medium medium medium 1pt;padding:0in 0in 0in 6pt;margin:5pt 0in 5pt 4.8pt;">
<div class="yiv3997825307MsoNormal">Hi Pat,<br clear="none">
<br clear="none">
What if you define maxDistance as
uint32_t?<br clear="none">
<br clear="none">
Cheers,<br clear="none">
<br clear="none">
Murray<br clear="none">
<br clear="none">
On 17/12/21 2:27 pm, Pat Caron via
DPRGlist wrote:<br clear="none">
> Hi guys, I'm looking for help
with the following Arduino code.<br clear="none">
> <br clear="none">
> uint32_t cm[8] =
{0,0,0,0,0,0,0,0}; // Create array<br clear="none">
> uint16_t maxDistance =
200; // Also used with NewPing.h
library<br clear="none">
> .<br clear="none">
> . // Other code here<br clear="none">
> .<br clear="none">
> irSensorL.read(); // Read
IR sensor... This is working!<br clear="none">
> cm[3] =
irSensorL.ranging_data.range_mm/10;
// This returns 268<br clear="none">
> if (cm[3] >
maxDistance) {<br clear="none">
> cm[3] = 999;
/<br clear="none">
> }<br clear="none">
> Serial.println(cm[3]); /
cm[3] value is always = 999<br clear="none">
> sendData();<br clear="none">
> <br clear="none">
> The cm[3] value is always 999
when I run this.<br clear="none">
> If I comment out the if
cm[3]... statement cm[3] value is
then 268.<br clear="none">
> <br clear="none">
> ...Pat C<br clear="none">
> <br clear="none">
>
_______________________________________________<br clear="none">
> DPRGlist mailing list<br clear="none">
> <a rel="nofollow noopener noreferrer" shape="rect" ymailto="mailto:DPRGlist@lists.dprg.org" target="_blank" href="mailto:DPRGlist@lists.dprg.org">DPRGlist@lists.dprg.org</a><br clear="none">
> <a rel="nofollow noopener noreferrer" shape="rect" target="_blank" href="http://lists.dprg.org/listinfo.cgi/dprglist-dprg.org">http://lists.dprg.org/listinfo.cgi/dprglist-dprg.org</a><br clear="none">
> <br clear="none">
<br clear="none">
-- <br clear="none">
<br clear="none">
.........................................................................<br clear="none">
Murray Altheim <murray18 at
altheim dot com>
= = ===<br clear="none">
<a rel="nofollow noopener noreferrer" shape="rect" target="_blank" href="http://www.altheim.com/murray/">http://www.altheim.com/murray/</a>
=== ===<br clear="none">
= =
===<br clear="none">
In the evening<br clear="none">
The rice leaves in the garden<br clear="none">
Rustle in the autumn wind<br clear="none">
That blows through my reed hut.<br clear="none">
-- Minamoto no Tsunenobu<br clear="none">
<br clear="none">
_______________________________________________<br clear="none">
DPRGlist mailing list<br clear="none">
<a rel="nofollow noopener noreferrer" shape="rect" ymailto="mailto:DPRGlist@lists.dprg.org" target="_blank" href="mailto:DPRGlist@lists.dprg.org">DPRGlist@lists.dprg.org</a><br clear="none">
<a rel="nofollow noopener noreferrer" shape="rect" target="_blank" href="http://lists.dprg.org/listinfo.cgi/dprglist-dprg.org">http://lists.dprg.org/listinfo.cgi/dprglist-dprg.org</a></div>
</blockquote>
</div>
</blockquote>
</div>
</blockquote>
</div>
<div style="margin-left:9.6pt;" class="yiv3997825307MsoNormal">_______________________________________________<br clear="none">
DPRGlist mailing list<br clear="none">
<a rel="nofollow noopener noreferrer" shape="rect" ymailto="mailto:DPRGlist@lists.dprg.org" target="_blank" href="mailto:DPRGlist@lists.dprg.org">DPRGlist@lists.dprg.org</a><br clear="none">
<a rel="nofollow noopener noreferrer" shape="rect" target="_blank" href="http://lists.dprg.org/listinfo.cgi/dprglist-dprg.org">http://lists.dprg.org/listinfo.cgi/dprglist-dprg.org</a></div>
<div class="yiv3997825307MsoNormal"> </div>
</div>
</div>
</blockquote>
</div>
<div style="margin-left:4.8pt;" class="yiv3997825307MsoNormal">_______________________________________________<br clear="none">
DPRGlist mailing list<br clear="none">
<a rel="nofollow noopener noreferrer" shape="rect" ymailto="mailto:DPRGlist@lists.dprg.org" target="_blank" href="mailto:DPRGlist@lists.dprg.org">DPRGlist@lists.dprg.org</a><br clear="none">
<a rel="nofollow noopener noreferrer" shape="rect" target="_blank" href="http://lists.dprg.org/listinfo.cgi/dprglist-dprg.org">http://lists.dprg.org/listinfo.cgi/dprglist-dprg.org</a></div>
<div class="yiv3997825307MsoNormal"> </div>
</div>
</div>
</blockquote>
</div>
<br clear="none">
<fieldset class="yiv3997825307mimeAttachmentHeader"></fieldset>
<pre class="yiv3997825307moz-quote-pre">_______________________________________________
DPRGlist mailing list
<a rel="nofollow noopener noreferrer" shape="rect" ymailto="mailto:DPRGlist@lists.dprg.org" target="_blank" href="mailto:DPRGlist@lists.dprg.org" class="yiv3997825307moz-txt-link-abbreviated">DPRGlist@lists.dprg.org</a>
<a rel="nofollow noopener noreferrer" shape="rect" target="_blank" href="http://lists.dprg.org/listinfo.cgi/dprglist-dprg.org" class="yiv3997825307moz-txt-link-freetext">http://lists.dprg.org/listinfo.cgi/dprglist-dprg.org</a>
</pre>
</div>
</div>
</div>
<div class="yqt5533604298" id="yqtfd06328">_______________________________________________<br clear="none">DPRGlist mailing list<br clear="none"><a shape="rect" ymailto="mailto:DPRGlist@lists.dprg.org" href="mailto:DPRGlist@lists.dprg.org">DPRGlist@lists.dprg.org</a><br clear="none"><a shape="rect" href="http://lists.dprg.org/listinfo.cgi/dprglist-dprg.org" target="_blank">http://lists.dprg.org/listinfo.cgi/dprglist-dprg.org</a><br clear="none"></div>
</font></div>
</div>
</div>