Sequence and Loops: The LED Chaser
The LED Chaser (also known as the Knight Rider sweep or Cylon Eye) is the perfect introduction to the power of for loops and arrays in C++. When blinking one LED is too boring, you learn to blink ten at astonishing speeds.
This is a very interesting and easy project as in this project you only need Arduino Uno or Arduino Nano, LEDs of the same colour or of different colours and some connecting wires. You can connect almost 10 to 12 LEDs with Arduino so that you get more interesting results for your hard work.
Escaping "Spaghetti Code"
A beginner might code a 5-LED chaser by writing 30 lines of code: digitalWrite(3, HIGH); delay(50); digitalWrite(3, LOW); digitalWrite(4, HIGH);...
This is highly inefficient. If you want to change the delay, you have to change it in 10 different places!
The Power of Arrays
- Storage: You store the pin numbers in an integer array:
int pins[] = {2, 3, 4, 5, 6};. - The Loop: You use a concise 3-line loop.
for (int i=0; i<5; i++) { digitalWrite(pins[i], HIGH); delay(50); digitalWrite(pins[i], LOW); } - Iteration: The loop rapidly substitutes the variable
i(0, 1, 2, 3) inside the brackets, executing the sweep perfectly. If you want it to bounce back, you just add a secondforloop that counts backward from 4 to 0 (i--).
Required Materials
- Arduino Uno/Nano: The sequence engine.
- 5 to 10 LEDs: Any color.
- 220-ohm Resistors (x10): Every single LED must have its own resistor, or you risk blowing out the Arduino pins.
- Large Breadboard.
Please watch the complete video to know how it works……
And like, share, comments on this video if you are new subscribe to our channel and follow on Arduino
I hope you enjoyed my project.
Thanks!