You are on page 1of 2

#include "Morse.

h"
#include "Arduino.h"
char* LETTERS[] = {
".-", "-...", "-.-.", "-..", ".",
"..-.", "--.", "....", "..", ".---",
"-.-", ".-..", "--", "-.", "---",
".--.", "--.-", ".-.", "..." ,"-",
"..-", "...-", ".--", "-..-", "-.--",
"--.."
// Z
};

//
//
//
//
//

A-E
F-J
K-O
P-T
U-Y

char* DIGITS[] = {
"-----",".----","..---","...--", // 0-3
"....-",".....","-....","--...", // 4-7
"---..","----."
// 8-9
};
Morse::Morse(const int output_pin, const int dot_length) {
_output_pin = output_pin;
_dot_length = dot_length;
_dash_length = dot_length*3;
pinMode(_output_pin, OUTPUT);
}
void Morse::output_code(const char* code) {
for (int i = 0; i < strlen(code); i++) {
if (code[i] == '.')
dot();
else
dash();
}
}
void Morse::dot() {
Serial.print(".");
output_symbol(_dot_length);
}
void Morse::dash() {
Serial.print("-");
output_symbol(_dash_length);
}
void Morse::output_symbol(const int length) {
digitalWrite(_output_pin, HIGH);
delay(length);
digitalWrite(_output_pin, LOW);
}
void Morse::send_message(const char*message) {
for (int i = 0; i < strlen(message); i++) {
const char current_char = toupper(message[i]);
if (isalpha(current_char)) {
output_code(LETTERS[current_char - 'A']);
delay(_dash_length);
} else if (isdigit(current_char)) {
output_code(DIGITS[current_char - '0']);
delay(_dash_length);

} else if (current_char == ' ') {


Serial.print(" ");
delay(_dot_length * 7);
}
}
Serial.println();
}

You might also like