Hey!
I wanted to dip my toes in creating custom libraries and created a dummy library to test things out. Sure enough I ran into a problem: the only thing the library is supposed to do is write some text via serial. However it only prints 2 characters.
.ino:
#include <MyLibrary.h>
MyLibrary myLib;
void setup() {
myLib.function1();
}
void loop() {}
MyLibrary.h:
#ifndef MyLibrary_h
#define MyLibrary_h
#include "Arduino.h"
class MyLibrary
{
public:
MyLibrary();
void function1();
void function2();
};
#endif
MyLibrary.cpp:
#include "Arduino.h"
#include "MyLibrary.h"
MyLibrary::MyLibrary()
{
Serial.begin(9600);
Serial.println("Instance was created - constructor called");
}
void MyLibrary::function1()
{
Serial.println("Function1 was called");
}
void MyLibrary::function2()
{
Serial.println("Function2 was called");
}
output:
In
Can someone help me out? Thanks in advance (=
Would it be too hard to tell us which 2 characters it printed?
You can't run code in a constructor. Make yourself a begin or init method for that.
aarg:
Would it be too hard to tell us which 2 characters it printed?
You can't run code in a constructor. Make yourself a begin or init method for that.
Just to clarify, you most certainly can run code in a constructor (that is what a constructor is... code) but you have to be mindful of the fact that your Arduino's hardware may not be ready, as you observed.
if you want to do something that depends on hardware readiness, use aarg's advice above.
aarg:
Would it be too hard to tell us which 2 characters it printed?
You can't run code in a constructor. Make yourself a begin or init method for that.
Would it be too hard to read my post in its entirety? I did tell you...
Why can't I run code in the constructor?
Do you mean I should call the init() function in the constructor? If so - I tried that with the same result.
BulldogLowell:
Just to clarify, you most certainly can run code in a constructor (that is what a constructor is... code) but you have to be mindful of the fact that your Arduino's hardware may not be ready, as you observed.
if you want to do something that depends on hardware readiness, use aarg's advice above.
Ah I see! Thanks for your answer. I called the init() function in the setup function of my ino. file and now eveything works fine (=
MarkGoingToSpace:
Ah I see! Thanks for your answer. I called the init() function in the setup function of my ino. file and now eveything works fine (=
of course, you can call a constructor any time....
this issue exists in particular when a global instance is created, as you did in your example.
another way of doing this would be to create a no-argument constructor for use in global space and then call an overload of the constructor in setup()...
here is a quick example:
class MyLibrary
{
public:
MyLibrary(){};
MyLibrary(HardwareSerial& stream, int baud);
void function1();
void function2();
};
MyLibrary::MyLibrary(HardwareSerial& stream, int baud)
{
stream.begin(baud);
stream.println("C'tor called");
}
void MyLibrary::function1()
{
Serial.println("Function1 was called");
}
void MyLibrary::function2()
{
Serial.println("Function2 was called");
}
MyLibrary myLib;
void setup()
{
myLib = MyLibrary (Serial, 9600);
myLib.function1();
myLib.function2();
}
void loop() {}