Introduction To Coding Class

Lesson #5: Scrolling LED

The Pattern Emerges

The maintenance tunnels beneath Cogsworth City hum with more than just ventilation fans tonight. Down here, where the clockwork mechanisms that power the entire metropolis tick in perfect synchronization, you've discovered something extraordinary. The old engineering manuals spoke of sequential activation protocols, patterns that could control multiple systems with elegant efficiency.

Your HERO Board sits before you, connected to five LEDs that stretch across your breadboard like tiny streetlights. But these aren't just lights. In Cogsworth City, everything follows patterns. The streetlamps that illuminate the cobblestone paths don't just flicker on randomly. They cascade in waves, creating flowing rivers of light that guide citizens through the winding districts.

Tonight, you're about to master the art of the for loop. Not just any loop, but one that can make light dance, systems cascade, and bring the fluid motion of Cogsworth's greatest inventions under your control. The gears are already turning. Time to make them sing in harmony.

What You'll Learn

When you finish this lesson, you'll be able to:

  • Control multiple LEDs with elegant for loops instead of repetitive code
  • Create smooth cascading light patterns that flow in both directions
  • Use loop counters to iterate through pin numbers systematically
  • Build sequential timing effects that create mesmerizing displays
  • Understand how iteration makes your code scalable and maintainable

This is your gateway to controlling not just five LEDs, but potentially hundreds of outputs with clean, efficient code.

Understanding For Loops

Imagine you're a conductor standing before an orchestra, but instead of musicians, you have a row of streetlights. You could walk to each light individually, flip its switch, wait a moment, then move to the next one. That's exactly what you've been doing with your previous LED code – writing separate digitalWrite() commands for each pin.

But what if you had a more elegant approach? What if you could simply say "Starting with light number 3, turn on each light in sequence through light number 7, waiting 100 milliseconds between each one"? That's the magic of a for loop.

A for loop is like giving your microcontroller a simple recipe: "Start counting at this number, keep going until you reach that number, and for each count, do this specific task." Instead of writing five separate digitalWrite() commands, you write one command that automatically repeats with different pin numbers.

The real power emerges when you want to control not just five LEDs, but fifty. Or when you want to change the pattern – your loop adapts instantly. It's the difference between manually winding fifty individual pocket watches versus building one master clockwork mechanism that synchronizes them all.

Wiring Your LED Sequence

We're connecting five LEDs to pins 3 through 7. Why these pins specifically? They're consecutive digital pins that give us a perfect range for our loop to iterate through.

  1. Connect the positive leg (longer leg) of LED 1 to digital pin 3
  2. Connect the positive leg of LED 2 to digital pin 4
  3. Connect the positive leg of LED 3 to digital pin 5
  4. Connect the positive leg of LED 4 to digital pin 6
  5. Connect the positive leg of LED 5 to digital pin 7
  6. Connect all negative legs (shorter legs) to ground through 220Ω resistors

Each resistor prevents too much current from flowing through the LEDs. Think of them as pressure valves in Cogsworth's steam system – they ensure just the right amount of electrical "pressure" reaches each light.

The Complete Code

Here's the full program that creates our cascading LED effect. Copy this to your IDE, then we'll break down exactly how it works: