My effort to create a class with both .h and .cpp files isn't working and it appears the constructor is not being called. I have spent a lot of time trying to figure this out, but still don't know why it isn't working.
Can someone please tell me why the prints in the constructor in the .cpp file do not appear on the serial monitor?
The three files are shown below and all three are included in a .zip file.
#include <arduino.h>
#include "Obstacle.h"
const int16_t CENTER = 88;
const int16_t SCANMAX = CENTER + 50;
const int16_t SCANMIN = CENTER - 50;
const int16_t MIN_DISTANCE = 400;
const int16_t LEFT = 0;
const int16_t RIGHT = 1;
int16_t OBSTACLE::GetDist()
{
return _dist;
}
int16_t OBSTACLE::GetDir()
{
return _dir;
}
OBSTACLE::OBSTACLE(int16_t servoPin) // constructor
{
Serial.println("a");
Serial.println(servoPin);
Serial.println("a");
srv.attach(servoPin);
}
boolean OBSTACLE::Tick()
{
static int16_t p = CENTER;
static int16_t incDec = 4;
int16_t mm;
p += incDec; // inc or dec depencing on value of incDec
if (p > SCANMAX) // if we exceed the max
{
p = SCANMAX - 4; // set it to the max
incDec = -4; // start decrimenting
}
if (p < SCANMIN) // if we are below min
{
p = SCANMIN + 4; // start incrementing
incDec = 4;
}
OBSTACLE::ServoWrite(p); // move the servo
_dist = mm; // set Dist
if (p < 90) // set dir left or right
_dir = LEFT;
else
_dir = RIGHT;
if (mm < MIN_DISTANCE) // is there an obstacle?
return true;
else
return false;
}
void OBSTACLE::ServoWrite(int16_t pos)
{
srv.write(pos);
}
Because the constructor runs before the hardware is configured to print and way before setup() sets the baud rate. Lesson Learned: Don't try to print from the constructor.
@PickyBiker the usual workaround for this is to give your class a begin() method which you then call in setup(), after Serial.begin(), to print anything you need to. Have the constructor simply save any parameter values as object attributes, private if you like, and then you can print their values in the begin() method.
This brings me to another question: Why doesn't the servo move? That is what I was debugging before I mistakenly believed the constructor was not executing.
@PickyBiker
I would debug your sketch of post #1 in the following way based on a single file version to turn the Servo back-and-forth at 2-sec interval. After that, I would go for three files version and add other operational/control features for the Servo.
Based on GolamMostafa's suggestion, here is the code without the need for pointers. It simply inserts the begin function in the class that is called from the Setup when everything is configured.
You have a begin() method for Serial object. I would suggest to change the Begin() method for ob object to servoInit() (or something else) in order to avoid confusion.
I have a long standing style preference that uses capitalized procedure names whether they are in a class or not. If you look at the zip file I posted you will see that style is used throughout the code.
Using Begin in this particular case was simply because PaulRB suggested begin() and I saw no reason to change that name other than to capitalize it per my adopted style. Had I come up with the name on my own, I likely would have used InitServo or InitOBSTACLE which I admit is a lousy name for initializing a servo class
In any case a big thank you to all who responded with helpful comments and suggestions.