Controlling multiple leds with less code

Salutations! Wondering how to control leds with less code. For the first code, I have 5 led's to create a chase effect. The second, a WS2812B matrix. How can I control all led's at once on the matrix w/o 64 lines of code? Thank you in advance!

int ledPins[] = {5,6,7,8,9};
int pushButton = 4;

void setup() {
  int index;
  for (index = 0; index <= 5; index++){
  pinMode(ledPins, OUTPUT);
  pinMode (pushButton, INPUT);
  }
  
}
void loop() {
  pushButton = digitalRead(4);
  if (pushButton == HIGH){
    digitalWrite(ledPins[0], HIGH);
    delay(100);
    digitalWrite(ledPins[1], HIGH);
    delay(100);
    digitalWrite(ledPins[2], HIGH);
    delay(100);
    digitalWrite(ledPins[3], HIGH);
    delay(100);
    digitalWrite(ledPins[4], HIGH);
    delay(100);
    digitalWrite(ledPins[0], LOW);
    digitalWrite(ledPins[1], LOW);
    digitalWrite(ledPins[2], LOW);
    digitalWrite(ledPins[3], LOW);
    digitalWrite(ledPins[4], LOW);
    delay(250);
    digitalWrite(ledPins[0], HIGH);
    digitalWrite(ledPins[1], HIGH);
    digitalWrite(ledPins[2], HIGH);
    digitalWrite(ledPins[3], HIGH);
    digitalWrite(ledPins[4], HIGH);
  }
  else {
    digitalWrite(ledPins[0], LOW);
    digitalWrite(ledPins[1], LOW);
    digitalWrite(ledPins[2], LOW);
    digitalWrite(ledPins[3], LOW);
    digitalWrite(ledPins[4], LOW);
    
  }

}/code]



[code]#include <FastLED.h>
#define LED_PIN     7
#define NUM_LEDS    64
CRGB leds[NUM_LEDS];
void setup() {
  FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
  
}
void loop() {
  
  leds[0] = CRGB(0, 0, 255);
  FastLED.show();
  delay(500);  
  leds[1] = CRGB(0, 0, 255);
  FastLED.show();
  delay(500);
  leds[2] = CRGB(0, 0, 255);
  FastLED.show();
  delay(500);
  leds[3] = CRGB(150, 0, 255);
  FastLED.show();
  delay(500);
  leds[4] = CRGB(0, 0, 255);
  FastLED.show();
  delay(500);
  leds[5] = CRGB(0, 0, 255);
  FastLED.show();
  delay(500);
  leds[6] = CRGB(0, 0, 255);
  FastLED.show();
  delay(500);
  leds[7] = CRGB(0, 0, 255);
  FastLED.show();
  delay(500);  
  leds[8] = CRGB(0, 0, 255);
  FastLED.show();
  delay(500);
  leds[9] = CRGB(0, 0, 255);
  FastLED.show();
  delay(500);
  leds[10] = CRGB(150, 0, 255);
  FastLED.show();
  delay(500);
  leds[11] = CRGB(0, 0, 255);
  FastLED.show();
  delay(500);
  leds[12] = CRGB(0, 0, 255);
  FastLED.show();
  delay(500);
  leds[13] = CRGB(0, 0, 255);
  FastLED.show();
  delay(500);
}/code]

Why are you able to use an array & for loop to initialize the pins in setup() but not in loop()?

The code can be compressed by looking at the patterns you're doing and the repetitious nature of the LED controls and moving things to for loops or a statemachine.

do you mean using the "for" function in the loop to make an index?

It's like setup() and loop() were written by two different people.

For example, setup() has the general idea of array indexing (slightly modified version of your OP code):

...
    for (int index = 0; index <= 5; index++)
    {
        pinMode(ledPins[i], OUTPUT); //fixed to include the index

    }//for

    pinMode (pushButton, INPUT);  //not required in the loop
...

but your loop has this (in part):

...
    digitalWrite(ledPins[0], HIGH);
    delay(100);
    digitalWrite(ledPins[1], HIGH);
    delay(100);
    digitalWrite(ledPins[2], HIGH);
    delay(100);
    digitalWrite(ledPins[3], HIGH);
    delay(100);
    digitalWrite(ledPins[4], HIGH);
    delay(100); ...

