<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <div class="moz-cite-prefix"><font face="monospace">As a side note,
        I believe there is great value in defensive programming as a
        habit.  In my code, I always use macros to define constants,
        especially when using them as indicies of an array representing
        a message.  In this case, you could use</font></div>
    <div class="moz-cite-prefix"><font face="monospace"><br>
      </font></div>
    <div class="moz-cite-prefix"><font face="monospace">#define
        US_LEFT   0</font></div>
    <div class="moz-cite-prefix"><font face="monospace">#define
        US_MID    1</font></div>
    <div class="moz-cite-prefix"><font face="monospace">#define
        US_RIGHT  2</font></div>
    <div class="moz-cite-prefix"><font face="monospace">#define
        IR_LEFT   3</font></div>
    <div class="moz-cite-prefix"><font face="monospace">#define
        IR_RIGHT  4</font></div>
    <div class="moz-cite-prefix"><font face="monospace">#define
        TIME_DIFF 5</font></div>
    <div class="moz-cite-prefix"><font face="monospace">#define
        BUFF_SIZE 8</font></div>
    <div class="moz-cite-prefix"><font face="monospace"><br>
      </font></div>
    <div class="moz-cite-prefix"><font face="monospace">#define
        SONAR_NUM 3</font></div>
    <div class="moz-cite-prefix"><font face="monospace">#define
        IR_NUM    2</font></div>
    <div class="moz-cite-prefix"><font face="monospace">#define
        MAX_DISTANCE 200</font></div>
    <div class="moz-cite-prefix"><font face="monospace">#define
        DIST_UNKNOWN 999<br>
      </font></div>
    <div class="moz-cite-prefix"><font face="monospace"><br>
      </font></div>
    <div class="moz-cite-prefix"><font face="monospace">assert(SONAR_NUM
        + IR_NUM <= BUFF_SIZE);<br>
      </font></div>
    <div class="moz-cite-prefix"><font face="monospace"><br>
      </font></div>
    <div class="moz-cite-prefix"><font face="monospace">uint32_t
        cm[BUFF_SIZE];</font></div>
    <div class="moz-cite-prefix"><font face="monospace">for (i = 0; i
        < BUFF_SIZE; ++i) {</font></div>
    <div class="moz-cite-prefix"><font face="monospace">   cm[i] = 0;<br>
      </font></div>
    <div class="moz-cite-prefix"><font face="monospace">}</font></div>
    <div class="moz-cite-prefix"><font face="monospace"><br>
      </font></div>
    <div class="moz-cite-prefix"><font face="monospace">irSensorL.read();</font></div>
    <div class="moz-cite-prefix"><font face="monospace">cm[IR_LEFT] =
        irSensorL.ranging_data.range_mm / 10;</font></div>
    <div class="moz-cite-prefix"><font face="monospace">if (cm[IR_LEFT]
        > MAX_DISTANCE) {</font></div>
    <div class="moz-cite-prefix"><font face="monospace">   cm[IR_LEFT] =
        DIST_UNKNOWN;<br>
      </font></div>
    <div class="moz-cite-prefix"><font face="monospace">}<br>
      </font></div>
    <div class="moz-cite-prefix"><font face="monospace"><br>
      </font></div>
    <div class="moz-cite-prefix"><font face="monospace">Coding constants
        with macros makes it hard to make mistakes, especially when
        making modifications to the code.  It also makes reading <br>
      </font></div>
    <div class="moz-cite-prefix"><font face="monospace">the code much
        easier.  This was a C coding standard at two companies where I
        have worked.</font></div>
    <div class="moz-cite-prefix"><font face="monospace"><br>
      </font></div>
    <div class="moz-cite-prefix"><font face="monospace"><br>
      </font></div>
    <div class="moz-cite-prefix"><font face="monospace">John G.</font><br>
    </div>
    <div class="moz-cite-prefix"><br>
    </div>
    >      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>
    <div class="moz-cite-prefix">On 12/17/2021 10:13 AM, Pat Caron via
      DPRGlist wrote:<br>
    </div>
    <blockquote type="cite"
