Arduino LED追逐器在几次循环后冻结?

时间:2013-05-13 18:26:04

标签: arduino electronics led

我正在使用如下所示的3 LED音序器电路和我的Arduino Uno R3。

Enter image description here

LED 1连接到引脚2,LED 2连接到引脚3,LED 3连接到引脚4. R1 / R2 / R3为330欧姆,每个1/4 W。

代码是:

int myPins[] = {2,3,4};           // Set pin array to pins 2 through 4

void setup()
{
    for (int thisPin = 0; thisPin < (sizeof(myPins)); thisPin++)
    {
        pinMode(myPins[thisPin],OUTPUT); // Set each pin in the array to OUTPUT mode.
    }
}

void loop()
{
    for (int thisPin = 0; thisPin < (sizeof(myPins)); thisPin++)    // Loop every pin and switch ON & OFF.
    {
        digitalWrite(myPins[thisPin], HIGH);  // Set LED ON.
        delay(100);                           // Keep it ON for 100 ms.
        digitalWrite(myPins[thisPin], LOW);   // Set LED OFF for 50 ms and then goto next one.
        delay(50);
    }
}

这似乎在开始时工作正常。 LED依次闪烁13次,然后连接到引脚2的LED保持亮起。当我重新上传sketch或者甚至点击IDE上的任何菜单项时,循环重新开始。

为什么会发生这种情况,这是因为电路中有些噪音?

PS:在loop()的第4次迭代中,连接到引脚4的LED似乎保持接近200 ms而不是100 ms,就在第13次迭代之前,板载TX LED闪烁一次。 / p>

1 个答案:

答案 0 :(得分:4)

int thisPin = 0; thisPin < (sizeof(myPins)); thisPin++
嗯,不,不。阅读sizeof()运算符的工作原理。您想要sizeof(myPins) / sizeof(myPins[0])

相关问题