problem with a class

Hi, I'm trying to make a class called le_led which will contain infos about a "pixel". I read the arduino tutorials and searched in the forums but I can't understand why it doesn't work.

My project have 4 files ("tabs") and the class is not declared in the main file. When I do the following in the main file:

le_led pixel01(byte(0), byte(0), byte(0));

I get the following error:

_04_Bi_Protocolo:49: error: 'le_led' does not name a type

I also noticed the the constructor takes a strange gray color.

Can somebody help me??? :wink:

class le_led
{
public:
le_led(byte led_in_r, byte led_in_g, byte led_in_b);
void change_r(byte led_in_r);
void change_g(byte led_in_g);
void change_b(byte led_in_b);
void change(byte led_in, int led_rgb);
byte get_r();
byte get_g();
byte get_b();
void get(int led_rgb);
private:
byte led_r;
byte led_g;
byte led_b;
};

le_led::le_led(byte led_in_r, byte led_in_g, byte led_in_b){

led_r = led_in_r;
led_g = led_in_g;
led_b = led_in_b;

}

void le_led::change_r(byte led_in_r) { led_r = led_in_r; }
void le_led::change_g(byte led_in_g) { led_g = led_in_g; }
void le_led::change_b(byte led_in_b) { led_b = led_in_b; }

byte le_led::get_r() { return led_r; }
byte le_led::get_g() { return led_g; }
byte le_led::get_b() { return led_b; }

That there are 4 tabs does not mean that the code on each tab gets compiled (or that the main sketch is even aware of the other tabs).

In the main sketch, you need to #include the header file for the class. Have you done that?

By the way, mixing code and definitions in the same file is generally frowned on. There should be a separate .cpp file with the function implementations in that file.

hi,

thanks for your help.

does this mean that every class in arduino should be a library???

I didn't that with #include as I thought that the tabs were automatically compiled... :frowning:

makes sense then to include a file that is already a tab??

"#include thefile.pde"

(please note the .pde)

does this mean that every class in arduino should be a library???

No. A library is a shared class. If the class you are developing is single use, there is no reason to make it a library.

I didn't that with #include as I thought that the tabs were automatically compiled.

Some tabs are automatically compiled - the first tab and those with the .pde extension on the file displayed in the tab. Others are not.

#including a file in a sketch ensures that the corresponding .cpp file gets compiled.

@velegno:

I thought that the tabs were automatically compiled

It is true that all of the .cpp tabbed files are compiled and linked together by Arduino.

However...

The compiler compiles each .cpp file separately. For each .cpp file, the compiler does not know what goes on in other .cpp files unless you tell it, usually by including headers that contain typedefs, class definitions, function prototype declarations, external variable declarations, etc.

Let's start fresh. Here's one way to do it:

Open Arduino and create a new tab called le_led.h

Put your class definition stuff in this tab:

//
//le_led.h
//
// Need to include Arduino core header WProgram.h so that typedef
// for byte is known
//
#include <WProgram.h>

class le_led
{
  public:
    le_led(byte led_in_r, byte led_in_g, byte led_in_b);
    void change_r(byte led_in_r);
    void change_g(byte led_in_g);
    void change_b(byte led_in_b);
    void change(byte led_in, int led_rgb);
    byte get_r();
    byte get_g();
    byte get_b();
    void get(int led_rgb);
  private:
    byte led_r;
    byte led_g;
    byte led_b;
};

Create another tab. Call it le_led.cpp and put your class implementation stuff there:

//
// le_led.cpp
//
// Include le_led.h so that class definition is known
//
#include "le_led.h"

le_led::le_led(byte led_in_r, byte led_in_g, byte led_in_b){
  
  led_r = led_in_r;
  led_g = led_in_g;
  led_b = led_in_b;
  
}

void le_led::change_r(byte led_in_r) { led_r = led_in_r; }
void le_led::change_g(byte led_in_g) { led_g = led_in_g; }
void le_led::change_b(byte led_in_b) { led_b = led_in_b; }

byte le_led::get_r() { return led_r; }
byte le_led::get_g() { return led_g; }
byte le_led::get_b() { return led_b; }

Now, in the main "sketch" tab put your application code:

//
// Include the header for the class definition
//
#include "le_led.h"

//
// Invoke the constructor to create an le_led object.
//
le_led leds(0, 0, 0); // Start with all equal to zero

void setup()
{
    Serial.begin(9600);
}

void loop()
{
    // Maybe you want to print the numerical values as (r,g,b)
    //
    Serial.print("leds: (");
    Serial.print(leds.get_r(), DEC);
    Serial.print(",");
    Serial.print(leds.get_g(), DEC);
    Serial.print(",");
    Serial.print(leds.get_b(), DEC);
    Serial.println(")");
    //
    // Whatever you want to do
    //
    

}

Regards,

Dave

it worked now thanks!!!! :slight_smile: :slight_smile: :slight_smile: