I m trying to program my arduino Mega 2560.
I created two libraries : 1 called "Relay" and an other called "shutter".
The second library use the first library in its code. But I ve got this message error :
Shutter\Shutter.cpp.o: In function `Shutter':
C:\Program Files (x86)\Arduino\libraries\Shutter/Shutter.cpp:11: undefined reference to `Relay::Relay(int)'
C:\Program Files (x86)\Arduino\libraries\Shutter/Shutter.cpp:11: undefined reference to `Relay::Relay(int)'
I think I don't have any error in my library called Ralay because I have already tested it. So I think that it's an error in my constructor of my class shutter.
#ifndef SHUTTER_h
#define SHUTTER_h
#include "Arduino.h"
#include "..\Relay\relay.h"
class Shutter // Object Shutter
{
public :
Shutter(int pinRelay1, int pinRelay2, int pinBouton);
void open(); //open the shutter
void close(); //close the shutter
bool isPushed(); //Is the button of the shutter pushed ? 1 if yes 0 if no
bool isActivated(); //Is the button of the shutter activated ? 1 if yes 0 if no
void setActivated(boolean a); //Change the value of _activated
void stopAction(); //Some times after using the open or close method, you should use this method.
private :
boolean _activated;
int _pinBouton;
Relay _relay1;
Relay _relay2;
};
#endif
@pYro_65 : Yes, It s not reallly clear. My library Shutter is composed of Shutter.cpp and Shutter.h
and the librairy Relay is composed of relay.cpp and relay.h
#ifndef RELAY_h
#define RELAY_h
#include "Arduino.h"
class Relay // Objet Relay
{
private:
int _pin;
boolean _isOFF;
public:
Relay(int pin);
void off(); //Put in position close
void on(); //Put in position open
bool isStateOFF() const; //Return TRUE if the relay is open
void switchState(); //switch the relay position
};
#endif
It's good practice on the Arduino that doing stuff with hardware (such as pinMode()) should not be included in an object constructor. The object could be created before the Arduino environment has been initialised and the hardware calls may therefore fail.
The recommended approach is to add a public begin() method to the class, move the hardware-related code into it, and call it from setup(), at which time the Arduino environment will be active.
EDIT
I have attached a simple example of an object inside an object doing hardware stuff and use of the begin() method. LEDhandler is the lower-level object (like your relay) and Toggler is the higher-level (like your Shutter).
Your example was very usefull ! I did not think about the problem of the function pinMode()... My bad.
Just a quote, I must not use the pointer. I post my new files :
shutter.h :
#ifndef SHUTTER_h
#define SHUTTER_h
#include "Arduino.h"
#include "..\Relay\relay.h"
class Shutter // Object Shutter
{
public :
Shutter(int pinRelay1, int pinRelay2, int pinBouton);
void begin();
void open(); //open the shutter
void close(); //close the shutter
bool isPushed(); //Is the button of the shutter pushed ? 1 if yes 0 if no
bool isActivated(); //Is the button of the shutter activated ? 1 if yes 0 if no
void setActivated(boolean a); //Change the value of _activated
void stopAction(); //Some time after using the open or close method, you should use this method.
private :
boolean _activated;
int _pinBouton;
Relay _relay1;
Relay _relay2;
};
#endif