Printing values in library .cpp during library execution

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.

Suggestions please.

Please provide your code. Both the library and your example that calls the library that "didn't work."

Did your example initialize Serial with Serial.begin(9600)

1 Like

See the pinned post re 'How to get the most from the forum'

Please think of this as a generic question. Not specific to my program-of-the-day code.
Perhaps I need to go back to my Idiots Guide to C++ book?

If you post the code, people can show you a fix that should work for most cases.

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).

1 Like

First, capitalization counts. It's:

#include <Arduino.h>

Second, it's likely unnecessary as the .h file associated with the .cpp probably already does that.

Concrete example:
sketch.ino

#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();
}

myLib.h

#ifndef myLib_h
#define myLib_h
#include "Arduino.h"

class myLib
{
  public:
    myLib();
    void begin();
    void update();
    
  
  private:
    unsigned long lastTime;
};

#endif

myLib.cpp


#include "myLib.h"
#include <Arduino.h>

myLib::myLib()
{
}

void myLib::begin()
{
  Serial.println("myLib::begin()");
  lastTime = millis();
}

void myLib::update()
{
  if ( millis() - lastTime > 1000 ) {
    lastTime = millis();
    Serial.println("Updating myLib");
  }
}

Serial Monitor Output

21:02:31.322 -> ReadymyLib::begin()
21:02:32.344 -> Updating myLib
21:02:33.322 -> Updating myLib
21:02:34.349 -> Updating myLib
21:02:35.326 -> Updating myLib
21:02:36.320 -> Updating myLib
21:02:37.359 -> Updating myLib
21:02:38.313 -> Updating myLib
21:02:39.354 -> Updating myLib
21:02:40.346 -> Updating myLib
21:02:41.339 -> Updating myLib
21:02:42.335 -> Updating myLib
...

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.

.

post a generic example showing your need of debug information and helpers can provide you suggestions which will work.

My money would be on an attempt to print from the constructor...

1 Like

Thank you b707. I like knowing the "why" (print from constructor...).

Thank you blh64. Your example is helpful.

you still haven't shown your code

Can be dangerous for objects declared global before setup().
You don't know if the Serial object has been created already...

And you actually know that begin() has not been called since the setup() has not run, so it won't work anyway.

You could try to call Serial.begin() in the constructor, which might work.
Alas, this trick burned my evening (debugging) several years ago.

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.

That's what begin() functions are for :slight_smile:

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.

Could be @bobthebulder is using Windows. Doesn't Windows treat file names case-insentitive?