0

I am attempting to create a library out of an existing code to use inside of another code. To clarify, I have one code, I need to add the functionality of another code to. Instead of plopping the 400+ lines inside of my existing code, I would like to create a library to house the other code so I can make my code easier to read. I followed the Arduino Library tutorial to get to where I am, but their code is simpler than mine. Ironically, my code is also about Morse code. I have included the library files and the code I am using to troubleshoot. I have attempted to troubleshoot these errors on my own by googling problems, I managed to cut the list about in half, but I just can't seem to root out these last few.

I am posting in this order: Morse.cpp, Morse.h, Library Test Code, The errors I need help with (all of which relate to the above three files) and the functioning code as it was before I tried to create this library.

The Following is the Morse.cpp Library File

/* Morse.cpp - Library for flashing Morse code.
Created by Young on Sept. 1, 2016 */

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

Morse::Morse(int LED);
{
pinMode(LED, OUTPUT);
_LED = LED;
}
Morse::Morse(int BEEP);
{
pinMode(BEEP, OUTPUT);
_BEEP = BEEP;
}

_note = 587;

_dotLen = 75;

_dashLen = dotLen * 7;

_elemPause = dotLen * 4;

_Spaces = dotLen * 10;

_wordPause = dotLen * 14;

void MorseDot();
void MorseDash();
void LightsOff(int delayTime);
void GetChar(char tmpChar);


void Morse::MorseDot()
{
digitalWrite(led13, HIGH);    // turn the LED on
tone(BEEP, note, dotLen);     // start playing a tone
delay(dotLen);                // hold in this position
}

void Morse::MorseDash()
{
digitalWrite(led13, HIGH);    // turn the LED on
tone(BEEP, note, dashLen);    // start playing a tone
delay(dashLen);               // hold in this position
}

void Morse::LightsOff(int delayTime)
{
digitalWrite(led13, LOW);     // turn the LED off
digitalWrite(BEEP, LOW);      // Silence the Beep
delay(delayTime);             // hold in this position
}

digitalWrite(led13, LOW);     // turn the LED off
digitalWrite(BEEP, LOW);      // Silence the Beep
delay(delayTime);             // hold in this position

