Arduino/FastLED newb code refinement help

First post so go easy on me. Very new to Arduino. Got some WS2812B strips and ATTiny85 chips to play around with holiday decorations. I've got a few patterns up and running but one in particular is a pain to use if adjusting to different strip lengths. Is there a better/ more efficient way to achieve the desired effect without writing all of the for lines? Thanks. Red_White_Blue4 - Wokwi ESP32, STM32, Arduino Simulator

#include "Arduino.h"
#include "FastLED.h"

#define NUM_LEDS 60   // Total number of leds used in the strip
#define DATA_PIN 1    //Data output pin

// Define the array of leds
CRGB leds[NUM_LEDS];
int x;
int temp;

void setup() {
pinMode(0, INPUT_PULLUP);

FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
}

void loop() {
while (digitalRead(0) == LOW) 
{
// scrolling red, white &  blue for 4th of july
// setup initial color layout first
for (int i = 0; i <= 4; i++) { // red
leds[i]=0xFF0000;
}
for (int i = 5; i <= 9; i++) { // white
leds[i]=0xFFFFFF;
}
for (int i = 10; i <= 14; i++) { // blue
leds[i]=0x0000F5;
}
for (int i = 15; i <= 19; i++) { // red
leds[i]=0xFF0000;
}
for (int i = 20; i <= 24; i++) { // white
leds[i]=0xFFFFFF;
}
for (int i = 25; i <= 29; i++) { // blue
leds[i]=0x0000F5;
}
for (int i = 30; i <= 34; i++) { // red
leds[i]=0xFF0000;
}
for (int i = 35; i <= 39; i++) { // white
leds[i]=0xFFFFFF;
}
for (int i = 40; i <= 44; i++) { // blue
leds[i]=0x0000F5;
}
for (int i = 45; i <= 49; i++) { // red
leds[i]=0xFF0000;
}
for (int i = 50; i <= 54; i++) { // white
leds[i]=0xFFFFFF;
}
for (int i = 55; i <= 59; i++) { // blue
leds[i]=0x0000F5;
}
while (true) //move continuously
{
FastLED.show(); //display
delay(35); //Scroll speed
//move each color one spot over
CRGB x =leds[59];  // These should be 1 less than #define NUM_LEDS ##?
for (int i = 59; i >= 1; i--) {
CRGB temp = leds[i-1];
leds[i] = temp;
}
leds[0]=x;
}
}
}

ATtiny85. 0 is good.
First of all... pin 0 or pin 1 should not be used. They have a special purpose. Use pin 2 for this.

Read about "for()" loops. It will help you when repeating patterns.

Before "setup()" start three arrays. These arrays are the first pixel (0 through 59) in the color (r or w or b) of the pattern (rwb).

int colorRed[] = { 0, 15, 30, 45}; //start of color
int colorWht[] = { 5, 20, 35, 50};
int colorBlu[] = {10, 25, 40, 55};

Then, inside "setup()" (no need to use "loop()") use these "for()" loops to count through the patterns...

  for (int i = 0; i < 4; i++) { // repeat pattern four times
    for (int j = 0; j < 5; j++) { // five pixels per color
      leds[colorBlu[i] + j] = 0x0000FF;
      leds[colorRed[i] + j] = 0xFF0000;
      leds[colorWht[i] + j] = 0xFFFFFF;
      FastLED.show();
    }
  }

Your complete program will look like this...

#include "Arduino.h"
#include "FastLED.h"

#define NUM_LEDS 60   // Total number of leds used in the strip
#define DATA_PIN 4    //Data output pin

int colorRed[] = { 0, 15, 30, 45}; //start of color
int colorWht[] = { 5, 20, 35, 50};
int colorBlu[] = {10, 25, 40, 55};

// Define the array of leds
CRGB leds[NUM_LEDS];

void setup() {
  FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);

  for (int i = 0; i < 4; i++) { // repeat pattern four times
    for (int j = 0; j < 5; j++) { // five pixels per color
      leds[colorBlu[i] + j] = 0x0000FF;
      leds[colorRed[i] + j] = 0xFF0000;
      leds[colorWht[i] + j] = 0xFFFFFF;
      FastLED.show();
    }
  }
}

void loop() {
  // empty
}
1 Like

Another good function in FastLED is fill_solid() in which you give it the Neopixel you want colored, and the function fills from "0" to "Neopixel". Another name for that type of coloring is "pixel planing" and is used in high-speed video graphics.

This is much better. Thank you.

I'll do some reading up on this. Thanks again!

After adding the animation, I saved the simulation of your code at Red_White_Blue4 Optimized - Wokwi ESP32, STM32, Arduino Simulator

Just for the sake of learning to write efficient code, are there any obvious changes you would suggest?

#include "Arduino.h"
#include "FastLED.h"

#define NUM_LEDS 60   // Total number of leds used in the strip
#define DATA_PIN 1    //Data output pin

int colorRed[] = { 0, 15, 30, 45}; //start of color
int colorWht[] = { 5, 20, 35, 50};
int colorBlu[] = {10, 25, 40, 55};

// Define the array of leds
CRGB leds[NUM_LEDS];

void setup() {
  FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);

  for (int i = 0; i < 4; i++) { // repeat pattern four times
    for (int j = 0; j < 5; j++) { // five pixels per color
      leds[colorBlu[i] + j] = 0x0000FF;
      leds[colorRed[i] + j] = 0xFF0000;
      leds[colorWht[i] + j] = 0xFFFFFF;
     
    }
  }
}

void loop() {
  // empty
   FastLED.show();
      delay(35); //Scroll speed
//move each color one spot over
CRGB x =leds[59];  // These should be 1 less than #define NUM_LEDS ##
for (int i = 59; i >= 1; i--) {
CRGB temp = leds[i-1];
leds[i] = temp;
}
leds[0]=x;

}

Wow. That's awesome! Vive la France!

@ry62

How did you get the Big Ring of LEDS?

This is all I can find, the small ring.

Select and place that small ring. Then, on the side where you're writing your sketch, hit the diagram.json tab. Look for the "wokwi-led-ring", section. Change "attrs": { "pixels": "60" } for this ring or however many leds in your strip.

@ry62
Thanks for the info.

1 Like

Wow! I'm glad you got it done! bravo! :clap::smile:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.