对于我的Arduino项目,我有一个Neopixel RGB Strip和72个LED。Arduino功能将RGB从一种颜色褪色到下一个
我可以成功改变任何LED的颜色(目前我只设置第一个0作为测试目的),所以我知道我的布线不是这里的问题,这是我的编码。
我想要做的是能够选择一种颜色,然后选择另一种颜色,并使第一种颜色淡入到下一种颜色等等(很像LIFX灯泡在使用iPhone应用程序时的行为)。
这是我的时刻:
我记录所有变量的输出,给你这是怎么回事的指示。我并不是100%确定自己要出错的地方,还是有更简单的方法来做我以后的事情(我愿意接受建议)。
该函数采用一个称为command
参数,它是由逗号分隔的字符串:
例如255, 0, 0
(RED)或0, 255, 0
(绿色)。
/*******************************************************************************
* 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 < 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;
}
问题是颜色之间并没有褪色。
道歉,如果任何这是令人困惑。如有必要,我可以提供更多信息。
这里的输出选择RED开始与:
Rstart 0, Gstart 0, Bstart 0
Rnew 0, Gnew 0, Bnew 0
Rend 255, Gend 0, Bend 0
Rnew 253, Gnew 0, Bnew 0
这里的输出选择GREEN直接算账:
Rstart 255, Gstart 0, Bstart 0
Rnew 253, Gnew 0, Bnew 0
Rend 0, Gend 255, Bend 0
Rnew 2, Gnew 253, Bnew 0
然后是输出选择BLUE后:
Rstart 0, Gstart 255, Bstart 0
Rnew 2, Gnew 253, Bnew 0
Rend 0, Gend 23, Bend 255
Rnew 0, Gnew 25, Bnew 253
@gotnull我刚刚更新了我的答案与文档:) – ladislas 2014-10-14 15:33:31