passing a value to a library question

Good day everyone!

I’m trying to figure out how to pass a boolean value to a library. I started out by using the ‘morse’ library example code found here: Arduino - LibraryTutorial

Which works. I added in some code to print data to the serial console and now I’m seeking a way to control if that data is printed or not. Here’s my arduino skectch:

#include <Morse.h>

Morse morse(A5);    // led pin is A5
Morse Debug(true);  // debug flag

void setup()
{
  while (!Serial);
  
  Serial.begin(115200);
  
}

void loop()
{
  morse.dot(); morse.dot(); morse.dot();
  morse.dash(); morse.dash(); morse.dash();
  morse.dot(); morse.dot(); morse.dot();
  delay(3000);
}

My Morse.h file is:

/*
  Morse.h - Library for flashing Morse code.
  Created by David A. Mellis, November 2, 2007.
  Released into the public domain.
*/
#ifndef Morse_h
#define Morse_h

#include "Arduino.h"

class Morse
{
  public:
    Morse(int pin);
    Debug(boolean debug);
    void dot();
    void dash();
  private:
    int _pin;
    boolean _debug;
};

#endif

and my Morse.cpp file is:

/*
  Morse.cpp - Library for flashing Morse code.
  Created by David A. Mellis, November 2, 2007.
  Released into the public domain.
*/

#include "Arduino.h"
#include "Morse.h"

Morse::Morse(int pin)
{
  pinMode(pin, OUTPUT);
  _pin = pin;
}

Morse::Debug(boolean debug)
{
  _debug = debug;
}

void Morse::dot()
{
  digitalWrite(_pin, HIGH);

  if (_debug)  {
    Serial.println("HIGH");
  }

  delay(250);
  digitalWrite(_pin, LOW);
  Serial.println("LOW");
  delay(250);  
}

void Morse::dash()
{
  digitalWrite(_pin, HIGH);
  delay(1000);
  digitalWrite(_pin, LOW);
  delay(250);
}

As is, right now, no matter what I set the ‘Morse Debug(true); // debug flag’ to, the data won’t print in the void Morse::dot() function.

Basically, I’m looking for a way to control when a library prints data to the serial.

Thanks and have a good day!
Randy

try:

#include <Morse.h>
Morse morse(A5);    // led pin is A5

void setup()
{
  while (!Serial);
  Serial.begin(115200);
  morse.Debug(true);  // debug flag
}

void loop()
{
  morse.dot(); morse.dot(); morse.dot();
  morse.dash(); morse.dash(); morse.dash();
  morse.dot(); morse.dot(); morse.dot();
  delay(3000);
}

If I had the problem, I would copy the entire "library" file into my Arduino code file and make it my own. Then you can change the code any way you desire.

Paul

or add it as an argument in the contructor

    Morse(int pin, boolean debug);
Morse morse(A5, true);

@ aarg - that didn't work, no change in behavior

@ Paul_KD7HB - that idea would work for a simple example like this, but the library I want to use this in is over 2000 lines, not something I want in the 1500 lines I have so far

gcjr:
or add it as an argument in the contructor

    Morse(int pin, boolean debug);
Morse morse(A5, true);

This works! I will have to look up the constructor part, I'm not sure I really understand that part.

If anyone has a good source to read up on the constructor part, please drop a link...

Thanks everyone!
Randy