Beginner Question - More efficient LED code line

This is a very simple question, but I'd be appreciative of help.
I'm currently using a 200 LED strip, in which I'd only like to use a specific amount of LEDs. To do this, I'm using the following string:

"leds[0] = CRGB(255, 0, 0)"

This line only activates the first LED. Usually, I'd simply C&P this line of code and increase the number each time, but that feels redundant. I would imagine there's a way to light up 0-X with one line. If so, could you assist me, and if not, is there a more efficient function to use?

Welcome to the forum

Your first move should be to investigate the use of a for loop

for (int led = 0; led < 100; led++)  //first 100 LEDs
{
  leds[led] = CRGB(255, 0, 0)   //set the current LED to red
}

Which LED library are you using ?

2 Likes
fill_solid( &(leds[0]), 10 /*number of leds*/, CHSV( 224, 187, 255) );
fill_solid( &(leds[10]), 10 /*number of leds*/, CRGB(255, 0, 0) );

Possibly, if you told us what library you're using. @kolaha has made a good guess, but it's really up to the OP to provide such info.

I apologize for not providing that before: I'm using the FastLED library.

1 Like

If you had provided your code, as prompted to by community guidelines, we'd not be having this discussion, because it would be immediately evident.

1 Like

Some of the many functions available...

http://fastled.io/docs/group___color_fills.html

LED coloring...

Even though the LastLED library has functions to do what is required I am still of the opinion that

3 Likes

I found this helpful. I vaguely remember writing a similar line of code for a similar project, so I'm incredibly appreciative of you reminding me :joy:

Thank you.

i have this beginners code my brothers

#include
#include
#include <unistd.h>
#include
#include

#define LED_1_GPIO 80
#define LED_2_GPIO 81
#define BUTTON_1_GPIO 100
#define BUTTON_2_GPIO 101

void setup_gpio(int gpio, const std::string& direction) {
std::ofstream exportFile("/sys/class/gpio/export");
if (!exportFile) {
std::cerr << "Erreur : Impossible d'ouvrir /sys/class/gpio/export" << std::endl;
exit(1);
}
exportFile << gpio;
exportFile.close();

std::ofstream directionFile("/sys/class/gpio/gpio" + std::to_string(gpio) + "/direction");
if (!directionFile) {
    std::cerr << "Erreur : Impossible d'ouvrir la direction de GPIO " << gpio << std::endl;
    exit(1);
}
directionFile << direction;
directionFile.close();

}

int read_gpio(int gpio) {
std::ifstream valueFile("/sys/class/gpio/gpio" + std::to_string(gpio) + "/value");
int value;
if (!valueFile) {
std::cerr << "Erreur : Impossible de lire la valeur du GPIO " << gpio << std::endl;
exit(1);
}
valueFile >> value;
return value;
}

void write_gpio(int gpio, int value) {
std::ofstream valueFile("/sys/class/gpio/gpio" + std::to_string(gpio) + "/value");
if (!valueFile) {
std::cerr << "Erreur : Impossible d'écrire dans la valeur du GPIO " << gpio << std::endl;
exit(1);
}
valueFile << value;
valueFile.close();
}

void led_blink() {
for (int i = 0; i < 10; ++i) {
write_gpio(LED_1_GPIO, 1);
write_gpio(LED_2_GPIO, 1);
std::this_thread::sleep_for(std::chrono::milliseconds(500));
write_gpio(LED_1_GPIO, 0);
write_gpio(LED_2_GPIO, 0);
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
}

int main() {
std::cout << "Initialisation des GPIO..." << std::endl;

setup_gpio(BUTTON_1_GPIO, "in");
setup_gpio(BUTTON_2_GPIO, "in");


setup_gpio(LED_1_GPIO, "out");
setup_gpio(LED_2_GPIO, "out");

while (true) {
    int button1_state = read_gpio(BUTTON_1_GPIO);
    int button2_state = read_gpio(BUTTON_2_GPIO);

    
    if (button1_state == 1) {
        write_gpio(LED_1_GPIO, 1);
    } else {
        write_gpio(LED_1_GPIO, 0);
    }

    
    if (button2_state == 1) {
        led_blink();
        std::this_thread::sleep_for(std::chrono::seconds(5));
    }

    
    std::this_thread::sleep_for(std::chrono::milliseconds(100));
}

return 0;

}

What about it ?

Very cool :+1:

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