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;
}
}
}
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.
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;
}
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.