For loop running twice

Hey, the for loop after “if(led ==9)” keeps running twice, if i set the delay to 150 it only runs once but when its less than 150 the loop runs twice, here’s the code:

const int switchPin = 8;
unsigned long previousTime = 0;
int switchState = 0;
int prevSwitchState = 0;
int led=1;
long interval = 1000;
int y = 1;
int z = 0;
void setup() {
  for(int x=2;x<8;x++){
    pinMode(x, OUTPUT);
  }
  pinMode(switchPin, INPUT); 
  Serial.begin(9600);
}
void loop() {
  unsigned long currentTime = millis();
  if( currentTime - previousTime > interval){
     previousTime = currentTime;

     digitalWrite(led,HIGH);
     led++;
  }
     if(led == 9){ 
      digitalWrite(2,LOW);
      digitalWrite(3,LOW);
      digitalWrite(4,LOW);
      digitalWrite(5,LOW);
      digitalWrite(6,LOW);
      digitalWrite(7,LOW);
      
      delay(100); 
          
        for(int y=1;y<8;y++){
      digitalWrite(y,HIGH);
      digitalWrite(y+1,HIGH);
      digitalWrite(y-1,LOW);
      delay(150);
      z++;
      
      Serial.print(y);
      Serial.println(z); 
      
      }
        if(z ==7){
       delay(100);
       digitalWrite(7,LOW);  
        }
      }        
  switchState = digitalRead(switchPin);
  if(switchState != prevSwitchState){
    for( int x = 2;x<8;x++){
      digitalWrite(x, LOW);
    }  
  led=0;
  previousTime = currentTime;
  }
  prevSwitchState = switchState;
}

The easier you make it to read and copy the code the more likely it is that you will get help

Please follow the advice given in the link below when posting code , use code tags and post the code here

With short delay()s in the for loop it will run fast enough that the 1000 millisecond interval has not passed. As a result led will still be 9 and the for loop will run again

Oh now i see it, thanks! The aim was that when led == 9 the led blinks would only happen once. Do you have any idea how it’d be the easiest to get the for loop to only run once when led == 9 despite what the interval is?

What exactly do you want to happen ?

You could increment led after the for loop to prevent it running again

I do something similar…
I define an array - perhaps 10 members, then use 5 of the members to indicate LED on/off states.
That way, if I cycle through the array while blinking the led, it’s quite easy to blink long/short patterns with a gap between cycles, or counts… e.g. 1/2/3/4 blinks depending on what I’m trying to indicate.

The fundamental idea of the code was that a LED will turn on after a certain time (long interval), until every LED is on and then (led==9) and then all the LEDs go LOW and the for loop kicks in blinking the LEDs. The problem at the moment is that the led value is 9 for a long period of time depending on the length of the interval and so the for loop keeps running as long as (led == 9) and not just once.