An Array of Leds controlled by a button

I am very new to using Arduino and wanted to create the same effect as the video.

Could anyone help me create the same effect in the above video?

So far I have managed the following :

const int buttonPin = 2;
int timer = 100; // The higher the number, the slower the timing.
int ledPins = {
6, 9, 10, 11 }; // an array of pin numbers to which LEDs are attached
int pinCount = 4; // the number of pins (i.e. the length of the array)

void setup() {
// the array elements are numbered from 0 to (pinCount - 1).
// use a for loop to initialize each pin as an output:
for (int i = 0; i < pinCount; i++) {
pinMode(ledPins*, OUTPUT); *

  • }*
    void loop() {
  • buttonState = digitalRead(buttonPin);*
  • // loop from the lowest pin to the highest:*
  • for (int i = 0; i < pinCount; i++) {*
  • // turn the pin on:*
    _ digitalWrite(ledPins*, HIGH); _
    delay(timer); _
    // turn the pin off:_
    _ digitalWrite(ledPins, LOW);
    // loop from the highest pin to the lowest:
    for (int i = pinCount - 1; i >= 0; i–) {
    // turn the pin on:
    digitalWrite(ledPins, HIGH);
    // turn the pin off:
    digitalWrite(ledPins, LOW);


What you want yo do can be broken down into the following steps:

  • Store the direction in which you are going (i.e. the direction in which you are lighting the LEDs use 1 for Left to right and -1 for Right to left.)
  • Declare a variable to store the current position of your lit LED
  • When the button is pressed add the direction variable to the position variable to get the new position. For example if the current LED is the 4th LED and you are going from Right to left you do: positon + direction = 4 + (-1) = 3.
  • Use a simple if statement to check if your position variable reaches the first or the last LED, if it does simply reverse the direction by multiplying it by (-1)
  • Switch off every led and turn on your required LED

Now, Code it up and let me know if it works. :)


I am quite new to programming so I will give it a try.