1
votes

I am using a 3 LED sequencer circuit like below with my Arduino Uno R3.

Enter image description here

LED 1 connected to pin 2, LED 2 to pin 3, LED 3 to pin 4. The R1/R2/R3 are 330 ohm, 1/4 W each.

The code is:

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);
    }
}

This seems to work fine in the begining. The LEDs blink on/off in sequence 13 times and then the LED connected to pin 2 stays on. When I re-upload my sketch or even click any menu items on the IDE, the loop restarts.

Why is this happening, is this due to some noise in the circuit?

P.S.: On the 4th iteration of the loop() it seems that the LED connected to pin 4 stays on for almost 200 ms instead of 100 ms, just before the 13th iteration the on board TX LED flashes once.

1

1 Answers

4
votes
int thisPin = 0; thisPin < (sizeof(myPins)); thisPin++

Huh, nope. Read how the sizeof() operator works. You want sizeof(myPins) / sizeof(myPins[0]) instead.