[Dprglist] Arduino Code Troubles
Doug Paradis
paradug at gmail.com
Thu Dec 16 20:46:02 PST 2021
Pat,
for (uint8_t x = 0; x < (SONAR_NUM + IR_NUM); x++) {
If SONAR_NUM _ IR_NUM = 8 then you are overrunning the array which
goes 0 to 7.
Regards,
Doug P.
On Thu, Dec 16, 2021 at 8:53 PM Chris Netter via DPRGlist <
dprglist at lists.dprg.org> wrote:
> So Markus and Karim already pointed out that the original code snipped
> works as designed.
>
>
>
> 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
>
>
>
> As for this new code snippet:
>
>
>
> I don’t see any obvious issue with the for loop. Are you positive that SONAR_NUM
> + IR_NUM == 8 ? if not, you are overshooting the array and that can cause
> all kinds of non-obvious issues.
>
>
>
> 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?
>
>
>
>
>
>
>
> *From: *Pat Caron via DPRGlist <dprglist at lists.dprg.org>
> *Sent: *Thursday, December 16, 2021 9:04 PM
> *To: *dprglist at lists.dprg.org
> *Subject: *Re: [Dprglist] Arduino Code Troubles
>
>
>
> Hi guys, here is a better example to show what is happening taken from
> actual code:
>
>
>
> .... other code
> for (uint8_t x = 0; x < (SONAR_NUM + IR_NUM); x++) { // SONAR_NUM +
> IR_NUM = 8
> if ((cm[x] == 0) || (cm[x] > maxDistance)) {
> cm[x] = 999;
> }
>
> sendData();
>
> }
>
> Printed values cm[0] to cm[5] (us_Left to ir_Right) without the above if
> statement
> | us_Left = 0cm | us_Mid = 95cm | us_Right = 51cm | ir_Left = 84cm |
> ir_Right = 252cm | Time = 45mS
>
> Printed values cm[0] to cm[5] with the above if statement
> \| us_Left = 999cm | us_Mid = 999cm | us_Right = 999cm | ir_Left = 999cm |
> ir_Right = 999cm | Time = 44mS
>
> ...Pat C
>
>
>
> On Thu, Dec 16, 2021 at 8:45 PM Karim Virani <pondersome64 at gmail.com>
> wrote:
>
> It's doing what it's coded to do
>
>
>
> On Thu, Dec 16, 2021 at 7:42 PM Pat Caron via DPRGlist <
> dprglist at lists.dprg.org> wrote:
>
> 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.
>
>
>
> ...Pat C
>
>
>
> On Thu, Dec 16, 2021 at 8:35 PM Murray Altheim via DPRGlist <
> dprglist at lists.dprg.org> wrote:
>
> Hi Pat,
>
> What if you define maxDistance as uint32_t?
>
> Cheers,
>
> Murray
>
> On 17/12/21 2:27 pm, Pat Caron via DPRGlist wrote:
> > Hi guys, I'm looking for help with the following Arduino code.
> >
> > uint32_t cm[8] = {0,0,0,0,0,0,0,0}; // Create array
> > uint16_t maxDistance = 200; // Also used with NewPing.h library
> > .
> > . // Other code here
> > .
> > irSensorL.read(); // Read IR sensor... This is working!
> > cm[3] = irSensorL.ranging_data.range_mm/10; // This returns 268
> > if (cm[3] > maxDistance) {
> > cm[3] = 999; /
> > }
> > Serial.println(cm[3]); / cm[3] value is always = 999
> > sendData();
> >
> > The cm[3] value is always 999 when I run this.
> > If I comment out the if cm[3]... statement cm[3] value is then 268.
> >
> > ...Pat C
> >
> > _______________________________________________
> > DPRGlist mailing list
> > DPRGlist at lists.dprg.org
> > http://lists.dprg.org/listinfo.cgi/dprglist-dprg.org
> >
>
> --
>
> ..........................................................................
> Murray Altheim <murray18 at altheim dot com> = = ===
> http://www.altheim.com/murray/ ===
> ===
> = =
> ===
> In the evening
> The rice leaves in the garden
> Rustle in the autumn wind
> That blows through my reed hut.
> -- Minamoto no Tsunenobu
>
> _______________________________________________
> DPRGlist mailing list
> DPRGlist at lists.dprg.org
> http://lists.dprg.org/listinfo.cgi/dprglist-dprg.org
>
> _______________________________________________
> DPRGlist mailing list
> DPRGlist at lists.dprg.org
> http://lists.dprg.org/listinfo.cgi/dprglist-dprg.org
>
>
> _______________________________________________
> DPRGlist mailing list
> DPRGlist at lists.dprg.org
> http://lists.dprg.org/listinfo.cgi/dprglist-dprg.org
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.dprg.org/pipermail/dprglist-dprg.org/attachments/20211216/b99f3f88/attachment.html>
More information about the DPRGlist
mailing list