SoftwareSerial in a library (again)

I am trying to use SoftwareSerial in a library, passing the pins to use in the constructor, but failing to make it work. I have read other threads on this but I don't understand what works.

I have simplified this using the example of a morse code example library that I found. This library code, with the SoftwareSerial bit added, is attached. The error message is that MySerial is not defined in the Morse::dot function.

Can someone please explain, in short words (I do not normally use C++), what I need to do to be able to use SoftwareSerial in any function in the library .cpp file.

Morse.ino (228 Bytes)

Morse.cpp (576 Bytes)

Morse.h (321 Bytes)

Your code is small enough to post in-line. Please do so, with CODE TAGS.

I am trying to use SoftwareSerial in a library, passing the pins to use in the constructor, but failing to make it work. I have read other threads on this but I don't understand what works.

I have simplified this using the example of a morse code example library that I found. This library code, with the SoftwareSerial bit added, is as requested included below. The error message is that MySerial is not defined in the Morse::dot function.

Can someone please explain, in short words (I do not normally use C++), what I need to do to be able to use SoftwareSerial in any function in the library .cpp file.

Morse.ino
-----------

#include <Morse.h>

Morse morse(13);

void setup()
{
}

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


Morse.h
---------

/*
  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);
    void dot();
    void dash();
  private:
    int _pin;
};

#endif

Morse.cpp
-----------

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

#include "Arduino.h"
#include <SoftwareSerial.h>
#include "Morse.h"

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

void Morse::dot()
{
  MySerial.begin(9600);
  digitalWrite(_pin, HIGH);
  delay(250);
  digitalWrite(_pin, LOW);
  delay(250);  
}

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

Many things wrong here.

First, I wouldn’t give the main .ino file the same name (Morse) as your library. It probably works, but it can be, at the very least, confusing.

Second the SoftwareSerial instance is local to the Morse class constructor. It ceases to exist after the function exits.

Third, you’re calling the begin method for the SoftwareSerial object it the dot() method of your Morse class and you’re calling pinMode() in the constructor of your class. Both of these are wrong. They should be called in a begin() method for your class.

But, the larger structural problem of what you’re doing is that you’re restricting the class to use only SoftwareSerial. Rather, you should generalize it to use an object of any class that inherits from the Stream class. You can pass a pointer (or reference) that object into the Morse constructor. Because different Stream objects need different initializations, do all that (including calling begin() on the object if required) in the setup() function of your main program.