How to Program Moving LEDs with an LED Strip

This is part 2 of this LED strip tutorial. Feel free to look at the previous one which is the solid color tutorial. We’re now going to dive into a slightly more challenging version of our LED program.

Requirements

  • LED strip
  • Arduino
  • Adafruit Neopixel Library (You can find this in the library manager of the Arduino IDE)
  • 3 wires

Schematic

Blue – Digital Pin
Red – 5 volts
Black – Ground

Traveling LEDs

#include <Adafruit_NeoPixel.h>
#define total 40
#define travelValue 3
Adafruit_NeoPixel ledstrip(total, 2,NEO_GRB + NEO_KHZ800); 

For this tutorial, I’m going to use a total for the amount of LEDs on the strip and travel value for the number of LEDS you want to travel through the LED strip. Instantiate the “ledstrip” object

void setup() {

  ledstrip.begin();

}

Allow the program to begin LED instantiation by doing “ledstrip.begin()”

void loop() {
    for(int x = 0;x <= total+travelValue;x++)
    {
    
      ledstrip.setPixelColor(x,255,0,0);//pixelnumber-1,,r,g,b); //red color
      ledstrip.setPixelColor(x-travelValue,0,0,0);//pixelnumber-1,,r,g,b); //No color
      delay(100);
      ledstrip.show(); //YOU NEED THIS TO SHOW LEDs
    }
}

What I did here was I subtracted x from travelValue. What this does is that as the next led lights up red, the previous one turns blank. Now with travelValue added to total, this creates a seamless loop when it returns back to the beginning of the LED strip.

Final Code

#include <Adafruit_NeoPixel.h>
#define total 10
#define travelValue 3
Adafruit_NeoPixel ledstrip(total, 2,NEO_GRB + NEO_KHZ800); 
//(numoftotalpixels,pinnumber(digital),ledtype,ledbitrate)
void setup() {

  ledstrip.begin();

}

void loop() {
    for(int x = 0;x <= total+travelValue;x++)
    {
    
      ledstrip.setPixelColor(x,255,0,0);//pixelnumber-1,,r,g,b);
      ledstrip.setPixelColor(x-travelValue,0,0,0);//pixelnumber-1,,r,g,b);
      delay(100);
      ledstrip.show();
    }
}

Leave a comment

Your email address will not be published. Required fields are marked *