PaulS:
Now to try to find out why it doesn't output anything to the port.
Well, that's easy. When is your class constructor called? When is the HardwareSerial constructor called? You don't know, do you? Therefore, you should not write code that relies on them being called in the correct order.
You need a begin() method in your class. You pass nothing to the constructor. You pass a serial instance to the begin method. You call the begin method in the setup() function.
So I'm trying to learn and I appreciate your patience and help, but I don't quite follow the recommendation to pass nothing to the constructor. I went to the C++ tutorial website here: http://www.cplusplus.com/doc/tutorial/ and reviewed examples of constructors and destructors for classes. I tested a few things. I Thought the constructor, being outside of setup() or loop(), is called first. and therefore passing the Serial object to the library when declaring an instance seemed reasonable.
I tried to construct an example where I did not pass Serial in the constructor, but I could not get it to compile. However I was successful in creating an example where I pass Serial in the constructor (for the instance myLibrary) and later calling a begin() function.
This code works, Is there some reason it is incorrect?
.ino
#include "MyLibrary.h"
MyLibrary myLibrary(Serial);
void setup() {
myLibrary.begin();
myLibrary.test();
}
void loop() {
myLibrary.test();
delay(1000);
}
.cpp
#include "MyLibrary.h"
#include "HardwareSerial.h"
//pass a reference to a Print object
MyLibrary::MyLibrary( HardwareSerial &print ) {
printer = &print; //operate on the address of print
}
void MyLibrary::begin() {
printer->begin(115200);
}
void MyLibrary::test() {
printer->println("Hello library with serial connectivity!");
}
and .h
#ifndef _MYLIBRARY_H
#define _MYLIBRARY_H
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
class MyLibrary {
public:
//pass a reference to a Print object
MyLibrary(HardwareSerial &print);
void begin( );
void test();
private:
HardwareSerial* printer;
};
#endif
My Other code does not But I was unsure what to put in the coonstructor I tried it 2 ways, with nothing (assuming the pointer to HardwareSerial has storage already assigned so it could safely be pointed to, and one declaring storage for the Hardware serial, but I get compiler errors trying to pass Serial in the begin call.
Here's the last round of my attempt to not pass anything in the constructor and pass the reference in the call to begin. I'm not following how to code what you suggested.
.ino
#include "MyLibrary.h"
MyLibrary myLibrary();
void setup() {
myLibrary.begin(Serial);
myLibrary.test();
}
void loop() {
myLibrary.test();
delay(1000);
}
.cpp
#include "MyLibrary.h"
#include "HardwareSerial.h"
//pass a reference to a Print object
MyLibrary::MyLibrary( ) {
printer = new HardwareSerial;
}
MyLibrary::~MyLibrary( ) {
delete printer;
}
void MyLibrary::begin(HardwareSerial &print) {
printer = &print; //operate on the address of print
printer->begin(115200);
}
void MyLibrary::test() {
printer->println("Hello library with serial connectivity!");
}
.h
#ifndef _MYLIBRARY_H
#define _MYLIBRARY_H
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
class MyLibrary {
public:
//pass a reference to a Print object
MyLibrary();
~MyLibrary();
void begin(HardwareSerial &print);
void test();
private:
HardwareSerial* printer;
};
#endif
Errors:
HardwareSerialClassExampleSplit_V2.cpp: In function 'void setup()':
HardwareSerialClassExampleSplit_V2:6: error: request for member 'begin' in 'myLibrary', which is of non-class type 'MyLibrary ()()'
HardwareSerialClassExampleSplit_V2:7: error: request for member 'test' in 'myLibrary', which is of non-class type 'MyLibrary ()()'
HardwareSerialClassExampleSplit_V2.cpp: In function 'void loop()':
HardwareSerialClassExampleSplit_V2:10: error: request for member 'test' in 'myLibrary', which is of non-class type 'MyLibrary ()()'
Note if the example I posted first is 'safe' I'm happy to code it per that example. if there is something inherently dangerous in that approach even though it works please point that out. - Thanks again - Kb