title: Arduino function to fade an RGB from one color to the next
body: <p>For my Arduino project I have a <a href="https://learn.adafruit.com/downloads/pdf/adafruit-neopixel-uberguide.pdf" rel="nofollow">Neopixel RGB Strip</a> with 72 LED's.</p>
  
  <p>I can successfully change the colour of any of the LED's (at the moment I'm only setting the first one 0 for testing purposes) so I know my wiring isn't the problem here, it's my coding.</p>
  
  <p>What I would like to do is be able to select a color and then another color and have the first color fade to the next color and so forth (much like the LIFX bulbs behave when using the iPhone application).</p>
  
  <p>This is what I have at the moment:</p>
  
  <p>I am logging output of all the variables to give you an indication of what's going on. I'm not 100% sure on where I'm going wrong or whether there's a much easier way to do what I'm after (I'm open to suggestions).</p>
  
  <p>The function takes a parameter called <code>command</code>, which is a string separated by commas:</p>
  
  <p>e.g. <code>255, 0, 0</code> (RED) or <code>0, 255, 0</code> (GREEN).</p>
  
  <pre><code>/*******************************************************************************
   * Function Name  : tinkerSetColour
   * Description    : Sets the strip with the appropriate colour
   * Input          : Pin and value
   * Output         : None.
   * Return         : 1 on success and a negative number on failure
   *******************************************************************************/
  int Rstart = 0, Gstart = 0, Bstart = 0;
  int Rnew = 0, Gnew = 0, Bnew = 0;
  
  int tinkerSetColour(String command)
  {
      sprintf(rgbString, "Rstart %i, Gstart %i, Bstart %i", Rstart, Gstart, Bstart);
      Spark.publish("rgb", rgbString);
  
      sprintf(rgbString, "Rnew %i, Gnew %i, Bnew %i", Rnew, Gnew, Bnew);
      Spark.publish("rgb", rgbString);
  
      // Clear strip.
      strip.show();
  
      int commaIndex = command.indexOf(',');
      int secondCommaIndex = command.indexOf(',', commaIndex+1);
      int lastCommaIndex = command.lastIndexOf(',');
  
      int red = command.substring(0, commaIndex).toInt();
      int grn = command.substring(commaIndex+1, secondCommaIndex).toInt();
      int blu = command.substring(lastCommaIndex+1).toInt();
  
      int Rend = red, Gend = grn, Bend = blu;
  
      sprintf(rgbString, "Rend %i, Gend %i, Bend %i", Rend, Gend, Bend);
      Spark.publish("rgb", rgbString);
  
      // Larger values of 'n' will give a smoother/slower transition.
      int n = 200;
      for (int i = 0; i &lt; n; i++)
      {
          Rnew = Rstart + (Rend - Rstart) * i / n;
          Gnew = Gstart + (Gend - Gstart) * i / n;
          Bnew = Bstart + (Bend - Bstart) * i / n;
  
          // Set pixel color here.
          strip.setPixelColor(0, strip.Color(Rnew, Gnew, Bnew));
      }
  
      sprintf(rgbString, "Rnew %i, Gnew %i, Bnew %i", Rnew, Gnew, Bnew);
      Spark.publish("rgb", rgbString);
  
      Rstart = red, Gstart = grn, Bstart = blu;
  
      sprintf(rgbString, "Rstart %i, Gstart %i, Bstart %i", Rstart, Gstart, Bstart);
      Spark.publish("rgb", rgbString);
  
      return 1;
  }
  </code></pre>
  
  <p>The problem is the colors are not fading between themselves.</p>
  
  <p>Apologies if any of this is confusing. I can provide more information if necessary.</p>
  
  <p>Here's the <strong>output</strong> selecting <strong>RED</strong> to begin with:</p>
  
  <pre><code>Rstart 0, Gstart 0, Bstart 0
  Rnew 0, Gnew 0, Bnew 0
  Rend 255, Gend 0, Bend 0
  Rnew 253, Gnew 0, Bnew 0
  </code></pre>
  
  <p>Here's the <strong>output</strong> selecting <strong>GREEN</strong> directly afterwards:</p>
  
  <pre><code>Rstart 255, Gstart 0, Bstart 0
  Rnew 253, Gnew 0, Bnew 0
  Rend 0, Gend 255, Bend 0
  Rnew 2, Gnew 253, Bnew 0
  </code></pre>
  
  <p>And then the <strong>output</strong> selecting <strong>BLUE</strong> after that:</p>
  
  <pre><code>Rstart 0, Gstart 255, Bstart 0
  Rnew 2, Gnew 253, Bnew 0
  Rend 0, Gend 23, Bend 255
  Rnew 0, Gnew 25, Bnew 253
  </code></pre>