Need LEDs to reset with a timer

'''
I've made a row of LEDs pins 2-7, I've gotten them to all turn on and then reset back and forth. But it seems to reset too fast, the second last LED turns on only for a fraction of a second, and the last doesn't seem to turn on at all.

In my mind I'm thinking.
If led 7 is on for 100ms, led = 2
To allow for the program to not reset so quick, how would I do this? And is there a better way?
'''

<
unsigned long previousTime = 0;
int led = 2;
long interval = 500;
void setup() {
for (int x = 2; x < 8; x++) {
pinMode(x, OUTPUT);
}
}
void loop() {
unsigned long currentTime = millis();
if (currentTime - previousTime > interval) {
previousTime = currentTime;
digitalWrite(led, HIGH);
led++;
if (led == 7) {
for (int x = 2; x < 8; x++) {
digitalWrite(x, LOW);
}
led = 2;
previousTime = currentTime;
}
}
}

In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.

Use the </> icon from the ‘reply menu’ to attach the copied sketch.


  previousTime = currentTime;
  digitalWrite(led, HIGH);
  led++;

  if (led == 8) 
  {
     for (int x = 2; x < 8; x++) 
     {
         digitalWrite(x, LOW);
     }

   led = 2;
   }

}

for illustration, not tested.

unsigned long previousTime = 0;
int led = 2;
long interval = 500;
int LEDcount = 6;
int arrayLEDpins[6] = {2, 3, 4, 5, 6, 7};
bool Tick = true;
void setup() {
  for (int x = 0; x >= LEDcount; x++)
  {
    pinMode( arrayLEDpins[x], OUTPUT );
  }
}
//
int currentLED = 0;
//
void loop()
{
  unsigned long currentTime = millis();
  if ( currentTime - previousTime >= interval )
  {
    if ( Tick )
    {
      digitalWrite(arrayLEDpins[currentLED], HIGH);
      currentLED++;
      previousTime = millis();
      if ( currentLED >= LEDcount )
      {
        Tick = !Tick;
      }
    } else {
      digitalWrite(arrayLEDpins[currentLED], LOW);
      currentLED--;
previousTime = millis();
      if ( currentLED == 0 )
      {
        Tick = !Tick;
      }
    }
  }
}

This should work...

unsigned long previousTime = 0;
unsigned long interval = 500;
int led = 2;


void setup()
{
  for (int x = 2; x < 8; x++)
  {
    pinMode(x, OUTPUT);
  }

  previousTime = millis();
}


void loop()
{
  unsigned long currentTime = millis();

  if (currentTime - previousTime > interval)
  {
    previousTime = currentTime;

    if (led == 8)
    {
      for (int x = 2; x < 8; x++)
      {
        digitalWrite(x, LOW);
      }
      led = 2;
    }
    else
    {
      digitalWrite(led++, HIGH);
    }
  }
}
1 Like

Your topic was moved to its current location as it is more suitable.

Could you also take a few moments to Learn How To Use The Forum.

It will help you get the best out of the forum in the future.

Thank you

Thank you, I didn't know < meant less then, instead I ended up using <=7. Feel a little silly

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.