title: Arduino C++ for loop exiting early?
body: <p>I've been staring at this for most of the afternoon and am stumped. The problem seems so simple that I'm sure it will be a real Doh! face palm moment when I crack it.</p>
  <p>Some background:</p>
  <p>I'm creating a little project to use an Arduino to read in Infrared signals from my remote controllers and send them to a Python program I'm writing.</p>
  <p>I send the sample command to the Arduino as a JSON string and use the Arduinojson library to deserialise it. That works fine. My sampling method captures the signal and writes it to an int array.</p>
  <p>Where I'm struggling is converting that array to a JSON string. I tried using the same library for serialising it but it used a lot of memory, so I'm  trying to write a loop to convert the ints to string form and construct a JSON string manually from them.</p>
  <p>My signal capture method:</p>
  <pre><code>// Capture an IR signal
  bool capture() {
    bool captured = false;
    if(myReceiver.getResults()) {
      int index = 0;
      for(bufIndex_t i = 1; i &lt; recvGlobal.recvLength; i++){
        irBuffer[index++] = recvGlobal.recvBuffer[i];
  
        // Debug
        Serial.print(i);
        Serial.print(&quot; - &quot;);
        Serial.println(recvGlobal.recvBuffer[i]);
      }
      irBuffer[index] = 0;
      captured = true;
    }
    myReceiver.enableIRIn();
    return captured;
  }
  </code></pre>
  <p>The debug output from that method shows I'm capturing 67 pulses:</p>
  <pre><code>1 - 4400
  2 - 4600
  3 - 550
  4 - 600
  5 - 550
  6 - 550
  7 - 550
  8 - 1650
  9 - 550
  10 - 1700
  11 - 550
  12 - 600
  13 - 550
  14 - 1650
  15 - 600
  16 - 550
  17 - 550
  18 - 600
  19 - 500
  20 - 600
  21 - 550
  22 - 550
  23 - 550
  24 - 1700
  25 - 550
  26 - 1650
  27 - 550
  28 - 600
  29 - 550
  30 - 1700
  31 - 550
  32 - 600
  33 - 500
  34 - 600
  35 - 550
  36 - 550
  37 - 550
  38 - 1700
  39 - 550
  40 - 1650
  41 - 550
  42 - 1700
  43 - 550
  44 - 1700
  45 - 550
  46 - 600
  47 - 500
  48 - 600
  49 - 550
  50 - 600
  51 - 500
  52 - 1700
  53 - 550
  54 - 600
  55 - 550
  56 - 550
  57 - 550
  58 - 600
  59 - 500
  60 - 600
  61 - 500
  62 - 1700
  63 - 600
  64 - 1650
  65 - 550
  66 - 1700
  67 - 550
  </code></pre>
  <p>My attempt at converting this to a JSON string:</p>
  <pre><code>#define MAX_IR 100
  
  ...
  
  
  if (RETURN_IR_SIGNAL) {
      char out[MAX_IR];
      char tmp[10];
      strcpy(out, &quot;{\&quot;signal\&quot;:[&quot;);
      for (int i = 0; i &lt;= MAX_IR; i++) {
        if (irBuffer[i] == 0) {
          break;
        }
        // Debug statements
        Serial.print(&quot;out = &quot;);
        Serial.println(out);
  
        sprintf(tmp, &quot;%d,&quot;, irBuffer[i]);
        strcat(out, tmp);
      }
      strcat(out, &quot;]}&quot;);
  
      // Debug
      Serial.println(&quot;Finished&quot;);
  
      RETURN_IR_SIGNAL = false;
    }
  </code></pre>
  <p>The debug output from that code is this:</p>
  <pre><code>out = {&quot;signal&quot;:[
  out = {&quot;signal&quot;:[4400,
  out = {&quot;signal&quot;:[4400,4600,
  out = {&quot;signal&quot;:[4400,4600,550,
  out = {&quot;signal&quot;:[4400,4600,550,600,
  out = {&quot;signal&quot;:[4400,4600,550,600,550,
  out = {&quot;signal&quot;:[4400,4600,550,600,550,550,
  out = {&quot;signal&quot;:[4400,4600,550,600,550,550,550,
  out = {&quot;signal&quot;:[4400,4600,550,600,550,550,550,1650,
  out = {&quot;signal&quot;:[4400,4600,550,600,550,550,550,1650,550,
  out = {&quot;signal&quot;:[4400,4600,550,600,550,550,550,1650,550,1700,
  out = {&quot;signal&quot;:[4400,4600,550,600,550,550,550,1650,550,1700,550,
  out = {&quot;signal&quot;:[4400,4600,550,600,550,550,550,1650,550,1700,550,600,
  out = {&quot;signal&quot;:[4400,4600,550,600,550,550,550,1650,550,1700,550,600,550,
  out = {&quot;signal&quot;:[4400,4600,550,600,550,550,550,1650,550,1700,550,600,550,1650,
  out = {&quot;signal&quot;:[4400,4600,550,600,550,550,550,1650,550,1700,550,600,550,1650,600,
  out = {&quot;signal&quot;:[4400,4600,550,600,550,550,550,1650,550,1700,550,600,550,1650,600,550,
  out = {&quot;signal&quot;:[4400,4600,550,600,550,550,550,1650,550,1700,550,600,550,1650,600,550,550,
  out = {&quot;signal&quot;:[4400,4600,550,600,550,550,550,1650,550,1700,550,600,550,1650,600,550,550,600,
  out = {&quot;signal&quot;:[4400,4600,550,600,550,550,550,1650,550,1700,550,600,550,1650,600,550,550,600,500,
  out = {&quot;signal&quot;:[4400,4600,550,600,550,550,550,1650,550,1700,550,600,550,1650,600,550,550,600,500,600,
  out = {&quot;signal&quot;:[4400,4600,550,600,550,550,55�␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀
  </code></pre>
  <p>You can see that when it gets to the 21st number in the array, it truncates the string to just a handful of numbers. Also, the fact that I don't see the final &quot;Finished&quot; message suggests that I'm maybe throwing an exception and restarting, but I have no way of seeing if that's the case.</p>
  <p>It's been over twenty years since I've written any C or C++ and so it's been an uphill battle but enjoyable. So I daresay that this is a classic rooky error, but it's eluding me.</p>
  <p>Any help much appreciated.</p>