【发布时间】:2013-04-26 17:53:53
【问题描述】:
我在 Arduino 中编写了循环通过 3 个 LED 彩色灯的代码,但它似乎(代码)容易出错,所以我试图想出一种新的方式来编写它。由于复杂性,我将坚持使用我正在尝试做的 phesdo 代码。这里是:
If (red LED isn't max and green LED is 0 and blue LED is 0)
{inc red LED; update dot matrix}
If (red LED is max and green LED isn't max and blue LED is 0)
{inc green LED; update dot matrix}
If ((red LED is/has-been max but not 0 ) and green LED is max and blue LED is 0)
{dec red; update dot matrix}
If (red LED is 0 and green LED is max and blue LED isn't max)
{inc blue; update dot matrix}
If (red LED is 0 and (green LED is/has-been max but not 0) and blue LED is max)
{dec green; update dot matrix}
If (red LED isn't Max and green LED is 0 and blue is Max )
{inc red; update dot matrix}
If (red LED is Max and green LED is 0 and (blue LED is/has-been Max but not 0))
{dec blue; update dot matrix}
Update LED Driver;
注意:对于视觉来说,它是一个红色->橙色->绿色->蓝绿色->蓝色->粉色->重复的色轮
需要注意的是,所有这些都在一个循环中,该循环仅在退出以获取其他数据之前运行一次。然后它必须返回到这个循环并记住它离开的颜色位置。否则,将所有这些包装在 for 循环中并线性执行它会很容易。因为它必须增加或减少一种颜色,如果您愿意,请了解它的颜色位置,更新 LED 驱动程序,然后返回增加或减少记住它停止的位置。那么有没有人有更好的代码方法,伪风格,除了这种复杂的 if 语句风格,我还可以使用。
【问题讨论】:
-
“所有这些都在一个循环中,只运行一次”那么它在循环中的意义何在?
-
不抱歉这个循环,但我必须在我不能只是坐在它更新颜色之前和之后做一些事情。我需要稍微改变颜色,更新 LED 驱动器,然后做其他事情,然后再回到这个,再更新一点,重复。
-
不要使用循环,使用计时器。