At this moment i'm learning how to write a library, to make my code look a lot cleaner.
However, when i was making my library, i need to make use of another library called #include <SoftwareSerial.h>
But how do initialise an instance of this library inside my class?
My header file
#ifndef Verzenden_h
#define Verzenden_h
#include "Arduino.h"
#include <SoftwareSerial.h>
class Verzenden
{
public:
Verzenden(int rxPin, int txPin);
void send(String data);
private:
int _rxPin;
int _txPin;
int _setPin;
int _enPin;
int _fiveV;
SoftwareSerial _APC(int rxPin, int txPin);
};
#endif
sketch\Verzenden.cpp: In constructor 'Verzenden::Verzenden(int, int)':
Verzenden.cpp:14: error: '((Verzenden*)this)->Verzenden::_APC' does not have class type
_APC.begin(9600);
^
sketch\Verzenden.cpp: In member function 'void Verzenden::send(String)':
Verzenden.cpp:18: error: '((Verzenden*)this)->Verzenden::_APC' does not have class type
_APC.send("test");
^
exit status 1
'((Verzenden*)this)->Verzenden::_APC' does not have class type
Here's one way. I did it all in a single .ino file because it's faster to check. But, you can/should split it into .h, .cpp, and .ino files for actual use as a library.
BTW, others will surely chime in to tell you that using the String class is stupid. Go with c-strings (i.e. null-terminated char arrays).
#include "Arduino.h"
#include <SoftwareSerial.h>
class Verzenden
{
public:
Verzenden(int rxPin, int txPin);
void begin(uint16_t baud);
void send(const String &data);
private:
int _rxPin;
int _txPin;
int _setPin;
int _enPin;
int _fiveV;
SoftwareSerial _APC;
};
Verzenden::Verzenden(int rxPin, int txPin) : _APC(rxPin, txPin) {}
void Verzenden::begin(uint16_t baud) {
_APC.begin(baud);
}
void Verzenden::send(const String &data) {
_APC.print(data);
}
Verzenden xxx(4, 5);
void setup() {
xxx.begin(9600);
xxx.send("Test");
}
void loop() {}