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