cite="mid:CA+mr86ffczyWkWwx8D=1AY4Hm5ss0YHf6ZqhU=a5=3N+zzBAfQ@mail.gmail.com">
      <meta http-equiv="content-type" content="text/html; charset=UTF-8">
      <div dir="ltr">
        <div class="gmail_default" style="font-size:small"><br>
          <div class="gmail_default" style="font-size:small">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>
            This should not be overrunning the array as it only loops
            through 5 times.<br>
            I have tried setting maxDistance as uint32_t without any
            difference.</div>
          <div class="gmail_default" style="font-size:small">I have
            commented out the sendData() function and added some
            Serial.print statements prior to reassigning the values.
            (shown in output)</div>
          <div class="gmail_default" style="font-size:small"><br>
          </div>
          <div class="gmail_default" style="font-size:small">#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", "ir_Right", "cm[5]", "cm[6]", "cm[7]"};<br>
            .<br>
            . // Other code here<br>
            .<br>
            <br>
            void oneSensorCycle() { // Sensor ping cycle complete, do
            something 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>
          </div>
          <div class="gmail_default" style="font-size:small">  .// other
            code<br>
          </div>
          <div class="gmail_default" style="font-size:small"> 
            oneSensorCycle()<br>
          </div>
          <div class="gmail_default" style="font-size:small">  . // more
            code<br>
          </div>
          <div class="gmail_default" style="font-size:small">}<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 = 999cm | ir_Right = 999cm | Time = 44mS<br>
            <br>
            ...Pat C<br>
            <br>
          </div>
        </div>
      </div>
      <br>
      <div class="gmail_quote">
        <div dir="ltr" class="gmail_attr">On Fri, Dec 17, 2021 at 10:07
          AM Chris Netter <<a href="mailto:netterchris@gmail.com"
            moz-do-not-send="true" class="moz-txt-link-freetext">netterchris@gmail.com</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">
          <div style="overflow-wrap: break-word;" lang="EN-US">
            <div class="gmail-m_6843183720359827069WordSection1">
              <p class="MsoNormal">The for loop stops at x < 8, so it
                won’t overrun.</p>
              <p class="MsoNormal"> </p>
              <p class="MsoNormal">What if you remove the sendData()
                call?  Does that help?</p>
              <p class="MsoNormal"> </p>
              <p class="MsoNormal">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.</p>
              <p class="MsoNormal"> </p>
              <p class="MsoNormal">Chris</p>
              <p class="MsoNormal"> </p>
              <div style="border-color:rgb(225,225,225) currentcolor
                currentcolor;border-style:solid none
                none;border-width:1pt medium medium;padding:3pt 0in 0in">
                <p class="MsoNormal" style="border:medium
                  none;padding:0in"><b>From: </b><a
                    href="mailto:paradug@gmail.com" target="_blank"
                    moz-do-not-send="true">Doug Paradis</a><br>
                  <b>Sent: </b>Thursday, December 16, 2021 11:46 PM<br>
                  <b>To: </b><a href="mailto:netterchris@gmail.com"
                    target="_blank" moz-do-not-send="true">Chris Netter</a><br>
                  <b>Cc: </b><a href="mailto:patcaron@mail.com"
                    target="_blank" moz-do-not-send="true">Pat Caron</a>;
                  <a href="mailto:dprglist@lists.dprg.org"
                    target="_blank" moz-do-not-send="true"
                    class="moz-txt-link-freetext">dprglist@lists.dprg.org</a><br>
                  <b>Subject: </b>Re: [Dprglist] Arduino Code Troubles</p>
              </div>
              <p class="MsoNormal"> </p>
              <div>
                <p class="MsoNormal">Pat,</p>
                <div>
                  <p class="MsoNormal">    for (uint8_t x = 0; x <
                    (SONAR_NUM + IR_NUM); x++) {</p>
                </div>
                <div>
                  <p class="MsoNormal"> </p>
                </div>
                <div>
                  <p class="MsoNormal">    If  SONAR_NUM _ IR_NUM = 8
                    then you are overrunning the array which goes 0 to
                    7.</p>
                </div>
                <div>
                  <p class="MsoNormal"> </p>
                </div>
                <div>
                  <p class="MsoNormal">Regards,</p>
                </div>
                <div>
                  <p class="MsoNormal">Doug P.</p>
                </div>
              </div>
              <p class="MsoNormal"> </p>
              <div>
                <div>
                  <p class="MsoNormal">On Thu, Dec 16, 2021 at 8:53 PM
                    Chris Netter via DPRGlist <<a
                      href="mailto:dprglist@lists.dprg.org"
                      target="_blank" moz-do-not-send="true"
                      class="moz-txt-link-freetext">dprglist@lists.dprg.org</a>>
                    wrote:</p>
                </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>
                      <p class="MsoNormal">So Markus and Karim already
                        pointed out that the original code snipped works
                        as designed.</p>
                      <p class="MsoNormal"> </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"> </p>
                      <p class="MsoNormal">As for this new code snippet:</p>
                      <p class="MsoNormal"> </p>
                      <p class="MsoNormal">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></p>
                      <p class="MsoNormal"><span style="font-size:12pt"> </span></p>
                      <p class="MsoNormal"><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></p>
                      <p class="MsoNormal"> </p>
                      <p class="MsoNormal"> </p>
                      <p class="MsoNormal"> </p>
                      <div style="border-color:rgb(225,225,225)
                        currentcolor currentcolor;border-style:solid
                        none none;border-width:1pt medium
                        medium;padding:3pt 0in 0in">
                        <p class="MsoNormal"><b>From: </b><a
                            href="mailto:dprglist@lists.dprg.org"
                            target="_blank" moz-do-not-send="true">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"
                            target="_blank" moz-do-not-send="true"
                            class="moz-txt-link-freetext">dprglist@lists.dprg.org</a><br>
                          <b>Subject: </b>Re: [Dprglist] Arduino Code
                          Troubles</p>
                      </div>
                      <p class="MsoNormal"> </p>
                      <div>
                        <div>
                          <p class="MsoNormal"><span
                              style="font-size:12pt">Hi guys, here is a
                              better example to show what is happening
                              taken from actual code:</span></p>
                        </div>
                        <div>
                          <p class="MsoNormal"><span
                              style="font-size:12pt"> </span></p>
                        </div>
                        <div>
                          <p class="MsoNormal"><span
                              style="font-size:12pt">.... 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>
                                  }</span></p>
                        </div>
                        <div>
                          <p class="MsoNormal"><span
                              style="font-size:12pt">  sendData();</span></p>
                        </div>
                        <div>
                          <p class="MsoNormal"><span
                              style="font-size:12pt">  }<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</span></p>
                        </div>
                      </div>
                      <p class="MsoNormal"> </p>
                      <div>
                        <div>
                          <p class="MsoNormal">On Thu, Dec 16, 2021 at
                            8:45 PM Karim Virani <<a
                              href="mailto:pondersome64@gmail.com"
                              target="_blank" moz-do-not-send="true"
                              class="moz-txt-link-freetext">pondersome64@gmail.com</a>>
                            wrote:</p>
                        </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>
                            <p class="MsoNormal">It's doing what it's
                              coded to do</p>
                          </div>
                          <p class="MsoNormal"> </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"
                                  target="_blank" moz-do-not-send="true"
                                  class="moz-txt-link-freetext">dprglist@lists.dprg.org</a>>
                                wrote:</p>
                            </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>
                                  <p class="MsoNormal"><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></p>
                                </div>
                                <div>
                                  <p class="MsoNormal"><span
                                      style="font-size:12pt"> </span></p>
                                </div>
                                <div>
                                  <p class="MsoNormal"><span
                                      style="font-size:12pt">...Pat C</span></p>
                                </div>
                              </div>
                              <p class="MsoNormal"> </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"
                                      target="_blank"
                                      moz-do-not-send="true"
                                      class="moz-txt-link-freetext">dprglist@lists.dprg.org</a>>
                                    wrote:</p>
                                </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">
                                  <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"
                                      target="_blank"
                                      moz-do-not-send="true"
                                      class="moz-txt-link-freetext">DPRGlist@lists.dprg.org</a><br>
                                    > <a
                                      href="http://lists.dprg.org/listinfo.cgi/dprglist-dprg.org"
                                      target="_blank"
                                      moz-do-not-send="true"
                                      class="moz-txt-link-freetext">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/"
                                      target="_blank"
                                      moz-do-not-send="true"
                                      class="moz-txt-link-freetext">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"
                                      target="_blank"
                                      moz-do-not-send="true"
                                      class="moz-txt-link-freetext">DPRGlist@lists.dprg.org</a><br>
                                    <a
                                      href="http://lists.dprg.org/listinfo.cgi/dprglist-dprg.org"
                                      target="_blank"
                                      moz-do-not-send="true"
                                      class="moz-txt-link-freetext">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"
                          target="_blank" moz-do-not-send="true"
                          class="moz-txt-link-freetext">DPRGlist@lists.dprg.org</a><br>
                        <a
                          href="http://lists.dprg.org/listinfo.cgi/dprglist-dprg.org"
                          target="_blank" moz-do-not-send="true"
                          class="moz-txt-link-freetext">http://lists.dprg.org/listinfo.cgi/dprglist-dprg.org</a></p>
                      <p class="MsoNormal"> </p>
                    </div>
                  </div>
                </blockquote>
              </div>
              <p class="MsoNormal" style="margin-left:4.8pt">_______________________________________________<br>
                DPRGlist mailing list<br>
                <a href="mailto:DPRGlist@lists.dprg.org" target="_blank"
                  moz-do-not-send="true" class="moz-txt-link-freetext">DPRGlist@lists.dprg.org</a><br>
                <a
                  href="http://lists.dprg.org/listinfo.cgi/dprglist-dprg.org"
                  target="_blank" moz-do-not-send="true"
                  class="moz-txt-link-freetext">http://lists.dprg.org/listinfo.cgi/dprglist-dprg.org</a></p>
              <p class="MsoNormal"> </p>
            </div>
          </div>
        </blockquote>
      </div>
      <br>
      <fieldset class="moz-mime-attachment-header"></fieldset>
      <pre class="moz-quote-pre" wrap="">_______________________________________________
DPRGlist mailing list
<a class="moz-txt-link-abbreviated" href="mailto:DPRGlist@lists.dprg.org">DPRGlist@lists.dprg.org</a>
<a class="moz-txt-link-freetext" href="http://lists.dprg.org/listinfo.cgi/dprglist-dprg.org">http://lists.dprg.org/listinfo.cgi/dprglist-dprg.org</a>
</pre>
    </blockquote>
    <p><br>
    </p>
  </body>
</html>