user3520669
user3520669

Reputation:

Arduino void loop does not loop

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.

Upvotes: 1

Views: 2328

Answers (1)

Arton Dorneles
Arton Dorneles

Reputation: 1709

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++)

Upvotes: 2

Related Questions