Need Help... Trying to only have ALL LED Blink

int timer = 100;           // The higher the number, the slower the timing.

void setup() {
  // use a for loop to initialize each pin as an output:
  for (int thisPin = 2; thisPin < 8; thisPin++)  {
    pinMode(thisPin, OUTPUT);      
  }
}

void loop() {
  // loop from the lowest pin to the highest:
  for (int thisPin = 2; thisPin < 8; thisPin++) { 
    // turn the pin on:
    digitalWrite(thisPin, HIGH);   
    delay(timer);                  
    // turn the pin off:
    digitalWrite(thisPin, LOW);    
  }

  // loop from the highest pin to the lowest:
  for (int thisPin = 7; thisPin >= 2; thisPin--) { 
    // turn the pin on:
    digitalWrite(thisPin, HIGH);
    delay(timer);
    // turn the pin off:
    digitalWrite(thisPin, LOW);
  }
}

Im trying to modify this to where all 6 LEDs come on and then off simultaneously. I am fairly new to this, so any explanation will help

Separate this into two loops with a delay between.

  • The first loop turns all pins high.
  • delay
  • The second loop turns all pins low.

what do you think this is doing to your led before going to the next pin in the for loop?

You can try this:

constexpr int timer {500};
constexpr byte START_PIN {2};
constexpr byte LAST_PIN {7};

void toggleLEDs(byte, byte, byte);

void setup() {
  for (int thisPin = START_PIN; thisPin <= LAST_PIN; thisPin++)  {
    pinMode(thisPin, OUTPUT);
  }
}

void loop() {
  toggleLEDs(HIGH, START_PIN, LAST_PIN);
  delay(timer);
  toggleLEDs(LOW, START_PIN, LAST_PIN);
  delay(timer);
}

void toggleLEDs(byte level, byte startPin, byte lastPin ) {
  for (byte thisPin = startPin; thisPin <= lastPin; ++thisPin) {
    digitalWrite(thisPin, level);
  }
}

The Arduino is very fast so when you don't put a delay in a loop it looks like everything happens simultaneously:

void loop()
{
  // loop from the lowest pin to the highest:
  for (int thisPin = 2; thisPin < 8; thisPin++)
  {
    // turn the pin on:
    digitalWrite(thisPin, HIGH);
  }

  delay(timer);

  // loop from the lowest pin to the highest:
  for (int thisPin = 2; thisPin < 8; thisPin++)
  {
    // turn the pin off:
    digitalWrite(thisPin, LOW);
  }

  delay(timer);
}

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