I found that some code won't work unless declared on the header file itself. For instance, with the following code, "Hello World" will be printed when test() is called:
//Myclass.h
class Myclass {
private:
SoftwareSerial *ss;
public:
void test() {
ss = & SoftwareSerial(0,1);
ss->begin(9600);
ss->print("Hello World");;
};
};
But if I just declare the method test() on the header, but code it on a separate cpp as usual with the exact same code, it compiles but doesn't output anything.
You must create a constructor that gets an instance of SoftSerial passed as an argument.
Why? I mean, why can't I create a new instance INSIDE my new class? (I'm a Java developer, if that helps). More specifically, why can I create it in the header file BUT NOT in the .cpp??
I'm very interested because, even though in this case I found a workaround (as I said, by coding directly into the header file) I have no clue why it works in one way and not the other, so I'm afraid I'll walk into the same problem again.
Why are you doing software serial on the hardware serial port?
Because the hardware serial port in the Arduino UNO comes with a serial-to-usb converter so I can debug its output from my computer using the serial monitor that comes with the arduino IDE. Of course, once it works I'm planning on using it on a different port.
The code you posted, whether in the header file or the source file, makes little sense.
Why are you creating a new instance of software serial every time the function gets called? It would make sense to create ONE instance, when the class gets instantiated, not every time the method gets called.
You still haven't explained why you are doing software serial on the hardware serial pins.
Why are you creating a new instance of software serial every time the function gets called
It's a test (you know, the funcion's name is "test"). I created the test only to explore why the initialization of SoftwareSerial worked when declared on the header but not on the .cpp. That's why I don't even bother to delete that instance.
You still haven't explained why you are doing software serial on the hardware serial pins.
Yes I did:
Because the hardware serial port in the Arduino UNO comes with a serial-to-usb converter so I can debug its output from my computer using the serial monitor that comes with the arduino IDE. Of course, once it works I'm planning on using it on a different port.
The easiest way to debug the output of SoftwareSerial is of course to read it directly through the serial monitor, and that's what I'm doing. The pins have hardware serial capabilities, but that doesn't mean that you HAVE to use that capabilities.