39

I'm trying to use class objects with the Arduino, but I keep running into problems. All I want to do is declare a class and create an object of that class. What would an example be?

Peter Mortensen
31.5k22 gold badges110 silver badges134 bronze badges
asked Nov 14, 2009 at 23:36

6 Answers 6

22

On Arduino 1.0, this compiles just fine:

class A
{
 public:
 int x;
 virtual void f() { x=1; }
};
class B : public A
{
 public:
 int y;
 virtual void f() { x=2; }
};
A *a;
B *b;
const int TEST_PIN = 10;
void setup()
{
 a=new A(); 
 b=new B();
 pinMode(TEST_PIN,OUTPUT);
}
void loop()
{
 a->f();
 b->f();
 digitalWrite(TEST_PIN,(a->x == b->x) ? HIGH : LOW);
}
answered Oct 6, 2012 at 15:14

3 Comments

Notice, I do not have a virtual destructor --- building dynamic objects seems okay, but deletion could lead to fragmentation, so I'm not convinced it is a good idea to do more than dynamically configure your environment.
shouldn't line 11 be int x; not int y;?
No, objects of type B just have both an x and y property.
14

There is an excellent tutorial on how to create a library for the Arduino platform. A library is basically a class, so it should show you how its all done.

On Arduino you can use classes, but there are a few restrictions:

  • No new and delete keywords
  • No exceptions
  • No libstdc++, hence no standard functions, templates or classes

You also need to make new files for your classes, you can't just declare them in your main sketch. You also will need to close the Arduino IDE when recompiling a library. That is why I use Eclipse as my Arduino IDE.

Wander Nauta
19.7k1 gold badge49 silver badges65 bronze badges
answered Nov 16, 2009 at 16:04

2 Comments

new/delete is just fine on Arduino 1.0.6
Declaring classes in your main sketch is fine in v2.0.0.
7

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1230935955 states:

By default, the Arduino IDE and libraries does not use the operator new and operator delete. It does support malloc() and free(). So the solution is to implement new and delete operators for yourself, to use these functions.

Code:

#include <stdlib.h> // for malloc and free
void* operator new(size_t size) { return malloc(size); } 
void operator delete(void* ptr) { free(ptr); }

This let's you create objects, e.g.

C* c; // declare variable
c = new C(); // create instance of class C
c->M(); // call method M
delete(c); // free memory

Regards, tamberg

Grhm
6,8944 gold badges42 silver badges65 bronze badges
answered Jan 14, 2010 at 14:09

Comments

4

I created this simple one a while back. The main challenge I had was to create a good build environment - a makefile that would compile and link/deploy everything without having to use the GUI. For the code, here is the header:

class AMLed
{
 private:
 uint8_t _ledPin;
 long _turnOffTime;
 public:
 AMLed(uint8_t pin);
 void setOn();
 void setOff();
 // Turn the led on for a given amount of time (relies
 // on a call to check() in the main loop()).
 void setOnForTime(int millis);
 void check();
};

And here is the main source

AMLed::AMLed(uint8_t ledPin) : _ledPin(ledPin), _turnOffTime(0)
{
 pinMode(_ledPin, OUTPUT);
}
void AMLed::setOn()
{
 digitalWrite(_ledPin, HIGH);
}
void AMLed::setOff()
{
 digitalWrite(_ledPin, LOW);
}
void AMLed::setOnForTime(int p_millis)
{
 _turnOffTime = millis() + p_millis;
 setOn();
}
void AMLed::check()
{
 if (_turnOffTime != 0 && (millis() > _turnOffTime))
 {
 _turnOffTime = 0;
 setOff();
 }
}

It's more prettily formatted here: http://amkimian.blogspot.com/2009/07/trivial-led-class.html

To use, I simply do something like this in the .pde file:

#include "AM_Led.h"
#define TIME_LED 12 // The port for the LED
AMLed test(TIME_LED);
Peter Mortensen
31.5k22 gold badges110 silver badges134 bronze badges
answered Nov 16, 2009 at 15:57

2 Comments

How would you pass test as a parameter for another function? What does the function signature look like for this kind of class? You're not using new AMLed(TIME_LED), so how would AMLed test(TIME_LED) look? I'm asking because I have two objects, both of type AMLed, and I want to reference them in a function.
@SamuelClay AMLed test(TIME_LED) line calls the constructor. There is no need for new as the object is already created.
2

My Webduino library is all based on a C++ class that implements a web server on top of the Arduino Ethernet shield. I defined the whole class in a .h file that any Arduino code can #include. Feel free to look at the code to see how I do it... I ended up just defining it all inline because there's no real reason to separately compile objects with the Arduino IDE.

answered Nov 18, 2009 at 2:24

Comments

1

Can you provide an example of what did not work? As you likely know, the Wiring language is based on C/C++, however, not all of C++ is supported.

Whether you are allowed to create classes in the Wiring IDE, I'm not sure (my first Arduino is in the mail right now). I do know that if you wrote a C++ class, compiled it using AVR-GCC, then loaded it on your Arduino using AVRDUDE, it would work.

Peter Mortensen
31.5k22 gold badges110 silver badges134 bronze badges
answered Nov 15, 2009 at 4:26

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.