Object dont work in other function

Hello,

I had make my own library. It works so long but in one case I have a problem that I can not solve.

Here the relevant code:

.h

class SRS_Configs
{
  public:
    void beginLED(byte R_PIN, byte G_PIN, byte B_PIN, byte CW_PIN, byte WW_PIN);
};

class SRS_Ethernets
{
  public:
    void readTCP();
};

.cpp

#include <FadeLed.h>
void SRS_Configs::beginLED(byte R_PIN, byte G_PIN, byte B_PIN, byte CW_PIN, byte WW_PIN)
{
  pinMode(R_PIN, OUTPUT);
  pinMode(G_PIN, OUTPUT);
  pinMode(B_PIN, OUTPUT);
  pinMode(CW_PIN, OUTPUT);
  pinMode(WW_PIN, OUTPUT);

  analogWriteResolution(12);

  FadeLed LED_R(R_PIN, false);
  FadeLed LED_G(G_PIN, false);
  FadeLed LED_B(B_PIN, false);
  FadeLed LED_CW(CW_PIN, false);
  FadeLed LED_WW(WW_PIN, false);

  LED_R.setTime(settings.FadeTime);
  LED_G.setTime(settings.FadeTime);
  LED_B.setTime(settings.FadeTime);
  LED_CW.setTime(settings.FadeTime);
  LED_WW.setTime(settings.FadeTime);

  LED_R.set(0);
  LED_G.set(0);
  LED_B.set(0);
  LED_CW.set(0);
  LED_WW.set(0);

}

void SRS_Ethernets::readTCP() {
Process_Incoming_Data();
}

void Process_Incoming_Data(){
        LED_R.set(DimR);
        LED_G.set(DimG);
        LED_B.set(DimB);
        LED_CW.set(DimCW);
        LED_WW.set(DimWW);
}

mainfile

const byte R_LED_PIN = 11;
const byte G_LED_PIN = 13;
const byte B_LED_PIN = 12;
const byte CW_LED_PIN = 6;
const byte WW_LED_PIN = 5;

void setup()
{
SRS_Config.beginLED(R_LED_PIN, G_LED_PIN, B_LED_PIN, CW_LED_PIN, WW_LED_PIN);
}

void loop(){
 SRS_Ethernet.readTCP();
}

My problem is that the objects LED_R LED_G LED_B … in the “Process_Incoming_Data()” function dont work.

Why and how can I fix this?

Regards
Steffen

since you declared the 'fadeled' objects 'locally' within .begin() they are not available outside of that function.

Deva_Rishi:
since you declared the 'fadeled' objects 'locally' within .begin() they are not available outside of that function.

Yes I know. That is the problem. But how can I make the opjects public in that function?
extern FadeLed LED_R(R_PIN, false); or public: FadeLed LED_R(R_PIN, false); dont work

Make the LED_R, ... instances part of your class:

class XXX
{
    private:
        FadeLED redLed;
        FadeLED greenLed;
        FadeLED blueLed;
   public:
        XXX(byte redLedPin, byte greenLedPin, byte blueLedPin)
            : redLed(redLedPin, false), greenLed(greenLedPin, false), blueLed(blueLedPin, false)
        {
            // Constructor code
        }
}

Thank you, but that dont work. I dont know why.
Even if I declare "FadeLed LED_R(R_PIN, false); ..... " outside function in the mainfile "LED_R ...." are not declared in other functions.

I spend a lot of hours the last days to search for a solution.
I think the best way is that anyone can look at the code and solve the problem.

So please can anyone can help me.
I think the best way would be via Teamviewer.
I will give you 50€ if this problem is solved.
Write me a PM

Thank you
Steffen

Post an MCVE that demonstrates your problem.

gfvalvo:
Post an MCVE that demonstrates your problem.

Please look at my first post.
It is still the same problem.

SReibert:
Please look at my first post.
It is still the same problem.

That code does not compile and hence is incomplete. So, I'll try again. Post an MCVE that demonstrates your problem.

gfvalvo:
That code does not compile and hence is incomplete. So, I’ll try again. Post an MCVE that demonstrates your problem.

Ok sorry. Here the copy and paste code:

You need the FadeLed library.

main.cpp

#include "SRS_Module.h"
#include <FadeLed.h>

