创建头文件并使用FastLED库中的CRGB LED阵列时出错

Error to create header file and use CRGB LED array from FastLED library

本文关键字:CRGB LED 阵列 出错 文件 FastLED 创建      更新时间:2023-10-16

我正在使用可寻址LED的Arduino项目,我正在使用FastLED.h库。

为了简化我的程序,我想创建一个名为LedsHorloge.h的库,从中我将调用函数Jauge(int tour, int numberLeds, int brightness, CRGB* ledTable)来点亮我的LED灯带。

我是C++的新手。

在Arduino IDE中,这是我得到的错误:

D:ProgrammationArduinolibrarieslibrariesJaugeLED/LedsHorloge.h:16:8: note: void LedsJauge::Jauge(int, int, int, CRGB*)
void Jauge(int tour, int numberLeds, int brightness, CRGB* ledTable);
^
D:ProgrammationArduinolibrarieslibrariesJaugeLED/LedsHorloge.h:16:8: note:   no known conversion for argument 4 from 'CRGB (*)[32]' to 'CRGB*'
exit status 1
no matching function for call to 'LedsJauge::Jauge(int&, int, int, CRGB (*)[32])'

当我在Arduino IDE:中运行此代码时,会发生此错误

#include <LedsHorloge.h>
#include "FastLED.h"
#define LED_PIN     6
#define NUMBER_LEDS    32
#define BRIGHTNESS  100
#define LED_TYPE    WS2812B
#define COLOR_ORDER GRB
#define VITESSE_JAUGE 150 // Delay milliseconds
CRGB leds[NUMBER_LEDS];
#define UPDATES_PER_SECOND 100
int tour = 0 ; // Compte le nombre de tour dans le loop
int pinButton = 8; // Pin qui déclenche la fonction "Jauge"
boolean flag = false ;
LedsJauge jauge(LED_PIN); // This is the object I create 
void setup() {
delay( 3000 ); // power-up safety delay
FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUMBER_LEDS).setCorrection( TypicalLEDStrip ); // Initialisation de la bande de leds
Serial.begin(9600);
FastLED.clear(); // Fonction d'initialisation, encore mal connue
pinMode(pinButton, INPUT_PULLUP) ;
InitBandeLeds(); // Eteint toutes les leds si elles sont allumées
FastLED.show();  // Applique la valeur HSV définie dans la variable "leds"
}
void loop() { 
// tant que le bouton n'est pas activé, le drapeau reste false
if ( digitalRead(pinButton) == LOW ){
flag = true ;
}

// lorsque le drapeau devient true on rentre dans la condition
if (flag){
if ( tour < NUMBER_LEDS ){  tour += 1;  } // A chaque loop la variable est incrémenté de 1
jauge.Jauge(tour, NUMBER_LEDS, BRIGHTNESS, &leds); // ERROR COMES FROM THIS LINE
FastLED.show(); 
FastLED.delay(1000 / UPDATES_PER_SECOND);
delay(VITESSE_JAUGE); // régle la vitesse d'allumage des leds
}
}

// Fonction qui éteint le bandeau de leds
void InitBandeLeds(){
for( int i = 0; i < NUMBER_LEDS; i++) {
if ( tour - i >0 ){
leds[i] = CHSV( 0, 0, 0) ;
}
}  
}

这是我的标题LedsHorloge.h

#ifndef LedsHorloge_h
#define LedsHorloge_h
#include <Arduino.h>
#include "FastLED.h"
class LedsJauge
{
public:
LedsJauge(int pin);
// A chaque tour une led supplémentaire de la jauge est allumée
void Jauge(int tour, int numberLeds, int brightness, CRGB* ledTable);
private:
int _pin;
};

#endif

这是我的源代码LedsHorloge.cpp

#include <Arduino.h>
#include "FastLED.h"
#include "LedsHorloge.h"
LedsJauge::LedsJauge(int pin)
{
_pin = pin;
}
void LedsJauge::Jauge(int tour, int numberLeds, int brightness, CRGB* ledTable)
{
for( int i = 0; i < numberLeds; i++) {
if ( tour - i >0 ){
ledTable[i] = CHSV( 100, 187, brightness) ;
}
}
return;
}

我不知道如何在对象CRGB leds[NUMBER_LEDS];上创建指针。

我从另一个论坛得到了答案,解决方案是替换:

jauge.Jauge(tour, NUMBER_LEDS, BRIGHTNESS, &leds);

通过

jauge.Jauge(tour, NUMBER_LEDS, BRIGHTNESS, leds);

然而,我真的不明白为什么我不能使用变量的内存值,而我在函数Jauge的声明中使用指针,让我们看看:

void LedsJauge::Jauge(int tour, int numberLeds, int brightness, CRGB* ledTable)

这是因为

CRGB leds[NUMBER_LEDS];

leds已经是一个指针;只是它是一个常量指针。即不能改变变量的地址。