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: https://www.arduino.cc/en/Hacking/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