Having already got the "made a keyer" T-shirt for PIC and Raspberry Pi, the implementation of a Keyer seems to be turning into my "Hello World" example for microcontrollers, so off we go again with the "KEYERduino"...
Here's the program (what the illuminati call a "sketch" in Arduino-speak):
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Keyer | |
Simple keyer for the Arduino | |
m0xpd | |
shack.nasties 'at Gee Male dot com' | |
Pins 6 & 7 connected to a paddle... | |
pin 6 is the 'dit' key (usually the left) | |
pin 7 is the 'dah' key (usually the right) | |
Pin 8 connected to a straight key | |
All the keys have one side to ground, the other pulled up to 5V by 10k resistors | |
(advanced users can exploit the internal pull-up resistors instead) | |
Pin 8 is for sidetone | |
Pin 13 is the Tx output to key the transmitter - using an open-collector NPN transistor or a relay | |
*/ | |
// set pin numbers: | |
const int ditPin = 6; | |
const int dahPin = 7; | |
const int str8Pin = 8; | |
const int sidetonePin = 9; | |
const int TxPin = 13; | |
// variables : | |
int ditState = 0; | |
int dahState = 0; | |
int str8State = 0; | |
unsigned sidetoneFreq=600; | |
int ditLength = 75; // dit length in milliseconds | |
int dahLength = 3 * ditLength; | |
int LastElement = 0; // code for the last element sent... | |
// 0 :- space | |
// 1 :- dit | |
// 2 :- dah | |
void setup() { | |
// initialize the Tx and sidetone pins as an output: | |
pinMode(TxPin, OUTPUT); | |
pinMode(sidetonePin,OUTPUT); | |
// initialize the Key pins as an inputs: | |
pinMode(ditPin, INPUT); | |
pinMode(dahPin, INPUT); | |
pinMode(str8Pin, INPUT); | |
} | |
void loop(){ | |
// read the state of the inputs: | |
ditState = digitalRead(ditPin); | |
dahState = digitalRead(dahPin); | |
str8State = digitalRead(str8Pin); | |
if (str8State == LOW) { | |
digitalWrite(TxPin,HIGH); | |
tone(sidetonePin,sidetoneFreq); | |
} | |
else { | |
digitalWrite(TxPin,LOW); | |
noTone(sidetonePin); | |
if ((ditState == LOW)&(dahState == HIGH)) { | |
LastElement = 1; | |
CWKey(ditLength); | |
CWSpace(ditLength); | |
} | |
else if ((dahState == LOW)&(ditState == HIGH)) { | |
LastElement = 2; | |
CWKey(dahLength); | |
CWSpace(ditLength); | |
} | |
else if ((dahState == LOW)&(ditState == LOW)) { | |
if (LastElement == 2) { | |
LastElement = 1; | |
CWKey(ditLength); | |
CWSpace(ditLength); | |
} | |
else { | |
LastElement = 2; | |
CWKey(dahLength); | |
CWSpace(ditLength); | |
} | |
} | |
else { | |
LastElement = 0; | |
} | |
} | |
} | |
void CWKey(int duration){ | |
digitalWrite(TxPin,HIGH); | |
tone(sidetonePin,sidetoneFreq); | |
delay(duration); | |
digitalWrite(TxPin,LOW); | |
noTone(sidetonePin); | |
} | |
void CWSpace(int duration){ | |
digitalWrite(TxPin,LOW); | |
delay(duration); | |
} | |
In the present case, not only does the LED associated with pin 13 light up - we're also thinking of the same digital output driving the base of an open-collector transistor to key the transmitter. Also, only the straight key input works precisely like the button tutorial, simply "lighting the LED" when pressed. Closing any of the other two "buttons" triggers generation of the "dits" and "dahs" of Morse code, making an "iambic keyer". Oh yes - I nearly forgot - I also included a nasty, square-wave sidetone generator.
Here's the circuit, expressed in the unique format of Fritzing, much beloved of the Arduino community...
Fritzing isn't really my cup of tea - its OK for generating the sort of easily-accessible images above but it seems very much harder to make proper schematics with Fritzing than with a tool like Eagle.
Of course, nobody in their right mind would permanently implement a Keyer on an Arduino - it simply isn't worth burning the value (~£14) of the Arduino in something as trivial as a keyer. But there are some cases where it might just be excusable:
- As a learning exercise - beyond the button tutorial
- When part of a more complex application - such as a MEPT beacon
- When you are just using the bare chip in a home-brewed board - cheap as chips!
...-.- de m0xpd
No comments:
Post a Comment