Hardware components | ||||||
![]() |
| × | 1 | |||
![]() |
| × | 2 | |||
![]() |
| × | 2 | |||
| × | 2 | ||||
![]() |
| × | 1 | |||
![]() |
| × | 1 | |||
![]() |
| × | 1 | |||
Software apps and online services | ||||||
![]() |
|
This is a simple telegraph, I made recently.. It has the ability to Encode Words to Morse Code.. And Decode Morse Code to Words..
You can input Words through the Serial Monitor, it will be changed to Morse Code and Outputted via the LED and Buzzer..
You can also use the Switch to input Morse Code.. It will be Outputted in Words and Displayed in the Serial Monitor..
Connections:LED 1 :
(+) to Digital Pin 2
(-) to GND (with 1k resisor)
LED 2
(+) to Digital Pin 3
(-) to GND (with 1k resisor)
Buzzer 1
(+) to Digital Pin 4
(-) to GND
Buzzer 2
(+) to Digital Pin 5
(-) to GND
Input switch
(Wire 1) to Digital Pin 6
(Wire 2) to GND
If you want to make it slower or faster, you can refer the chart below and change the timings.. It is in Milli Seconds.. Currently the speed is (about) 15 Words Per Minutes(WPM)..
You can see it in action here :
Hope it helps.. Cheers!
// Needs Fifo.h (Queue functions) copied into the sketch folder
#include "Fifo.h"
Fifo<char> symbolQueue( 160 ) ;
const uint8_t led = 2;
const uint8_t ledPin = 3;
const uint8_t buz = 4;
const uint8_t buzPin = 5;
const int inputPin = 6;
String code = "";
// To increase speed, change '50' to a smaller number..
// To decrease speed, change '50' to a larger number..
const unsigned long MsecDot = 50;
const unsigned long MsecDash = MsecDot * 3;
const unsigned long MsecSym = MsecDot;
const unsigned long MsecLtr = MsecDot * 3;
const unsigned long MsecWord = MsecDot * 7;
String letters[] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-",
".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..",
"-----" , ".----" , "..---" , "...--" , "....-" , "....." , "-...." , "--..." , "---.." , "----." , "E" };// Morse letters (26) followed by numbers(10)
void convertor(){
int i = 0;
if (code == ".-.-.-")
{
Serial.print(".");
}
else
{
while (letters[i] != "E")
{
if (letters[i] == code)
{
if ( i >= 0 && i <= 25 ) Serial.print(char('A' + i));
else if ( i >= 26 && i <= 35 ) Serial.print(char('0' + i - 26 ));
break;
}
i++;
}
if (letters[i] == "E")
{
Serial.println("");
}
}
code = "";
}
bool getKey() {
// get debounced key press
static bool currentKeyState = HIGH ;
static bool newKeyState = HIGH ;
static uint32_t newKeyStateAtMs = millis() ;
bool dr = digitalRead(inputPin) ;
if ( dr != newKeyState ) {
newKeyState = dr ;
newKeyStateAtMs = millis() ;
}
if ( currentKeyState != newKeyState && millis() - newKeyStateAtMs > 25 ) {
// debounce: accept only a mature change ( > X ms )
currentKeyState = newKeyState ;
}
return currentKeyState ;
}
void decoder() {
static bool lastKey = HIGH ;
static uint32_t lastTranstionAtMs = millis() ;
static bool inLetter = false ;
static bool inWord = false ;
uint32_t ms = millis() ;
digitalWrite( ledPin, !getKey() ) ;
digitalWrite( buzPin, !getKey() ) ;
if ( getKey() != lastKey ) {
// transition
if ( lastKey == LOW ) {
// we were in a symbol - determine if it was a '.' or a '-'
code += ( ms - lastTranstionAtMs < MsecDot * 2 ? '.' : '-' ) ;
} else {
// we were in a space
inLetter = true ;
inWord = true ;
}
lastTranstionAtMs = ms ;
lastKey = getKey() ;
}
if ( getKey() == HIGH ) {
// we are in a space
if ( inLetter && ms - lastTranstionAtMs > MsecLtr ) {
// flush out letter
convertor() ;
inLetter = false ;
}
if ( inWord && ms - lastTranstionAtMs > MsecWord ) {
// flush out word
Serial.print(" " ) ;
inWord = false ;
}
}
}
void charToMorse( char mChar ) {
// converts input character to a set of morse symbols which are pushed onto a
// queue together with an end of character marker '|'
uint8_t offset ; // offset into table letters[] based on ascii code of char
bool inTable = false ;
if ( mChar >= 'a' && mChar <= 'z' ) {
offset = mChar - 97 ;
inTable = true ;
}
else if ( mChar >= 'A' && mChar <= 'Z' ) {
offset = mChar - 64 ;
inTable = true ;
}
else if ( mChar >= '0' && mChar <= '9' ) {
offset = (mChar - 48) + 26 ;
inTable = true ;
}
else {}
if ( inTable ) {
// Serial.print( letters[ offset ] ) ;
for ( uint8_t i = 0 ; i < letters[ offset ].length() ; i++ ) symbolQueue.push( letters[ offset ].charAt( i ) ) ;
symbolQueue.push( '|' ) ;
}
else if ( mChar == ' ' ) {
// Serial.print( '|' ) ;
symbolQueue.push( '|' ) ;
}
}
void setup() {
Serial.begin(9600);
pinMode(led, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(buz, OUTPUT);
pinMode(buzPin, OUTPUT);
pinMode(inputPin, INPUT_PULLUP);
Serial.println("Start");
symbolQueue.flush() ;
}
void loop() {
enum state_t { inDot, inDash, inSpace, inWaiting } ;
static state_t state = inWaiting ;
static uint32_t inStateAtMs = millis() ;
// get input from user, analyse, convert to morse symbols and queue
if (Serial.available()) {
char inChar = Serial.read() ;
charToMorse( inChar ) ;
}
// finite state machine - unload queue and sound buzzer as required
switch ( state ) {
case inWaiting : {
if ( ! symbolQueue.isempty() ) {
char inCh = symbolQueue.pop() ;
// debug
Serial.print( inCh ) ;
inStateAtMs = millis() ;
if ( inCh == '.' ) {
digitalWrite(led, HIGH);
digitalWrite(buz, HIGH);
state = inDot ;
}
else if ( inCh == '-' ) {
digitalWrite(led, HIGH);
digitalWrite(buz, HIGH);
state = inDash ;
}
else if ( inCh == '|' ) {
state = inSpace ;
}
}
break ;
}
case inDot : {
if ( millis() - inStateAtMs > MsecDot ) {
inStateAtMs = millis() ;
digitalWrite(led, LOW);
digitalWrite(buz, LOW);
state = inSpace ;
}
break ;
}
case inDash : {
if ( millis() - inStateAtMs > MsecDash ) {
inStateAtMs = millis() ;
digitalWrite(led, LOW);
digitalWrite(buz, LOW);
state = inSpace ;
}
break ;
}
case inSpace : {
if ( millis() - inStateAtMs > MsecLtr ) {
inStateAtMs = millis() ;
digitalWrite(led, LOW);
digitalWrite(buz, LOW);
state = inWaiting ;
}
break ;
}
}
decoder() ;
}
Comments
Please log in or sign up to comment.