05. Getting Started in Code - frc2052/2052-LED-Training GitHub Wiki

For our first program we will create a small bit of code in our main.cpp file to blink some LEDs.

#include <Arduino.h>  //include library
#include <FastLED.h> //include library
#define NUM_LEDS 255 //how many LEDs are in your strip/panel
#define DATA_PIN 2 //signal wire is connected to this pin on arduino, KnightKrawlers Arduinos are connected to 2 
#define COLOR_ORDER GRB //surprisingly, many LED strips don't use the color order Red,Green,Blue
#define CHIP_SET WS2812B //KnightKrawler uses WS2812B Leds, but there are many other options
#define BRIGHTNESS 255 //255 is maximum brightness, lower this value to limit brightness
#define VOLTS 5 //All WS2812B LED strips are 5v
#define MAX_AMPS 2500 //this value is in milliamps, our power converter allows for 3 amp at 5v. Choose a value at or bellow 3000 milliamps

CRGB g_leds[NUM_LEDS]; //create our LED array object for all our LEDs


void setup() {
  FastLED.addLeds<CHIP_SET, DATA_PIN, COLOR_ORDER>(g_leds, NUM_LEDS);
  FastLED.setMaxPowerInVoltsAndMilliamps(VOLTS, MAX_AMPS);
  FastLED.setBrightness(BRIGHTNESS);
  FastLED.clear();
  FastLED.show();
}

void loop() {
  g_leds[0] = CRGB(255,0,0); //red
  g_leds[1] = 0x00FF00; //green
  g_leds[2] = CRGB::Blue;
  FastLED.show();
  delay(1000);
  FastLED.clear();
  FastLED.show();  
  delay(1000);
}

In this code example, we are creating constants at the top of the file using the #define keyword. These are values that will not change in our program. The comments explain what each variable does.

We declare a special array object for our LEDs called g_leds. The "g_" indicates that this variable is "global" meaning that it can be used anywhere in our project. You are not required to use the "g_" prefix. This is a style preference that creates a hint for the developer to understand this variable was declared so that it can be used anywhere. The CRGB object is defined by the FastLED library.

There are two primary methods in every basic Arduino project. setup() and loop(). The setup method is called once when the program starts. The loop method is called over and over as long as the program is running.

Inside the setup method we have standard initialization code that most programs using the FastLED library will use. You can find a full explanation on the FastLED Wiki

Before we start setting the color of an LED, it is important to understand how the order of the LEDs is determined and what are Red, Green, Blue channels. If you watch this video from 2:40 to 3:05 you will see a great explanation of how data travels down the strip, which determines the order of the LEDs, and a close up of how RGB values are combined to create the desired color.

In our first loop method we will simply blink the first 3 LEDs in our strip/panel. You can see we use three different methods of setting the color for LEDs. You can set each color using the RGB raw values from 0 to 255. For the LED in position 0, we set CRGB(255,0,0), which is turning the red channel to full brightness, and turning off the green and blue channels.

For the LED in position 1, we are using a hex value to set the color. This method will look familiar to anyone who is familiar with setting colors in HTML or working in a graphics program for drawing images. There are many online color pickers available, but getting the color you want just right will depend on your brightness and may require some tweaking.

For the LED in position 2, we use a built-in color code defined the the FastLED library.

There are many ways to set the color of an LED and these are just 3 ways. For example, if you want to loop through all the colors to create a rainbow, you may want to use the CHSV object to set the Hue, Saturation, and Value. By changing the H value from 0 to 255, you will rotate through the rainbow of colors, leaving S and V set at 255. We will try this later.

After setting all the LEDs to the color values you want, you must call FastLED.show() The 'FastLED.clear()' method sets all LEDs to black, but you must still call the show() method for the changes to take effect.

After running the code above, you should see the first 3 LEDs in your strip blink on an off Red, Green and Blue. If your strip is not showing the colors in that order, you will have to change the constant COLOR_ORDER GRB to correctly indicate the order your LED strip uses for the color codes.

With the code written, it is time to upload the code to the ESP32. Most programmers will instinctively want to get the green play button in VS Code to start debugging their project. Unfortunately, this does not work for The ESP32. You must use the two tiny button at the bottom of the window.

The Check Mark will compile your code and check for errors. The Arrow will compile your code and upload it to the ESP32 connected to your laptop with the USB cable. Once the program has uploaded, it will immediately begin to run and continue running until you disconnect it from power.

⚠️ **GitHub.com Fallback** ⚠️