How to make the LEDS loop?

Hi Everyone,

Just started to use the Arduino, but still a lot to learn i notice.
So i made a pcb and i found a simple tut to let them burn the right color (see below for the simple code).

But now them want to blink or made them loop in different style.

I did try to find something useful, but no luck yet.

Could somebody help me please?

Thank you in advance.

Hereby the simple script.

#include <FastLED.h>

#define LED_PIN 5

#define NUM_LEDS 44

CRGB leds[NUM_LEDS];

void setup() {

FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);

}

void loop(void) {

leds[0] = CRGB(215, 0, 0);

FastLED.show();

delay(200);

leds[1] = CRGB(215, 0, 0);

FastLED.show();

delay(200);

leds[2] = CRGB(215, 215, 0);

FastLED.show();

delay(200);

leds[3] = CRGB(215, 215, 0);

FastLED.show();

delay(200);

leds[4] = CRGB(0, 215, 0);

FastLED.show();

delay(200);

leds[5] = CRGB(215, 0, 0);

FastLED.show();

delay(200);

leds[6] = CRGB(215, 0, 0);

FastLED.show();

delay(200);

leds[7] = CRGB(215, 0, 0);

FastLED.show();

delay(200);

leds[8] = CRGB(215, 215, 0);

FastLED.show();

delay(200);

leds[9] = CRGB(215, 215, 0);

FastLED.show();

delay(200);

leds[10] = CRGB(215, 215, 0);

FastLED.show();

delay(200);

leds[11] = CRGB(215, 215, 0);

FastLED.show();

delay(200);

leds[12] = CRGB(215, 215, 0);

FastLED.show();

delay(200);

leds[13] = CRGB(0, 215, 0);

FastLED.show();

delay(200);

leds[14] = CRGB(0, 215, 0);

FastLED.show();

delay(200);

leds[15] = CRGB(215, 0, 0);;

FastLED.show();

delay(200);

leds[16] = CRGB(215, 0, 0);

FastLED.show();

delay(200);

leds[17] = CRGB(215, 0, 0);

FastLED.show();

delay(200);

leds[18] = CRGB(215, 215, 0);

FastLED.show();

delay(200);

leds[19] = CRGB(0, 215, 0);

FastLED.show();

delay(200);

leds[20] = CRGB(215, 0, 0);

FastLED.show();

delay(200);

leds[21] = CRGB(215, 0, 0);

FastLED.show();

delay(200);

leds[22] = CRGB(215, 0, 0);

FastLED.show();

delay(200);

leds[23] = CRGB(0, 215, 0);

FastLED.show();

delay(200);

leds[24] = CRGB(0, 215, 0);

FastLED.show();

delay(200);

leds[25] = CRGB(215, 215, 0);

FastLED.show();

delay(200);

leds[26] = CRGB(215, 0, 0);

FastLED.show();

delay(200);

leds[27] = CRGB(0, 215, 0);

FastLED.show();

delay(200);

leds[28] = CRGB(215, 215, 0);

FastLED.show();

delay(200);

leds[29] = CRGB(215, 215, 0);

FastLED.show();

delay(200);

leds[30] = CRGB(215, 215, 0);

FastLED.show();

delay(200);

leds[31] = CRGB(215, 0, 0);

FastLED.show();

delay(200);

leds[32] = CRGB(215, 0, 0);

FastLED.show();

delay(200);

leds[33] = CRGB(215, 0, 0);

FastLED.show();

delay(200);

leds[34] = CRGB(0, 215, 0);

FastLED.show();

delay(200);

leds[35] = CRGB(0, 215, 0);

FastLED.show();

delay(200);

leds[36] = CRGB(215, 215, 0);

FastLED.show();

delay(200);

leds[37] = CRGB(215, 215, 0);

FastLED.show();

delay(200);

leds[38] = CRGB(215, 0, 0);

FastLED.show();

delay(200);

leds[39] = CRGB(0, 215, 0);

FastLED.show();

delay(200);

leds[40] = CRGB(0, 215, 0);

FastLED.show();

delay(200);

leds[41] = CRGB(0, 215, 0);

FastLED.show();

delay(200);

leds[42] = CRGB(215, 0, 0);

FastLED.show();

delay(200);

leds[43] = CRGB(0, 215, 0);

FastLED.show();

delay(200);

}

The FastLED library has worked examples.

Welcome to the forum

Your topic was MOVED to the Programming category of the forum category which is more appropriate than the original as it has nothing to do with the Arduino project itself

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

1 Like

Please return and edit your post, inserting the code using the code tags.

