我在我的Arduino Uno R3 上使用如下所示的 3 LED 定序器电路。
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 保持亮起。当我重新上传我的草图甚至单击 IDE 上的任何菜单项时,循环会重新启动。
为什么会发生这种情况,这是由于电路中的一些噪声造成的吗?
PS:在第 4 次迭代中,loop()
连接到引脚 4 的 LED 似乎保持亮着近 200 毫秒而不是 100 毫秒,就在第 13 次迭代之前,板载 TX LED 闪烁一次。