If you’ve copied the files but made any syntactical errors, you’ll also be notified
now. If you have to correct some errors, make sure you change your original
source code files. After you’ve fixed the errors, copy the files to the
libraries
folder again, and don’t forget to restart the IDE.
Turning a static string into Morse code is nice, but wouldn’t it be great if our
program could work for arbitrary strings? So, let’s add a more sophisticated
example. This time, we’ll write code that reads messages from the serial port
and feeds them into a
Telegraph
instance.
Create a new sketch named
MorseCodeGenerator
and enter the following code:
TelegraphLibrary/examples/MorseCodeGenerator/MorseCodeGenerator.ino
#include "telegraph.h"
const unsigned int OUTPUT_PIN = 13;
const unsigned int DIT_LENGTH = 200;
const unsigned int MAX_MESSAGE_LEN = 128;
const unsigned int BAUD_RATE = 9600;
const char NEWLINE = '\n';
char message_text[MAX_MESSAGE_LEN];
int index = 0;
Telegraph telegraph(OUTPUT_PIN, DIT_LENGTH);
void setup() {
Serial.begin(BAUD_RATE);
}
void loop() {
if (Serial.available() > 0) {
int current_char = Serial.read();
if (current_char == NEWLINE || index == MAX_MESSAGE_LEN - 1) {
message_text[index] = 0;
index = 0;
telegraph.send_message(message_text);
} else {
message_text[index++] = current_char;
}
}
}
Again, we include the header file of the
Telegraph
class, and as usual we define
some constants:
OUTPUT_PIN
defines the pin our LED is connected to, and
DIT_LENGTH
contains the length of a dit measured in milliseconds.
NEWLINE
is
set to the ASCII code of the newline character. We need it to determine the
report erratum • discuss
Installing and Using the Telegraph Class • 69
www.it-ebooks.info