【问题标题】:Arduino LED chaser freezes after a few loops?Arduino LED 追逐者在几个循环后冻结?
【发布时间】:2013-05-13 18:26:04
【问题描述】:

我在Arduino Uno R3 上使用如下所示的 3 LED 定序器电路。

LED 1 连接到引脚 2,LED 2 连接到引脚 3,LED 3 连接到引脚 4。R1/R2/R3 为 330 ohm,每个 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 上的任何菜单项时,循环会重新启动。

为什么会发生这种情况,这是由于电路中的一些噪声造成的吗?

P.S.:在 loop() 的第 4 次迭代中,连接到引脚 4 的 LED 似乎保持亮着近 200 毫秒而不是 100 毫秒,就在第 13 次迭代之前,板载 TX LED 闪烁一次。

【问题讨论】:

    标签: arduino led


    【解决方案1】:
    int thisPin = 0; thisPin < (sizeof(myPins)); thisPin++
    

    嗯,不。阅读sizeof() 运算符的工作原理。你想要sizeof(myPins) / sizeof(myPins[0])

    【讨论】:

    • OK sizeof 返回字节。哦,我完全忘记了这一点。谢谢会调整它。
    • @mobbarley 不要那样做,&lt; 很好,否则你会遇到一个错误。
    • 我已将循环更改为:for (int thisPin = 0; thisPin &lt; (sizeof(myPins)/sizeof(int));
    猜你喜欢
    • 2018-07-06
    • 1970-01-01
    • 1970-01-01
    • 2017-10-20
    • 1970-01-01
    • 2016-06-17
    • 1970-01-01
    • 1970-01-01
    • 2021-06-14
    相关资源
    最近更新 更多