//Morse Code Binary Tree // Left child at 2n+1 // Right child at 2n+2 // <.|-> // .E -T // .I -A .N -M //RX const char MorseTree[] = { '\0', 'E', 'T', 'I', 'A', 'N', 'M', 'S', 'U', 'R', 'W', 'D', 'K', 'G', 'O', 'H', 'V', 'F', 'U', 'L', 'A', 'P', 'J', 'B', 'X', 'C', 'Y', 'Z', 'Q', '\0', '\0', '5', '4', '\0', '3', '\0', '\0', '\0', '2', '\0', '\0', '+', '\0', '\0', '\0', '\0', '1', '6', '=', '/', '\0', '\0', '\0', '(', '\0', '7', '\0', '\0', '\0', '8', '\0', '9', '0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '?', '_', '\0', '\0', '\0', '\0', '"', '\0', '\0', '.', '\0', '\0', '\0', '\0', '@', '\0', '\0', '\0', '\0', '\0', '\0', '-', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', ';', '!', '\0', ')', '\0', '\0', '\0', '\0', '\0', ',', '\0', '\0', '\0', '\0', ':', '\0', '\0', '\0', '\0', '\0', '\0', '\0' }; int val = 0; // A Variable to Store the Light Value from the LDR int ctrHigh = 0; int ctrLow = 0; int codePtr = 0; int dotLen = 400; /**********************************************/ //TX 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 = 50; //time in mS int dashLength = dotLength * 2; void setup() { // put your setup code here, to run once: pinMode(13, OUTPUT); pinMode(12, OUTPUT); pinMode(A1, OUTPUT); digitalWrite(13, HIGH); digitalWrite(A1, LOW); Serial.begin(9600); Serial.print(codePtr); } void loop() { char ch; if (Serial.available()) { ch = Serial.read(); flashDashDot(MorseTable[ch]); delay(dotLength * 2); } RX(); } void flashDashDot(const char * morseCode)//TX { int i = 0; while (morseCode[i] != 0) { if (morseCode[i] == '.') { dot(); } else if (morseCode[i] == '-') { dash(); } i++; } } void dot() { digitalWrite(12, HIGH); delay(dotLength); digitalWrite(12, LOW); delay(dotLength); } void dash() { digitalWrite(12, HIGH); delay(dashLength); digitalWrite(12, LOW); delay(dotLength); } void space() { digitalWrite(12, HIGH); delay(150); digitalWrite(12, LOW); delay(150); } void RX() { val = analogRead(0); if (val >= 10) { ctrHigh++; ctrLow = 0; } else { ctrLow++; if ((ctrHigh >= dotLen) && (ctrHigh < dotLen * 2)) { codePtr = (2 * codePtr) + 1; } else if (ctrHigh >= dotLen * 2) { codePtr = (2 * codePtr) + 2; } else { if (ctrLow == dotLen * 2) { Serial.print(MorseTree[codePtr]); codePtr = 0; } } ctrHigh = 0; } }