const byte R_LED_PIN = 11;
const byte G_LED_PIN = 13;
const byte B_LED_PIN = 12;
const byte CW_LED_PIN = 6;
const byte WW_LED_PIN = 5;


void setup()
{
  BeginLED(R_LED_PIN, G_LED_PIN, B_LED_PIN, CW_LED_PIN, WW_LED_PIN);
}

void loop(){
  FadeLed::update();

  readTCP();
}

SRS_Module.h

#ifndef SRS_Module_h
#define SRS_Module_h
#include "Arduino.h"

void readTCP();
void Process_Incoming_Data();
void BeginLED(byte _R_LED_PIN, byte _G_LED_PIN, byte _B_LED_PIN, byte _CW_LED_PIN, byte _WW_LED_PIN);


#endif

SRS_Module.cpp

#include "SRS_Module.h"
#include "Arduino.h"
#include <FadeLed.h>

void BeginLED(byte _R_LED_PIN, byte _G_LED_PIN, byte _B_LED_PIN, byte _CW_LED_PIN, byte _WW_LED_PIN)
{
  pinMode(_R_LED_PIN, OUTPUT);
  pinMode(_G_LED_PIN, OUTPUT);
  pinMode(_B_LED_PIN, OUTPUT);
  pinMode(_CW_LED_PIN, OUTPUT);
  pinMode(_WW_LED_PIN, OUTPUT);

  FadeLed LED_R(_R_LED_PIN, false);
  FadeLed LED_G(_G_LED_PIN, false);
  FadeLed LED_B(_B_LED_PIN, false);
  FadeLed LED_CW(_CW_LED_PIN, false);
  FadeLed LED_WW(_WW_LED_PIN, false);

  LED_R.setTime(1);
  LED_G.setTime(1);
  LED_B.setTime(1);
  LED_CW.setTime(1);
  LED_WW.setTime(1);

  LED_R.set(0);
  LED_G.set(0);
  LED_B.set(0);
  LED_CW.set(0);
  LED_WW.set(0);
}

void readTCP() {
    Process_Incoming_Data();
}

void Process_Incoming_Data(){
    LED_R.set(125);  //error: 'LED_R' was not declared in this scope
    LED_G.set(125);  //error: 'LED_G' was not declared in this scope
    LED_B.set(125);  //error: 'LED_B' was not declared in this scope
    LED_CW.set(125);  //error: 'LED_CW' was not declared in this scope
    LED_WW.set(125);  //error: 'LED_WW' was not declared in this scope
}

OK, comparing this code to your original post, I see that the way you’re trying to do it with multiple files (or a class as you incorrectly tried in your first post) doesn’t really make much sense. The FadeLed object need to be accessible by multiple functions (i.e. global or class members). But, the FadeLed class constructors requires the pin number to be assigned when the object is instantiated. That limits how you can define the FadeLed objects.

The easiest way is just do it all in one file:

#include "Arduino.h"
#include <FadeLed.h>

void readTCP();
void Process_Incoming_Data();
void BeginLED();

const byte R_LED_PIN = 11;
const byte G_LED_PIN = 13;
const byte B_LED_PIN = 12;
const byte CW_LED_PIN = 6;
const byte WW_LED_PIN = 5;

FadeLed LED_R(R_LED_PIN, false);
FadeLed LED_G(G_LED_PIN, false);
FadeLed LED_B(B_LED_PIN, false);
FadeLed LED_CW(CW_LED_PIN, false);
FadeLed LED_WW(WW_LED_PIN, false);

void setup() {
  BeginLED();
}

void loop() {
  FadeLed::update();
  readTCP();
}

void BeginLED() {
  pinMode(R_LED_PIN, OUTPUT);
  pinMode(G_LED_PIN, OUTPUT);
  pinMode(B_LED_PIN, OUTPUT);
  pinMode(CW_LED_PIN, OUTPUT);
  pinMode(WW_LED_PIN, OUTPUT);

  LED_R.setTime(1);
  LED_G.setTime(1);
  LED_B.setTime(1);
  LED_CW.setTime(1);
  LED_WW.setTime(1);

  LED_R.set(0);
  LED_G.set(0);
  LED_B.set(0);
  LED_CW.set(0);
  LED_WW.set(0);
}

void readTCP() {
  Process_Incoming_Data();
}