Applying the same thinking (with corrections) from your setup(), why not do something like:

        for( int i=0; i<5; i++ )
        {
            digitalWrite(ledPins[i], HIGH);
            delay(100);
            
        }//for

(I wouldn't advocate the use of millis() as you're doing but don't want to change that for the sake of staying focused on your current problem...)

sorry, beginner question, what do you mean by millis? like the millis function? I will try that code tho, thanks a bunch

Yog1Girl:
How can I control all led's at once on the matrix w/o 64 lines of code? Thank you in advance!

This will be one way

void loop()
{
  for (uint8_t ledCnt = 0; ledCnt < NUM_LEDS; ledCnt++)
  {
    switch (ledCnt)
    {
      case 3:
        leds[ledCnt] = CRGB(150, 0, 255);
        break;
      case 10:
        leds[ledCnt] = CRGB(150, 0, 255);
        break;
      default:
        leds[ledCnt] = CRGB(0, 0, 255);
    }
    FastLED.show();
    delay(500);
  }
}

Note that with your colours, you can combine the cases 3 and 10

   switch (ledCnt)
    {
      case 3:
      case 10:
        leds[ledCnt] = CRGB(150, 0, 255);
        break;
      default:
        leds[ledCnt] = CRGB(0, 0, 255);
    }

How much you have to type depends on the pattern that you want to achieve; if you can apply a mathematical formula (e.g. each 3rd led has a different colour), your code can be smaller

void loop()
{
  for (uint8_t ledCnt = 0; ledCnt < NUM_LEDS; ledCnt++)
  {
    if (ledCnt % 3 == 0)
    {
      leds[ledCnt] = CRGB(150, 0, 255);
    }
    else
    {
      leds[ledCnt] = CRGB(0, 0, 255);
    }

    FastLED.show();
    delay(500);
  }
}

Note
64 leds with 500ms delay in a for-loop will take 32 seconds to complete; during that time, pressing a button will not have any effect.

PS
Code not tested

To prepare for the future, below is a non-blocking version of the code; no more for-loops, no more delays.

void loop()
{
  // remember last time that strip was updated
  static uint32_t lastUpdateTime;
  // remember next led to update
  static uint8_t ledCnt;

  if (millis() - lastUpdateTime >= 500)
  {
    // remember when last updated
    lastUpdateTime = millis();
    // set the led
    switch (ledCnt)
    {
      case 3:
      case 10:
        leds[ledCnt] = CRGB(150, 0, 255);
        break;
      default:
        leds[ledCnt] = CRGB(0, 0, 255);
    }
    // show the led
    FastLED.show();
    // update the led counter
    ledCnt++;
    if (ledCnt >= NUM_LEDS)
    {
      ledCnt = 0;
    }
  }
}

Again, code not tested

Yog1Girl:
sorry, beginner question, what do you mean by millis? like the millis function? I will try that code tho, thanks a bunch

Sorry, meant "delay()"; you should be using millis() to do delay timing. Later on, when your code becomes more complex and you want to do several things at the same time, you'll be glad you didn't get dependent on the "delay()" method early on.

sterretje, thank you! the code works. now how would you suggest I turn all 64 off at once and back on?

Do you understand the codes that I presented?

In a for-loop, you can set all leds to off (I think it's CRGB(0,0,0)). And next use show() to apply.

There is also a clear() method.

I suggest that you read the FastLED wiki; FastLED basic usage and from there get ideas what is possible.

int ledPins[] = {5,6,7,8,9};
...
...
  for (index = 0; index <= 5; index++)

Oops

Thanks for the link! Makes more sense now I think I can do what I need to with my project. Thank you for all of the help!

Alright, so now how do I make it do a chase effect with all 64, have all lights go off, then back on? here is the code I have. At the moment, there is no chase with this code, they all go on at once. should I write a new function, and/or change the order?

#include <FastLED.h>
#define LED_PIN     9
#define NUM_LEDS    64
CRGB leds[NUM_LEDS];

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

}

void loop() {
  for (uint8_t ledCnt = 0; ledCnt < NUM_LEDS; ledCnt++)
  {
    switch (ledCnt)
    {
      case 3:
        leds[ledCnt] = CRGB(150, 0, 255);
        break;
      case 10:
        leds[ledCnt] = CRGB(150, 0, 255);
        break;
      default:
        leds[ledCnt] = CRGB(0, 0, 255);
    }
    FastLED.show();
    delay(100);
    FastLED.clear();
        for(int led = 0; led < NUM_LEDS; led++) { 
            leds[led] = CRGB::Blue; 
        }
        FastLED.show();
    }
  }
/code]
  1. WHAT you want to do?
  2. Why you are so intrested in the lines of code? It js totally nonsense
  3. do you count only your printed lines or all the lines of your code? If the first the answare is simple, I can do whatever I want in only one line: I can create a library, that contiens all my code and, in the code.ino the only line I write is the #include of the library. If the second you simply can't write a code in less than 64 lines, for a simple reason: you don't see the complete Arduino code, but only a part of it. In the complete code there are so many #include of libraries, that you CAN'T cancel, and it's very probably that the full code has more than 64 lines

