<div dir="ltr"><div class="gmail_default" style="font-size:small">Thanks Marcus, when I test the code in the function only, it works, but in my program it fails as shown.</div><div class="gmail_default" style="font-size:small">I am trying to get debugging working in Platformio on the nano.</div><div class="gmail_default" style="font-size:small">...Pat C.<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Dec 17, 2021 at 12:08 PM markus <<a href="mailto:markus@bibi.ca">markus@bibi.ca</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi Pat,<br>
<br>
looking at your code, whatever goes wrong doesn't seem to be in the<br>
code snippet you sent. In my experience there is either<br>
an ISR messing up the memory or you run into a stack corruption.<br>
<br>
Can you check the memory usage, static and dynamic and see if<br>
you have a stack overrun?<br>
<br>
I would recommend running the program in the debugger and setting a<br>
breakpoint right on the "if" condition of the first loop where you<br>
assign the 999. Check the entire array on each iteration.<br>
<br>
If the array still looks correct on the last iteration, step through<br>
the second loop and check the values.<br>
<br>
Interesting problem, please keep us posted with any findings.<br>
<br>
On Fri, 17 Dec 2021 11:13:47 -0500<br>
Pat Caron via DPRGlist <<a href="mailto:dprglist@lists.dprg.org" target="_blank">dprglist@lists.dprg.org</a>> wrote:<br>
<br>
> What I meant to say last night is the array size is cm[8]. The<br>
> values for SONAR_NUM == 3 and IR_NUM == 2<br>
> This should not be overrunning the array as it only loops through 5<br>
> times. I have tried setting maxDistance as uint32_t without any<br>
> difference. I have commented out the sendData() function and added<br>
> some Serial.print statements prior to reassigning the values. (shown<br>
> in output)<br>
> <br>
> #define SONAR_NUM 3<br>
> #define IR_NUM 2<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 libraray<br>
> char *position[] = {"us_Left", "us_Mid", "us_Right", "ir_Left",<br>
> "ir_Right", "cm[5]", "cm[6]", "cm[7]"};<br>
> .<br>
> . // Other code here<br>
> .<br>
> <br>
> void oneSensorCycle() { // Sensor ping cycle complete, do something<br>
> with the results.<br>
> for (uint8_t x = 0; x < (SONAR_NUM + IR_NUM); x++) {<br>
> Serial.print("cm[");<br>
> Serial.print(x);<br>
> Serial.print("] = ");<br>
> Serial.println(cm[x]);<br>
> if ((cm[x] == 0) or (cm[x] > maxDistance)) {<br>
> cm[x] = 999;<br>
> }<br>
> }<br>
> if (troubleshoot) {<br>
> for (uint8_t i = 0; i < (SONAR_NUM + IR_NUM); i++) {<br>
> Serial.print("| ");<br>
> Serial.print(position[i]);<br>
> Serial.print(" = ");<br>
> Serial.print(cm[i]);<br>
> Serial.print("cm ");<br>
> }<br>
> Serial.print("| ");<br>
> Serial.print("Time = ");<br>
> Serial.print(elapsedMillis);<br>
> Serial.print("mS");<br>
> Serial.println();<br>
> }<br>
> //else {<br>
> // sendData();<br>
> //}<br>
> }<br>
> <br>
> <br>
> Loop() {<br>
> .// other code<br>
> oneSensorCycle()<br>
> . // more code<br>
> }<br>
> <br>
> Serial output:<br>
> <br>
> -- Miniterm on /dev/ttyUSB-sensors 115200,8,N,1 ---<br>
> --- Quit: Ctrl+C | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---<br>
> cm[0] = 0<br>
> cm[1] = 95<br>
> cm[2] = 91<br>
> cm[3] = 85<br>
> cm[4] = 246<br>
> | us_Left = 999cm | us_Mid = 999cm | us_Right = 999cm | ir_Left =<br>
> 999cm | ir_Right = 999cm | Time = 44mS<br>
> <br>
> ...Pat C<br>
> <br>
> <br>
> On Fri, Dec 17, 2021 at 10:07 AM Chris Netter <<a href="mailto:netterchris@gmail.com" target="_blank">netterchris@gmail.com</a>><br>
> wrote:<br>
> <br>
> > The for loop stops at x < 8, so it won’t overrun.<br>
> ><br>
> ><br>
> ><br>
> > What if you remove the sendData() call? Does that help?<br>
> ><br>
> ><br>
> ><br>
> > Also, make sure you have declared any global variables which are<br>
> > modified by interrupts or other tasks as “volatile”, otherwise the<br>
> > compile may decide to optimize away parts of your code.<br>
> ><br>
> ><br>
> ><br>
> > Chris<br>
> ><br>
> ><br>
> ><br>
> > *From: *Doug Paradis <<a href="mailto:paradug@gmail.com" target="_blank">paradug@gmail.com</a>><br>
> > *Sent: *Thursday, December 16, 2021 11:46 PM<br>
> > *To: *Chris Netter <<a href="mailto:netterchris@gmail.com" target="_blank">netterchris@gmail.com</a>><br>
> > *Cc: *Pat Caron <<a href="mailto:patcaron@mail.com" target="_blank">patcaron@mail.com</a>>; <a href="mailto:dprglist@lists.dprg.org" target="_blank">dprglist@lists.dprg.org</a><br>
> > *Subject: *Re: [Dprglist] Arduino Code Troubles<br>
> ><br>
> ><br>
> ><br>
> > Pat,<br>
> ><br>
> > for (uint8_t x = 0; x < (SONAR_NUM + IR_NUM); x++) {<br>
> ><br>
> ><br>
> ><br>
> > If SONAR_NUM _ IR_NUM = 8 then you are overrunning the array<br>
> > which goes 0 to 7.<br>
> ><br>
> ><br>
> ><br>
> > Regards,<br>
> ><br>
> > Doug P.<br>
> ><br>
> ><br>
> ><br>
> > On Thu, Dec 16, 2021 at 8:53 PM Chris Netter via DPRGlist < <br>
> > <a href="mailto:dprglist@lists.dprg.org" target="_blank">dprglist@lists.dprg.org</a>> wrote: <br>
> ><br>
> > So Markus and Karim already pointed out that the original code<br>
> > snipped works as designed.<br>
> ><br>
> ><br>
> ><br>
> > It’s also been pointed out already that you are mixing data types.<br>
> > I don’t think that’s the issue, but worth trying to make both the<br>
> > array and maxDistance uint16_t<br>
> ><br>
> ><br>
> ><br>
> > As for this new code snippet:<br>
> ><br>
> ><br>
> ><br>
> > I don’t see any obvious issue with the for loop. Are you positive<br>
> > that SONAR_NUM<br>
> > + IR_NUM == 8 ? if not, you are overshooting the array and that<br>
> > can cause all kinds of non-obvious issues.<br>
> ><br>
> ><br>
> ><br>
> > Also, what does sendData() do? Could it have some side-effects?<br>
> > Maybe its causing a stack overflow, array bounds overflow, or<br>
> > similar, which in turn causes non-obvious issues?<br>
> ><br>
> ><br>
> ><br>
> ><br>
> ><br>
> ><br>
> ><br>
> > *From: *Pat Caron via DPRGlist <<a href="mailto:dprglist@lists.dprg.org" target="_blank">dprglist@lists.dprg.org</a>><br>
> > *Sent: *Thursday, December 16, 2021 9:04 PM<br>
> > *To: *<a href="mailto:dprglist@lists.dprg.org" target="_blank">dprglist@lists.dprg.org</a><br>
> > *Subject: *Re: [Dprglist] Arduino Code Troubles<br>
> ><br>
> ><br>
> ><br>
> > Hi guys, here is a better example to show what is happening taken<br>
> > from actual code:<br>
> ><br>
> ><br>
> ><br>
> > .... other code<br>
> > for (uint8_t x = 0; x < (SONAR_NUM + IR_NUM); x++) { //<br>
> > SONAR_NUM + IR_NUM = 8<br>
> > if ((cm[x] == 0) || (cm[x] > maxDistance)) {<br>
> > cm[x] = 999;<br>
> > }<br>
> ><br>
> > sendData();<br>
> ><br>
> > }<br>
> ><br>
> > Printed values cm[0] to cm[5] (us_Left to ir_Right) without the<br>
> > above if statement<br>
> > | us_Left = 0cm | us_Mid = 95cm | us_Right = 51cm | ir_Left = 84cm |<br>
> > 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 =<br>
> > 999cm | ir_Right = 999cm | Time = 44mS<br>
> ><br>
> > ...Pat C<br>
> ><br>
> ><br>
> ><br>
> > On Thu, Dec 16, 2021 at 8:45 PM Karim Virani<br>
> > <<a href="mailto:pondersome64@gmail.com" target="_blank">pondersome64@gmail.com</a>> wrote:<br>
> ><br>
> > It's doing what it's coded to do<br>
> ><br>
> ><br>
> ><br>
> > On Thu, Dec 16, 2021 at 7:42 PM Pat Caron via DPRGlist < <br>
> > <a href="mailto:dprglist@lists.dprg.org" target="_blank">dprglist@lists.dprg.org</a>> wrote: <br>
> ><br>
> > Hi Murray, I did try that with the same results. NewPing.h library<br>
> > is looking for a uint16_t although it didn't complain when I tried<br>
> > that.<br>
> ><br>
> ><br>
> ><br>
> > ...Pat C<br>
> ><br>
> ><br>
> ><br>
> > On Thu, Dec 16, 2021 at 8:35 PM Murray Altheim via DPRGlist < <br>
> > <a href="mailto:dprglist@lists.dprg.org" target="_blank">dprglist@lists.dprg.org</a>> wrote: <br>
> ><br>
> > 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<br>
> > > library .<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<br>
> > > 268 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<br>
> > > 268.<br>
> > ><br>
> > > ...Pat C<br>
> > ><br>
> > > _______________________________________________<br>
> > > DPRGlist mailing list<br>
> > > <a href="mailto:DPRGlist@lists.dprg.org" target="_blank">DPRGlist@lists.dprg.org</a><br>
> > > <a href="http://lists.dprg.org/listinfo.cgi/dprglist-dprg.org" rel="noreferrer" target="_blank">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/" rel="noreferrer" target="_blank">http://www.altheim.com/murray/</a><br>
> > === ===<br>
> > =<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" target="_blank">DPRGlist@lists.dprg.org</a><br>
> > <a href="http://lists.dprg.org/listinfo.cgi/dprglist-dprg.org" rel="noreferrer" target="_blank">http://lists.dprg.org/listinfo.cgi/dprglist-dprg.org</a><br>
> ><br>
> > _______________________________________________<br>
> > DPRGlist mailing list<br>
> > <a href="mailto:DPRGlist@lists.dprg.org" target="_blank">DPRGlist@lists.dprg.org</a><br>
> > <a href="http://lists.dprg.org/listinfo.cgi/dprglist-dprg.org" rel="noreferrer" target="_blank">http://lists.dprg.org/listinfo.cgi/dprglist-dprg.org</a><br>
> ><br>
> ><br>
> ><br>
> > _______________________________________________<br>
> > DPRGlist mailing list<br>
> > <a href="mailto:DPRGlist@lists.dprg.org" target="_blank">DPRGlist@lists.dprg.org</a><br>
> > <a href="http://lists.dprg.org/listinfo.cgi/dprglist-dprg.org" rel="noreferrer" target="_blank">http://lists.dprg.org/listinfo.cgi/dprglist-dprg.org</a><br>
> ><br>
> ><br>
> > <br>
<br>
</blockquote></div>