how to make and use a class

Is it possible to make a class in a .pde file? Something like this processing example? http://webzone.k3.mah.se/projects/arduino-workshop/projects/arduino_meets_processing/instructions/accelero.html
When I make a new tab in which I make a class arduino say's: "error: 'OneWireLib' does not name a type" Here is "OneWireLib" the name of the .pde file and the class.
For now the class is virtually empty:

class OneWireLib
{
OneWireLib() 
{
  int y =1;
}
void ConvertTemp()
{
  int x =1;
}
}

In my main program I call the class like this:

OneWireLib owl;

I see a few things that might help:

  • Make sure you put a semicolon ';' after the final '}' in the class declaration.
  • you will need to make some things "public" if you access them from outside the class. By default, members of a class are "private".
  • You should declare variables as members of the class if they are part of the object.

Here is an example of what I mean:

class OneWireLib
{
public:
    OneWireLib() 
    {
        int y =1;       // this 'y' will go away once the constructor returns (it is a local variable)
        somevar = 99;     // initialize class member variables here in the constructor
    }

    void ConvertTemp()
    {
         int x =1;
    }

    int GetSomeVar()    // example of method for using private data
    {
        return somevar;
    }

private:
     int somevar;      // put variables that are part of the object here
};        // <===  note semicolon here... terminates the class declaration

OneWireLib  onewire;     // declare an instance of the OneWireLib class

void setup()
{
    int x = onewire.GetSomeVar();     // example of calling a class method
}

I tried you're example using a second tab and it doesn't work. This is the error:
error: 'OneWireLib' does not name a type In function 'void test()':

But, if I try it in one file it does work. What's the difference? I tried to #include the file but it is not found.

Please post your code and I will see if I can help.

The mainfile testclass.pde:

// #include "onewire.pde"

OneWireLib  onewire;     // declare an instance of the OneWireLib class

void setup()
{
    int x = onewire.GetSomeVar();     // example of calling a class method
}
void loop()
{
}

and the class file onewire.pde:

class OneWireLib
{
public:
    OneWireLib() 
    {
        int y =1;       // this 'y' will go away once the constructor returns (it is a local variable)
        somevar = 99;     // initialize class member variables here in the constructor
    }

    void ConvertTemp()
    {
         int x =1;
    }

    int GetSomeVar()    // example of method for using private data
    {
        return somevar;
    }

private:
      int somevar;      // put variables that are part of the object here
};        // <===  note semicolon here... terminates the class declaration

the onewire.pde file is in the same directory as the testclass.pde file.
And again, all this code in one file does work.

OK, now I understand. I too have had problems trying to #include a header file in the same directory as my sketch. Looks like Arduino does NOT put the sketch directory inside the C++ include path. But it looks like maybe you are trying to make a reusable library. Try taking a look at this:

I didn't start the thread for a reusable library. It was more to get my code more readable. So I figured that a class in another tab would be helpful. After reading this http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1167857708 thread I figured it would be easy to do. :slight_smile:

If the #include statement uses "double quotes", the compiler will check the directory that the including file is in (i.e. the sketch directory). You just need to add to your sketch a .h file with the appropriate contents, and place the #include statement at the top of the main sketch file, and you should be fine.

But is this .h necessary? If you start a new tab the file will be saved as a .pde file. So, I expected that a #include "onewire.pde" would work, but it doesn't.

Okay, I tried it. If I rename the onewire.pde to onewire.h and I use "#include "onewire.h" it works. And it gets better, if I save the file als foo.h it won't get the pde extension. But this might be something platform specific? (I am on OSX)
So, only one question left: why does .h work and .pde not?