Skip to content

Instantly share code, notes, and snippets.

Created January 5, 2013 08:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4460482 to your computer and use it in GitHub Desktop.
Save anonymous/4460482 to your computer and use it in GitHub Desktop.
This is a simple arduino sketch to copy morse code on pin 7 and send ascii text to the serial port. It also gives feedback on an led and a tone on pin 5.
/*
# Copyright (c) 2013 Matthew Denson
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
This is a simple arduino sketch to copy morse code on pin 7 and
send ascii text to the serial port.
It also gives feedback on an led and a tone on pin 5.
Here is background on timing morse code for decoding.
* The DIT is the smallest time unit.
* The DAH is the length three DITs (3*DIT).
* Innerletter space (between elements) ia one DIT long. (1*DIT)
* Interletter space (between letters) is one DAH long. (3*DIT)
* Interword space (between words) is seven DITs long. (7*DIT)
The Standard definition of words per minute (WPM) in morse is the number
of time the word "PARIS" can be sent in a minute, which is the lenght of
50 DITs.
So WPM = 1200/DIT or DIT = 1200/WPM
A debounce of 20ms means the shortest DIT duration is 20ms, allowing for
a maximum of 60 WPM. This is more than adequate for most operators ;-).
*/
#include <avr/pgmspace.h>
// key states
#define UP HIGH
#define DOWN LOW
// lamp states
#define OFF HIGH
#define ON LOW
// pin difinitions
#define LED 6
#define BUZZER 5
#define KEY 7
int DIT = 80;
int letter = 1;
boolean keyDown = false;
boolean wordEnded = true;
unsigned long time = 0;
prog_uchar chars[] PROGMEM = {
'?','?','e','t','i','a','n','m', 's','u','r','w','d','k','g','o',
'h','v','f','?','l','?','p','j', 'b','x','c','y','z','q','!','?',
'5','4','?','3','?','?','?','2', '&','?','+','?','?','?','?','1',
'6','=','/','?','?','?','(','?', '7','?','?','?','8','?','9','0',
'?','?','?','?','?','?','?','?', '?','?','?','?','?','_','?','?',
'?','?','"','?','?','.','?','?', '?','?','@','?','?','?','\'','?',
'?','-','?','?','?','?','?','?', '?','?',';','!','?',')','?','?',
'?','?','?',',','?','?','?','?', ':','?','?','?','?','?','?','?'
};
void setup() {
pinMode(LED, OUTPUT);
digitalWrite(LED, HIGH);
pinMode(BUZZER, OUTPUT);
pinMode(KEY, INPUT);
digitalWrite(KEY, HIGH); // Turn on pull-up resistor
Serial.begin(9600);
delay(1000);
keyDown = false;
time = millis();
}
void loop()
{
// How long was key up?
unsigned long duration = millis() - time;
// debounce the key
if (duration < 20) return;
if (digitalRead(KEY) == UP) {
if (keyDown) {
// -----------------------
// key up feedback
digitalWrite(LED, OFF);
noTone(BUZZER);
keyDown = false;
time = millis();
wordEnded = false;
// discriminate the morse element
if (duration<(2*DIT)) {
letter = letter*2;
} else {
letter = (letter*2) + 1;
}
} else { // key still up
// discriminate the morse spacing
if (!wordEnded && duration>(10*DIT)) {
Serial.print(" ");
wordEnded = true;
} else if (letter!=1 && duration>(2*DIT)) {
Serial.print((char)pgm_read_byte_near(chars + (letter&0x7F)));
letter = 1;
}
}
} else {
if (!keyDown) {
// -----------------------
// key down feedback
digitalWrite(LED, ON);
tone(BUZZER, 600);
keyDown = true;
time = millis();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment