<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv=Content-Type content="text/html; charset=utf-8"><meta name=Generator content="Microsoft Word 15 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
.MsoChpDefault
{mso-style-type:export-only;}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
{page:WordSection1;}
--></style></head><body lang=EN-US link=blue vlink="#954F72" style='word-wrap:break-word'><div class=WordSection1><p class=MsoNormal>So Markus and Karim already pointed out that the original code snipped works as designed.</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>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</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>As for this new code snippet:</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>I don’t see any obvious issue with the for loop. Are you positive that <span style='font-size:12.0pt'>SONAR_NUM + IR_NUM == 8 ? if not, you are overshooting the array and that can cause all kinds of non-obvious issues.<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt'>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></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal><o:p> </o:p></p><div style='mso-element:para-border-div;border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in'><p class=MsoNormal style='border:none;padding:0in'><b>From: </b><a href="mailto:dprglist@lists.dprg.org">Pat Caron via DPRGlist</a><br><b>Sent: </b>Thursday, December 16, 2021 9:04 PM<br><b>To: </b><a href="mailto:dprglist@lists.dprg.org">dprglist@lists.dprg.org</a><br><b>Subject: </b>Re: [Dprglist] Arduino Code Troubles</p></div><p class=MsoNormal><o:p> </o:p></p><div><div><p class=MsoNormal><span style='font-size:12.0pt'>Hi guys, here is a better example to show what is happening taken from actual code:<o:p></o:p></span></p></div><div><p class=MsoNormal><span style='font-size:12.0pt'><o:p> </o:p></span></p></div><div><p class=MsoNormal><span style='font-size:12.0pt'>.... other code<br> for (uint8_t x = 0; x < (SONAR_NUM + IR_NUM); x++) { // SONAR_NUM + IR_NUM = 8<br> if ((cm[x] == 0) || (cm[x] > maxDistance)) {<br> cm[x] = 999;<br> }<o:p></o:p></span></p></div><div><p class=MsoNormal><span style='font-size:12.0pt'> sendData();<o:p></o:p></span></p></div><div><p class=MsoNormal><span style='font-size:12.0pt'> }<br><br>Printed values cm[0] to cm[5] (us_Left to ir_Right) without the above if statement<br>| us_Left = 0cm | us_Mid = 95cm | us_Right = 51cm | ir_Left = 84cm | ir_Right = 252cm | Time = 45mS<br><br>Printed values cm[0] to cm[5] with the above if statement<br>\| us_Left = 999cm | us_Mid = 999cm | us_Right = 999cm | ir_Left = 999cm | ir_Right = 999cm | Time = 44mS<br><br>...Pat C<o:p></o:p></span></p></div></div><p class=MsoNormal><o:p> </o:p></p><div><div><p class=MsoNormal>On Thu, Dec 16, 2021 at 8:45 PM Karim Virani <<a href="mailto:pondersome64@gmail.com">pondersome64@gmail.com</a>> wrote:</p></div><blockquote style='border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-right:0in'><div><p class=MsoNormal>It's doing what it's coded to do</p></div><p class=MsoNormal><o:p> </o:p></p><div><div><p class=MsoNormal>On Thu, Dec 16, 2021 at 7:42 PM Pat Caron via DPRGlist <<a href="mailto:dprglist@lists.dprg.org">dprglist@lists.dprg.org</a>> wrote:</p></div><blockquote style='border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-right:0in'><div><div><p class=MsoNormal><span style='font-size:12.0pt'>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.<o:p></o:p></span></p></div><div><p class=MsoNormal><span style='font-size:12.0pt'><o:p> </o:p></span></p></div><div><p class=MsoNormal><span style='font-size:12.0pt'>...Pat C<o:p></o:p></span></p></div></div><p class=MsoNormal><o:p> </o:p></p><div><div><p class=MsoNormal>On Thu, Dec 16, 2021 at 8:35 PM Murray Altheim via DPRGlist <<a href="mailto:dprglist@lists.dprg.org">dprglist@lists.dprg.org</a>> wrote:</p></div><blockquote style='border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-right:0in'><p class=MsoNormal>Hi Pat,<br><br>What if you define maxDistance as uint32_t?<br><br>Cheers,<br><br>Murray<br><br>On 17/12/21 2:27 pm, Pat Caron via DPRGlist wrote:<br>> Hi guys, I'm looking for help with the following Arduino code.<br>> <br>> uint32_t cm[8] = {0,0,0,0,0,0,0,0}; // Create array<br>> uint16_t maxDistance = 200; // Also used with NewPing.h library<br>> .<br>> . // Other code here<br>> .<br>> irSensorL.read(); // Read IR sensor... This is working!<br>> cm[3] = irSensorL.ranging_data.range_mm/10; // This returns 268<br>> if (cm[3] > maxDistance) {<br>> cm[3] = 999; /<br>> }<br>> Serial.println(cm[3]); / cm[3] value is always = 999<br>> sendData();<br>> <br>> The cm[3] value is always 999 when I run this.<br>> If I comment out the if cm[3]... statement cm[3] value is then 268.<br>> <br>> ...Pat C<br>> <br>> _______________________________________________<br>> DPRGlist mailing list<br>> <a href="mailto:DPRGlist@lists.dprg.org">DPRGlist@lists.dprg.org</a><br>> <a href="http://lists.dprg.org/listinfo.cgi/dprglist-dprg.org">http://lists.dprg.org/listinfo.cgi/dprglist-dprg.org</a><br>> <br><br>-- <br><br>..........................................................................<br>Murray Altheim <murray18 at altheim dot com> = = ===<br><a href="http://www.altheim.com/murray/">http://www.altheim.com/murray/</a> === ===<br> = = ===<br> In the evening<br> The rice leaves in the garden<br> Rustle in the autumn wind<br> That blows through my reed hut.<br> -- Minamoto no Tsunenobu<br><br>_______________________________________________<br>DPRGlist mailing list<br><a href="mailto:DPRGlist@lists.dprg.org">DPRGlist@lists.dprg.org</a><br><a href="http://lists.dprg.org/listinfo.cgi/dprglist-dprg.org">http://lists.dprg.org/listinfo.cgi/dprglist-dprg.org</a></p></blockquote></div></blockquote></div></blockquote></div><p class=MsoNormal style='margin-left:9.6pt'>_______________________________________________<br>DPRGlist mailing list<br><a href="mailto:DPRGlist@lists.dprg.org">DPRGlist@lists.dprg.org</a><br><a href="http://lists.dprg.org/listinfo.cgi/dprglist-dprg.org">http://lists.dprg.org/listinfo.cgi/dprglist-dprg.org</a></p><p class=MsoNormal><o:p> </o:p></p></div></body></html>