The output device is connected to pins D13 and GND, and a potentiometer is connected as a variable voltage divider between 5 VDC and GND, with the middle pin connected to A3. The potentiometer controls the speed of the code, and the output device receives TTL output for the morse code. Code follows:
/*
MORSER
cc 04/05/2009 Pete Marchetto
This program translates serial text input into morse code output. Morse code is comprised of five kinds of character elements: the short, or "dit", which is 1 u long, the long, or "dah", which is 3 u long, the intra-character pause, which is also 1 u long, the inter-character pause, which is also 3 u long, and the inter-word pause, which is 7 u long.
*/
int outputPin = 13; //pin for the LED
int inPin=3; //pin for the potentiometer
int val; //value passed from serial interface
int u=50; //unit value, for time
void setup()
{
Serial.begin(9600);
pinMode(outputPin, OUTPUT);
}
void loop()
{
u=1+analogRead(inPin);
if (Serial.available()) {
val = Serial.read();
delay(3*u);
if (val == ' ') {
delay(5*u);
}
if (val == 'A') {
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
}
if (val == 'B') {
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
}
if (val == 'C') {
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
}
if (val == 'D') {
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
}
if (val == 'E') {
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
}
if (val == 'F') {
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
}
if (val == 'G') {
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
}
if (val == 'H') {
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
}
if (val == 'I') {
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
}
if (val == 'J') {
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
}
if (val == 'K') {
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
}
if (val == 'L') {
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
}
if (val == 'M') {
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
}
if (val == 'N') {
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
}
if (val == 'O') {
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
}
if (val == 'P') {
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
}
if (val == 'Q') {
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
}
if (val == 'R') {
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
}
if (val == 'S') {
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
}
if (val == 'T') {
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
}
if (val == 'U') {
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
}
if (val == 'V') {
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
}
if (val == 'W') {
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
}
if (val == 'X') {
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
}
if (val == 'Y') {
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
}
if (val == 'Z') {
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
}
if (val == '0') {
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
}
if (val == '1') {
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
}
if (val == '2') {
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
}
if (val == '3') {
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
}
if (val == '4') {
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
}
if (val == '5') {
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
}
if (val == '6') {
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
}
if (val == '7') {
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
}
if (val == '8') {
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
}
if (val == '9') {
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
}
if (val == '.') {
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
}
if (val == ',') {
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
}
if (val == '?') {
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
}
if (val == '!') {
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(3*u);
digitalWrite(outputPin, LOW);
delay(u);
digitalWrite(outputPin, HIGH);
delay(u);
digitalWrite(outputPin, LOW);
}
}
}
1 comment:
I like this. But you could make it simpler to read and understand if you made a generic send function.
private void send(string str)
{
for x = 0 to lenght(str)
if (int)substirng(str(x,0)== 1
Send dash
else send dot
next x
}
Then at the top you have you serial input go to a case statment that calls the function as such..
case "A"
send("101101")
The string uses 1 and 0's to correspond to the dots and dashes.
Post a Comment