8 LED Chase on Arduino Mega — Arduino Mega simulation

Eight LEDs on Mega pins 2-9 with per-LED series resistors and a chasing blink sketch.

An interactive Arduino Mega circuit simulation you can run free in your browser on Velxio, by kr-moumen1.

Components

Sketch code

uint8_t leds[8] = {2, 3, 4, 5, 6, 7, 8, 9};

void setup() {
  for (int i = 0; i < 8; i++) {
    pinMode(leds[i], OUTPUT);
  }
}

void loop() {
  // Each round, the moving LED stops one position earlier
  for (int filled = 0; filled < 8; filled++) {
    int target = 7 - filled;  // where the moving LED will settle

    for (int i = 0; i <= target; i++) {
      digitalWrite(leds[i], HIGH);
      delay(100);

      if (i < target) {
        digitalWrite(leds[i], LOW);  // keep moving
      }
      // at i == target it stays on
    }
  }

  delay(1000);  // hold when completely full

  // Reset and start over
  for (int i = 0; i < 8; i++) {
    digitalWrite(leds[i], LOW);
  }
  delay(500);
}

More projects by kr-moumen1