const char* MorseTable[] = {
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
// space, !, “, #, $, %, &, ‘
NULL, “-.-.–“, “.-..-.”, NULL, NULL, NULL, NULL, “.—-.”,
// ( ) * + , – . /
“-.–.”, “-.–.-“, NULL, “.-.-.”, “–..–“, “-….-“, “.-.-.-“, “-..-.”,
// 0 1 2 3 4 5 6 7
“—–“, “.—-“, “..—“, “…–“, “….-“, “…..”, “-….”, “–…”,
// 8 9 : ; < = > ?
“—..”, “—-.”, “—…”, “-.-.-.”, NULL, “-…-“, NULL, “..–..”,
// @ A B C D E F G
“.–.-.”, “.-“, “-…”, “-.-.”, “-..”, “.”, “..-.”, “–.”,
// H I J K L M N O
“….”, “..”, “.—“, “-.-“, “.-..”, “–“, “-.”, “—“,
// P Q R S T U V W
“.–.”, “–.-“, “.-.”, “…”, “-“, “..-“, “…-“, “.–“,
// X Y Z [ \ ] ^ _
“-..-“, “-.–“, “–..”, NULL, NULL, NULL, NULL, “..–.-“,
// ‘ a b c d e f g
NULL, “.-“, “-…”, “-.-.”, “-..”, “.”, “..-.”, “–.”,
// h i j k l m n o
“….”, “..”, “.—“, “-.-“, “.-..”, “–“, “-.”, “—“,
// p q r s t u v w
“.–.”, “–.-“, “.-.”, “…”, “-“, “..-“, “…-“, “.–“,
// x y z { | } ~ DEL
“-..-“, “-.–“, “–..”, NULL, NULL, NULL, NULL, NULL,
};
int dotLength = 500;
int dashLength = dotLength*3;
void setup() {
// put your setup code here, to run once:
pinMode(13, OUTPUT);
Serial.begin(9600);
}
void loop() {
char ch;
if(Serial.available()){
ch = Serial.read();
flashDashDot(MorseTable[ch]);
delay(dotLength*2);
}
}
void flashDashDot(const char * morseCode)
{
int i = 0;
while(morseCode[i] != 0)
{
if(morseCode[i] == ‘.’){
dot();
} else if (morseCode[i] == ‘-‘){
dash();
}
i++;
}
}
void dot()
{
digitalWrite(13, HIGH);
delay(dotLength);
digitalWrite(13, LOW);
delay(dotLength);
}
void dash()
{
digitalWrite(13, HIGH);
delay(dashLength);
digitalWrite(13, LOW);
delay(dotLength);
}