void Process_Incoming_Data() {
  LED_R.set(125);
  LED_G.set(125);
  LED_B.set(125);
  LED_CW.set(125);
  LED_WW.set(125);
}

You could also do it by having a class with FadeLed pointers as members and then instantiate the FadeLed objects dynamically. Not sure it’s worth that much effort though.

gfvalvo:
I see that the way you're trying to do it with multiple files (or a class as you incorrectly tried in your first post) doesn't really make much sense.

Yes, but I decided to make a library because I had make different types of controller boards. One type is a LED Controller, one a binary switch, one with ethernet one with RS485 another one with Ethernet and RS485 and so on.
So my plan was to put all the stuff into a library and in the mainfile are only the pin definitinons and definitions about wich functions are needed. In the past I had for every board a single mainfile. If I changed i.E. the Ethernet code I had to change it in all other files that the code is in all files at the same level.

gfvalvo:
But, the FadeLed class constructors requires the pin number to be assigned when the object is instantiated. That limits how you can define the FadeLed objects.

Yes thats the problem. FadeLed LED_R; dont work

gfvalvo:
You could also do it by having a class with FadeLed pointers as members and then instantiate the FadeLed objects dynamically. Not sure it's worth that much effort though.

I think that would be the solution. But how? I searched for the keywords to find a solution but I am not able to put it into the code that it works.

Regards
Steffen

SReibert:
I think that would be the solution. But how? I searched for the keywords to find a solution but I am not able to put it into the code that it works.

I don't think so. To begin with, you need to separate the access to the led's to one library. Then, you don't use

LED_R.set(123);

but rather

ledLibrary.setGreenIntensity(123);

in functions that need to manipulate the led's.

Now for the actual FadeLED objects. You can make them member variables of your library, even if they do not have a default constructor. The initializer list in my post #3 is used to initialize the objects with their pin values.

Thank you for your answere.

My actual Version is that:

main.cpp

#include "SRS_Module.h"
#include <FadeLed.h>


const byte R_LED_PIN = 11;
const byte G_LED_PIN = 13;
const byte B_LED_PIN = 12;
const byte CW_LED_PIN = 6;
const byte WW_LED_PIN = 5;

Configs Config(R_LED_PIN, G_LED_PIN, B_LED_PIN, CW_LED_PIN, WW_LED_PIN);

void setup()
{
  
}

void loop(){
  FadeLed::update();

  Config.readTCP();
}

SRS_Module.h

#ifndef SRS_Module_h
#define SRS_Module_h
#include "Arduino.h"
#include <FadeLed.h>

void readTCP();
void Process_Incoming_Data();

class Configs
{
    private:
        FadeLed LED_R;
        FadeLed LED_G;
        FadeLed LED_B;
        FadeLed LED_CW;
        FadeLed LED_WW;
   public:
        Configs(byte _R_LED_PIN, byte _G_LED_PIN, byte _B_LED_PIN, byte _CW_LED_PIN, byte _WW_LED_PIN)
            : LED_R(_R_LED_PIN, false), LED_G(_G_LED_PIN, false), LED_B(_B_LED_PIN, false), LED_CW(_CW_LED_PIN, false), LED_WW(_WW_LED_PIN, false)
        {
            pinMode(_R_LED_PIN, OUTPUT);
            pinMode(_G_LED_PIN, OUTPUT);
            pinMode(_B_LED_PIN, OUTPUT);
            pinMode(_CW_LED_PIN, OUTPUT);
            pinMode(_WW_LED_PIN, OUTPUT);

            LED_R.setTime(1);
            LED_G.setTime(1);
            LED_B.setTime(1);
            LED_CW.setTime(1);
            LED_WW.setTime(1);

            LED_R.set(0);
            LED_G.set(0);
            LED_B.set(0);
            LED_CW.set(0);
            LED_WW.set(0);
        }
        void readTCP();
        void Process_Incoming_Data();
};

#endif

SRS_Module.cpp

#include "SRS_Module.h"
#include "Arduino.h"
#include <FadeLed.h>


void Configs::readTCP() {
    Process_Incoming_Data();
}

void Configs::Process_Incoming_Data(){
    LED_R.set(125);
    LED_G.set(125);
    LED_B.set(125);
    LED_CW.set(125);
    LED_WW.set(125);
}

And there no errors :slight_smile:
Thank you very much.

Regards
Steffen