1

I am trying to blink an Array of LEDs hooked up to a pin on the Arduino.

When I upload the code onto my Arduino :

//array of pins
int allLEDPins[4] = {2, 3, 4, 5};

//the chase function
void Chaster(int* anArray) {
  for (int i = 0; i < 5; i++) {
    digitalWrite(allPins[i], HIGH);
    delay(200);
    digitalWrite(allPins[i], LOW);
    delay(200);
  }
}

//setup pins
void setup() {
  pinMode(allPins[0], OUTPUT);
  pinMode(allPins[1], OUTPUT);
  pinMode(allPins[2], OUTPUT);
  pinMode(allPins[3], OUTPUT);
}

void loop() {
  Chaster(allLEDPins);
}

The loop function does not loop. I am using an Arduino Zero in Arduino IDE 1.6.8 on my Windows 10 machine. Thank you in advance.

1 Answer 1

2

You are accessing an index out of range in the for loop inside the function Chaster. Note that your array allLEDPins have only 4 elements and you tried to access allLEDPins[4] while the last element is allLEDPins[3]. This causes an error during running time.

In order to fix that, replace for (int i = 0; i < 5; i++) by for (int i = 0; i < 4; i++)

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. By changing the for loop, it all worked out!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.