trying to make my project work and control the leds without having to write at least 64 different lines of code for each light, for each effect I want to do. having less line of code seems to make it easier to modify once I see how it looks. the other posters have answered my questions and have offered simple solutions, which I appreciate. I am still learning how to code, this is my first project from scratch, and my first time using a led matrix, so I am learning as I go, hence why I seeking assistance here, which has been VERY helpful

You will need to think about the logic of your chase. It's not quite clear to me what you mean by chase, but here is something that I would call a chase.

It will start with all leds off, next start switching on one-by-one a defined number of leds (MAX_ON) starting from the beginning of the strip. Once the MAX_ON number of leds is ON, it will start switching them off from the beginning of the strip while continuing switching on new leds closer to the end of the strip.

For a strip with 8 leds and MAX_ON set to 3, it would look like

'

bbbbbbbb <--+
Xbbbbbbb    |
XXbbbbbb    |
XXXbbbbb    |
bXXXbbbb    |
bbXXXbbb    |
bbbXXXbb    |
bbbbXXXb    |
bbbbbXXX    |
bbbbbbXX    |
bbbbbbbX ---+
#include <FastLED.h>
#define LED_PIN     A0
#define NUM_LEDS    8   // number of leds in strip
#define MAX_ON      6   // number of leds that are on at a time
CRGB leds[NUM_LEDS];

const uint16_t delayTime = 50;

void setup()
{
  FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
  Serial.begin(57600);
  while (!Serial);
  if (MAX_ON > NUM_LEDS)
  {
    Serial.println("Idiot, number of leds that can be ON in a strip (MAX_ON) can't be bigger than the number of leds in the strip (NUM_LEDS)");
    for (;;);
  }
}

void loop()
{
  // last time that strip was updated
  static uint32_t lastUpdateTime = 0;
  // position in strip to update
  static uint8_t ledCnt = 0;

  // check if it's time to update the strip
  if (millis() - lastUpdateTime < delayTime)
  {
    // not time yet
    return;
  }

  // update the last time that we updated the strip
  lastUpdateTime = millis();

  // we basically fill the strip one led at a time
  if (ledCnt < NUM_LEDS)
  {
    //Serial.print("Setting led "); Serial.println(ledCnt);
    leds[ledCnt] = CRGB(150, 0, 255);
  }
  // once we have reached MAX_ON, start clearing from the beginning
  if (ledCnt - MAX_ON >= 0)
  {
    //Serial.print("Clearing led "); Serial.println(ledCnt - MAX_ON);
    leds[ledCnt - MAX_ON] = CRGB(0, 0, 0);
  }
  FastLED.show();

  // next led at the next time that loop() is called
  ledCnt++;

  // if the last led is cleared
  if (ledCnt >= NUM_LEDS + MAX_ON)
  {
    // clear the counter
    ledCnt = 0;
  }
}

Notes:
1)
I've changed the pin to suite my needs.
I've changed NUM_LEDS as I only have an 8-led strip.
2)
You can change MAX_ON to change the number of leds that will be on in the chase
3)
You can uncomment the Serial.print statements to see on serial monitor what is happening.

Tested with a Leonardo