For debugging purposes I want to print certain variables in a library .cpp code. I want to print the values during execution of the code, not return a value after code has executed.
Per a post read I tried adding #include <arduino.h> to the .cpp code and using Serial.print, with no success.
The only way to do this is to edit the library .cpp code and insert a print statements on it. To use a Serial.print() methods inside the library you have to pass a pointer to the Serial object to the library function.
Addition - of course, in general you can see a state of variables running the code on the debugger session. Unfortunately, a on-chip debugging for a classic AVR mcu is not supported.
If you use Serial and included Arduino.h then it will known.
Where it wonβt work is if you try to print from the constructor of a global instance as the constructor is called way before setup (and thus Serial.begin())is called β the board is not configured yet).
#include "mylib.h"
myLib me;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.print("Ready");
me.begin();
}
void loop() {
// put your main code here, to run repeatedly:
me.update();
}
You can also add an additional, private variable to the class, called debug or something similar and another method that toggles that variable true or false and make all your Serial.print statements conditional on that variable.
main() has not been called either, so the hardware init() function has not be called either and as you said the Serial instance might not even be there.
Bigger picture-
I am trying to get the Adafruit RFM69 module to work using an Arduino Uno I have on-hand and using the Low Power Lab library. Like so many others who have posted on various forums, I am having a hard time getting this combo to work. I do have this combo working using the Radio Head library.
I got the Serial.print to work within the LPL .cpp library file. This helped me zero in on the SPI interface as the problem area.