Home > front end >  how to change the name of an array using a function to access a totally different array in Arduino /
how to change the name of an array using a function to access a totally different array in Arduino /

Time:01-06

I have been working on LED strip project in arduino and wanted to light up addressable LEDs using Ardunio <FastLED.h> libaray. I decided to use the same set of codes to access two LED strips connected to two different digital pins on the arduino mega controller. see arduino code below.

#include <FastLED.h>
#include <string.h>


 CRGB leds1[5];
 CRGB leds2[5];

 void setup() {
  FastLED.addLeds<WS2812, 4, GRB>(leds1, 5);
  FastLED.addLeds<WS2812, 5, GRB>(leds2, 10);

  FastLED.setMaxPowerInVoltsAndMilliamps(5, 500);
  FastLED.clear();
  FastLED.show();
 
}

void loop() {
  
  num1();
 
}

void num1(){

   a(255);   // single strip 
 
}


void a(int value ){  

  int red = value;
  int green =0;
  int blue =0;

  leds1[0]=CRGB(red, green, blue); 
  leds1[1]=CRGB(red, green, blue);
  leds1[2]=CRGB(red, green, blue);
  leds1[3]=CRGB(red, green, blue);
  leds1[4]=CRGB(red, green, blue);
  

  FastLED.setBrightness(20);
  FastLED.show();

}

I want to use the function void a to access the leds in the array leds2 .

how to change the leds1 to leds2 using a variable passes as a parameter to the function void a ??

I tried below , no syntax errors but cannot light up the LEDs.

#include <FastLED.h>
#include <string.h>


CRGB leds1[5];
CRGB leds2[5];

void setup() {
  FastLED.addLeds<WS2812, 4, GRB>(leds1, 5);
  FastLED.addLeds<WS2812, 5, GRB>(leds2, 10);

  FastLED.setMaxPowerInVoltsAndMilliamps(5, 500);
  FastLED.clear();
  FastLED.show();
 
}

void loop() {

  num1('1');
  num1('2');
 
}

void num1(char suffix){

   a(255,suffix);   
 
}



void a(int value , char suffix){

  int red = value;
  int green =0;
  int blue =0;

  char lednum[6] ;
  lednum[0]='l';
  lednum[1]='e';
  lednum[2]='d';
  lednum[3]='s';
  lednum[4]=suffix;
  lednum[5]=0;

  
  
  CRGB led= lednum;

  led[0]=CRGB(red, green, blue); 
  led[1]=CRGB(red, green, blue);
  led[2]=CRGB(red, green, blue);
  led[3]=CRGB(red, green, blue);
  led[4]=CRGB(red, green, blue);
  

  FastLED.setBrightness(20);
  FastLED.show();

}

CodePudding user response:

heres a starting hint

 void a(int value , CRGB *leds) {
      ...
 }

then you can do

  a(255, leds1);

or

 a(128, leds2);
  • Related