简体   繁体   English

如何使用 function 更改数组的名称以访问 Arduino / C/ C++ 中完全不同的数组

[英]how to change the name of an array using a function to access a totally different array in Arduino / C/ C++

I have been working on LED strip project in arduino and wanted to light up addressable LEDs using Ardunio <FastLED.h> libaray.我一直致力于 arduino 中的 LED 灯条项目,并希望使用 Ardunio <FastLED.h> libaray 点亮可寻址 LED。 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.我决定使用同一组代码来访问连接到 arduino mega controller 上两个不同数字引脚的两个 LED 灯条。请参阅下面的 arduino 代码。

#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.我想使用 function void a访问数组leds2 中的 LED。

how to change the leds1 to leds2 using a variable passes as a parameter to the function void a??如何使用变量将 leds1 更改为 leds2 作为参数传递给 function void a?

I tried below, no syntax errors but cannot light up the LEDs.我在下面尝试过,没有语法错误,但无法点亮 LED。

#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();

}

heres a starting hint这是一个开始提示

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

then you can do那么你可以做

  a(255, leds1);

or或者

 a(128, leds2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM