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); 
void loop() {
  unsigned long currentTime = millis();
  if( currentTime - previousTime > interval){
     previousTime = currentTime;

     if(led == 9){ 
        for(int y=1;y<8;y++){
        if(z ==7){
  switchState = digitalRead(switchPin);
  if(switchState != prevSwitchState){
    for( int x = 2;x<8;x++){
      digitalWrite(x, LOW);
  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.