0

I'm working on Arduino project with addressable LEDs and I'm using the FastLED.h library.

To simplify my program, I want to create a library called LedsHorloge.h from which one I'll call the function Jauge(int tour, int numberLeds, int brightness, CRGB* ledTable) to light up my LED strip.

I'm newbie in C++.

In the Arduino IDE, this is the error I get:

D:\Programmation\Arduino\libraries\libraries\JaugeLED/LedsHorloge.h:16:8: note: void LedsJauge::Jauge(int, int, int, CRGB*)

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

        ^

D:\Programmation\Arduino\libraries\libraries\JaugeLED/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])'

This error happens when I run this code in the 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) ;
        }
    }  
}

This is my header called 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

This is my source code called 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;
}

I don't know how to make a pointer on the object CRGB leds[NUMBER_LEDS];.

Joel Spolsky
  • 33,372
  • 17
  • 89
  • 105
FrancNovation
  • 349
  • 6
  • 20

2 Answers2

0

I get the answer from another forum, the solution is to replace :

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

By

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

However, I don't really understand why I must not use the memory value of the variable whereas I use a pointer in the declaration of the function Jauge, let's see :

void LedsJauge::Jauge(int tour, int numberLeds, int brightness, CRGB* ledTable)
FrancNovation
  • 349
  • 6
  • 20
0

That's because

    CRGB leds[NUMBER_LEDS];

leds is already a pointer; only that it's a const pointer. i.e. one cannot change the address of the variable.

  • Note that `leds` is *not* a pointer, it's an array. However, arrays decay to pointers (to their first element) when used as parameters in function calls. – Adrian Mole Apr 18 '21 at 13:51