void Morse::GetChar(char tmpChar)
{
// Take the passed character and use a switch to find the morse code for that character
switch (tmpChar) {
    case 'a':
        MorseDot();
        LightsOff(elemPause);
        MorseDash();
        LightsOff(elemPause);
        break;
    case 'b':
        MorseDash();
        LightsOff(elemPause);
        MorseDot();
        LightsOff(elemPause);
        MorseDot();
        LightsOff(elemPause);
        MorseDot();
        LightsOff(elemPause);
        break;
    case 'c':
        MorseDash();
        LightsOff(elemPause);
        MorseDot();
        LightsOff(elemPause);
        MorseDash();
        LightsOff(elemPause);
        MorseDot();
        LightsOff(elemPause);
        break;
/*This continues for the rest of the alphabet as well as numbers and characters, 
however to save space and time I have only listed a,b, and c */

The following is the Morse.h Library file

/* Morse.h - Library for flashing Morse code.
Created by Young on Sept. 1, 2016 */

#ifndef Morse_h
#define Morse_h

#include "Arduino.h"

class Morse
{
public:
Morse(int LED, int BEEP, int delayTime, char tmpChar);

void MorseDot();
void MorseDash();
void LightsOff(int delayTime);
void GetChar(char tmpChar);

private:
int _LED;
int _BEEP;
int _delayTime;
char _tmpChar;
int _note;
int _dotLen;
int _dashLen;
int _elemPause;
int _Spaces;
int _wordPause;
};

#endif

The following is the test code

#include <Morse.h>

int delayTime;
int tmpChar;

Morse dot();
Morse dash();
Morse dark(int delayTime); 
Morse set(char tmpChar);

char stringToMorseCode[] = "N 36° 00.19518   W 084° 13.88856";    //Final Coordinates

// Create variable to define the output pins
int LED = 13;      // blink an led on output 12
int BEEP = 14;      // output audio on pin 8
int i;

void setup() 
{

}

void loop() 
{ 
// Loop through the string and get each character one at a time until the end is reached
for (int i = 0; i < sizeof(stringToMorseCode) - 1; i++);
{
// Get the character in the current position
char tmpChar = stringToMorseCode[i];
// Set the case to lower case
tmpChar = toLowerCase(tmpChar);
// Call the subroutine to get the morse code equivalent for this character
set.GetChar(tmpChar);
}

// At the end of the string long pledause before looping and starting again

dark.LightsOff(8000);            // hold in this position     

}

These are the errors I get when I run this code.

/Users/DMYoung1/Documents/Arduino/Morse_Library_Test/Morse_Library_Test.ino: In function 'void loop()':
Morse_Library_Test:33: error: request for member 'GetChar' in 'set', which is of non-class type 'Morse(char)'
   set.GetChar(tmpChar);
       ^
Morse_Library_Test:38: error: request for member 'LightsOff' in 'dark', which is of non-class type 'Morse(int)'
 dark.LightsOff(8000);            // hold in this position     
      ^
exit status 1
request for member 'GetChar' in 'set', which is of non-class type 'Morse(char)'

In case this will also help, this is the original code which I know works. Hopefully this could help someone else understand the transformation of a slightly more complicated morse reader code into a library, so please feel free to respond as though I know nothing. It will help me and could help someone else.

/*
Morse Code Project
This code will loop through a string of characters and convert these to morse code.  
It will blink an LED light and play an audible tone on a piezo speaker.  
*/ 


char stringToMorseCode[] = "Convert to Morse"; //String to Convert

// Create variable to define the output pins
int led13 = 1;      // blink an led on output 12
//int led6 = 0;        // blink an led on output 6
int BEEP = 0;      // output audio on pin 8
int note = 587;      // music note/pitch

/*
Set the speed of your morse code
Adjust the 'dotlen' length to speed up or slow down your morse code
(all of the other lengths are based on the dotlen)

Here are the ratios code elements:
Dash length = Dot length x 3
Pause between elements = Dot length
(pause between dots and dashes within the character)
Pause between characters = Dot length x 3
Pause between words = Dot length x 7

*/
int dotLen = 75;     // length of the morse code 'dot'
int dashLen = dotLen * 7;    // length of the morse code 'dash'
int elemPause = dotLen * 4;  // length of the pause between elements of a character
int Spaces = dotLen * 10;     // length of the spaces between characters
int wordPause = dotLen * 14;  // length of the pause between words

// the setup routine runs once when you press reset:
void setup() {                
// initialize the digital pin as an output for LED lights.
pinMode(led13, OUTPUT); 
pinMode(BEEP, OUTPUT);

}

// Create a loop of the letters/words you want to output in morse code (defined in string at top of code)
void loop()
{ 
// Loop through the string and get each character one at a time until the end is reached
for (int i = 0; i < sizeof(stringToMorseCode) - 1; i++)
{
// Get the character in the current position
char tmpChar = stringToMorseCode[i];
// Set the case to lower case
tmpChar = toLowerCase(tmpChar);
// Call the subroutine to get the morse code equivalent for this character
GetChar(tmpChar);
}

// At the end of the string long pause before looping and starting again
LightsOff(8000);      
}

// DOT
void MorseDot()
{
digitalWrite(led13, HIGH);    // turn the LED on
tone(BEEP, note, dotLen);   // start playing a tone
delay(dotLen);              // hold in this position
}

// DASH
void MorseDash()
{
digitalWrite(led13, HIGH);    // turn the LED on 
tone(BEEP, note, dashLen);   // start playing a tone
delay(dashLen);               // hold in this position
}

// Turn Off
void LightsOff(int delayTime)
{
digitalWrite(led13, LOW);     // turn the LED off
digitalWrite(BEEP, LOW);      // Silence the Beep
delay(delayTime);             // hold in this position

}

// *** Characters to Morse Code Conversion *** //
void GetChar(char tmpChar)
{
// Take the passed character and use a switch case to find the morse code for that character
switch (tmpChar) {
case 'a': 
MorseDot();
LightsOff(elemPause);
MorseDash();
LightsOff(elemPause);
break;
case 'b':
MorseDash();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
break;

//Again, this switch case would continue through the alphabet, but I am stopping 
//here because that would dramatically increase the length of this page
0

The problem is that you are trying to call a class function without an object (hence the error). The original code works because the functions are created without a class. When you add the class (in the library), you need to create an object to perform those functions with it. To do this, use the constructor className object(parameters)

So in your case, you'd do Morse Object(LEDpin)

To use your functions you can now do Object.Function(parameters)

  • I read about this, I just couldn't figure out how to fix it!! I understand everything you have told me except for the parameters. What / how should I set those? – user26383 Sep 7 '16 at 2:33
  • Let me rephrase, I understand conceptually everything you have told me and I really appreciate your response. I'm afraid I just can't seem to make the connection from concept to my code. I have been reading about objects and function parameters, I just can't seem to understand how to actually implement them. For example, should I put this code in the library file? Or does it go in my program code? Either way, how should I proceed. When I said treat me like I know nothing, I really don't know anything about this, but I can't learn if I don't try. – user26383 Sep 7 '16 at 2:41
  • There is this thing called a 'class constructor'. You can look it up for a more detailed explanation, but basically you have a function without a type inside your class, named the same thing as your class. You have a couple of them, Morse() - I would combine them into one Morse() with a bunch of parameters. Basically, when you create the object Morse() will automatically run, using the parameters you put in where I said. I would look it up - I'm not sure I explained it that well! – Mension1234 Sep 7 '16 at 2:45
  • Great, I will look it up and if I get stuck I will post back here, probably in a few hours. Thanks for the help so far!! – user26383 Sep 7 '16 at 2:47
  • I have fixed it would seem all the errors I had before, but I have two more they are both the same, but relate to different members in objects. These seem to be common errors as I have been able to read a great deal about other people's troubles with them, but I seem to be unable to diagnose their problems or mine. The errors are as follows: – user26383 Sep 7 '16 at 15:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.