What is wrong with my for loop?

I want the code to turn on some leds in order, then blink them and then loop it. I cannot find my misstake but when i turn on the code 4 leds immediately turn on and sometimes blink. What are my misstakes?

int allPins[] = {2,3,4,5,6,7,8,9};

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
  setPinMode(OUTPUT);

  // put your main code here, to run repeatedly:
for(int i = 0; i < 9; i++){
    digitalWrite(i, HIGH);
    delay;1000;
    Serial.print(i);

    
}
{
  delay(10);
  setPinStates(LOW);  
  delay(20);
  setPinStates(HIGH); 
  delay(20);
  setPinStates(LOW);  
  
  }
  }
void setPinStates(byte state)
{
  byte numberOfPins = sizeof(allPins) / sizeof(allPins[0]);
  for (int p = 0; p < numberOfPins; p++)
  {
    digitalWrite(allPins[p], state);
    
  }
}

void setPinMode(byte mode)
{
  byte numberOfPins = sizeof(allPins) / sizeof(allPins[0]);
  for (int p = 0; p < numberOfPins; p++)
  {
    digitalWrite(allPins[p], mode);
  }
}
    delay; 1000;

maybe...

delay(1000);

and

    delay(10);
    setPinStates(LOW);
    delay(20);
    setPinStates(HIGH);
    delay(20);
    setPinStates(LOW);

10ms, 20ms... you won't see that with the naked eye. Maybe try 500ms.

so sloppy :slight_smile: thanks. However, the 4 leds are still turned on after the first iteration even though the serial monitor shows the correct numbers now.

Post your code.. where do you write to the monitor apart from the statement below?

    Serial.print(i);

nowhere, I just used it to check if the iteration of the pins went as it should.

int allPins[] = {2,3,4,5,6,7,8,9};

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
  setPinMode(OUTPUT);
  int i =0;

  // put your main code here, to run repeatedly:
for(int i = 0; i < 9; i++){
    digitalWrite(allPins[i], HIGH);
    delay(1000);
    Serial.print(i);

    
}
{
  delay(10);
  setPinStates(LOW);  
  delay(100);
  setPinStates(HIGH); 
  delay(100);
  setPinStates(LOW);  
  
  }
  }
void setPinStates(byte state)
{
  byte numberOfPins = sizeof(allPins) / sizeof(allPins[0]);
  for (int p = 0; p < numberOfPins; p++)
  {
    digitalWrite(allPins[p], state);
    
  }
}

void setPinMode(byte mode)
{
  byte numberOfPins = sizeof(allPins) / sizeof(allPins[0]);
  for (int p = 0; p < numberOfPins; p++)
  {
    digitalWrite(allPins[p], mode);
  }
}

The pins are still not turning on in a sequence. The blink part works and the i iterates but all leds are lit after the first iteration.

I found it!
i added

 setPinStates(LOW);

to the start of the loop :slight_smile: thank you for your time friend!

btw.. this is not a good idea...

  int i = 0;

  // put your main code here, to run repeatedly:
  for (int i = 0; i < 9; i++) {

declaring i twice... asking for trouble. get rid of the first one.

and... put this in setup()

  setPinMode(OUTPUT);

Why the magic number 9?

Your functions don't use magic numbers.

and if that doesn't convince you...

you still have undefined behaviour when read array out of bounds