2 Likes

[EDIT] Your sketch does loop. You just can not see it because it places the same colors on the same pixels. You should make a clearing sequence at the end, or during the loop.[/EDIT]

You can see the value of 215 repeats. It can be represented as a (dec, bin or hex) value inside the leds[x]=() function.

For example:

  leds[0] = CRGB(215, 0, 0);
  leds[2] = CRGB(215, 215, 0);
  leds[4] = CRGB(0, 215, 0);

The [0] value could be 0b100 or 0x4
The [2] value could be 0b110 or 0x6
The [4] value could be 0b010 or 0x2

The full sequence in hex would be:
4, 4, 6, 6, 2, 4, 4, 4, 6, 6, 6, 6, 6, 2, 2, 4, 4, 4, 6, 2, 4, 4, 4, 2, 2, 6, 4, 2, 6, 6, 6, 4, 4, 4, 2, 2, 6, 6, 4, 2, 2, 2, 4, 2

With this sequence, you can write one function:

  for (int i = 0; i < 44; i++) {
    leds[i] = CRGB(seq[0], seq[1], seq[2]);
    FastLED.show();
    delay(200);
  }

Here is a simulation of your original code, plus a clearing sequence at the end.

Just waiting for the launch code.

how would you trigger a change, by adding a button?

Hi GCJR,

A button would not work in this display, thank you for the tip.

Wow, that is awesome and a lot easier.

Thank you very much!!!

Here's the simulation with repetition replaced with looping...

#include <FastLED.h>
#define LED_PIN 5
#define NUM_LEDS 44
CRGB leds[NUM_LEDS];

void setup() {
  FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
  // Serial.begin(115200);
}

void loop(void) {
  original();
}

void original() {

  int color[45][3] = {
    {215, 0, 0}, {215, 0, 0}, {215, 215, 0}, {215, 215, 0}, {0, 215, 0},    // 1 = 44662
    {215, 0, 0}, {215, 0, 0}, {215, 0, 0}, {215, 215, 0}, {215, 215, 0},    // 2 = 44466
    {215, 215, 0}, {215, 215, 0}, {215, 215, 0}, {0, 215, 0}, {0, 215, 0},  // 3 = 66622
    {215, 0, 0}, {215, 0, 0}, {215, 0, 0}, {215, 215, 0}, {0, 215, 0},      // 4 = 44462
    {215, 215, 0}, {215, 215, 0}, {215, 215, 0}, {0, 215, 0}, {0, 215, 0},  // 5 = 66622
    {215, 215, 0}, {215, 0, 0}, {0, 215, 0}, {215, 215, 0}, {215, 215, 0},  // 6 = 64266
    {215, 215, 0}, {215, 0, 0}, {215, 0, 0}, {215, 0, 0}, {0, 215, 0},      // 7 = 64442
    {0, 215, 0}, {215, 215, 0}, {215, 215, 0}, {215, 0, 0}, {0, 215, 0},    // 8 = 26642
    {0, 215, 0}, {0, 215, 0}, {215, 0, 0}, {0, 215, 0}                      // 9 = 2242
  };

  for (int i = 0; i < 45; i++) { // 44 LEDs
    leds[i] = CRGB(color[i][0], color[i][1], color[i][2]);
    FastLED.show();
    delay(100);
  }

  for (int i = 0; i < 45; i++) { // a different way to clear the pixels
    leds[i] = CRGB(0, 0, 0);
    FastLED.show();
    delay(10);
  }

There are features of C/C++ that will save you major time beating the keys!

... annotated in comments on the right.

This sim was a step toward that idea, but still recognizable if compared to the source.

(and looking back, I see my copy/paste blunders in the values in the code (not in the comments... for that I thank you for commenting)

I'm coming from over 40 years experience and hard way learning.

There's a command structure called switch-case that takes a lot of code to do the same with more spaghetti potential. Switch-case makes state machine code easier to write but perversely makes function splitting harder because you don't have to, just add cases (not always best).

There's the whole subject of pointers, not beginner stuff, and structs without leaving C and then C++ is bigger than all that with so much featurized parts that don't fit small memory devices like AVR chips.

So much and what you can do does keep getting better.

1 Like

can you provide an example?

You have many examples in the Libraries you use.

C++ is a big study that helped me compact a lot of big code that used loads of RAM -- none of which matches Arduino but a lot of C++ can and does if you just avoid dynamic allocation and every damned class that uses it!

Beginners do not need C++. It can keep them from learning more basic programming and end up with poor spotty foundations to code on.

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