Search code examples
arduinoarduino-ide

Arduino - For Loop with Array not working


I have a problem with for loops and an array in Arduino IDE.

  • test1 does not work
  • test2 does work
  • test3 does work

How can I get test1 to work?

void test1(){
    for(int i=1; i<5; i++) {
    individualPixels[i]==1;
  }
}
void test2(){
    individualPixels[1]=1;
    individualPixels[2]=1;
    individualPixels[3]=1;
    individualPixels[4]=1;
  }
}
void test3(){
    for(int i=1; i<5; i++) {
    Serial.println(individualPixels[i]); //prints out 0 4 times
  }
}

Solution

  • You're not actually assigning anything in test1, you're testing for equality (individualPixels[i]==1 should be individualPixels[i] = 1, note the single equality sign).

    Also, as other commenters mentioned, C/C++ uses zero based indexing.