First, the circuit. Its pretty simple, just an LED connected to pin 2 of the arduino, it has a 220 ohm resister to protect the led.
The fun part was the code. I wanted it to be able to display any message typed into the computer. So first I defined what a dash and a dot are. Thank you Wikipedia :) Then its some quick and dirty letter conversions and a loop to read in data from the serial port and we are off
Code:
/**
* Author: John Stemberger
* Date: December 18, 2011
* Description: A morse code blinker. This will blink a sentence in Morse code. The sentence will either be the defualt sentence (SOS) or a sentence read in from the serial connection if one exists
**/
char sentence[255] = "SOS\0";
int ledPin = 2;
int counter = 0;
int dotLength = 100;
void setup()
{
// set up the pin
pinMode(ledPin, OUTPUT);
// setup serial connection to the computer to read the sentence
Serial.begin(9600);
}
void dot()
{
Serial.print("DOT ");
digitalWrite(ledPin, HIGH);
delay(dotLength);
digitalWrite(ledPin, LOW);
delay(dotLength);
}
void dash()
{
Serial.print("DASH ");
digitalWrite(ledPin, HIGH);
delay(3*dotLength);
digitalWrite(ledPin, LOW);
delay(dotLength);
}
void displayLetter(char letter)
{
// 1. a dash is equal to 3 dots
// 2. the space between parts of the same letter is eual to one dot
// 3. the space between two letters is equal to three dots
// 4. the space between 2 words is equal to 7 dots
switch(letter)
{
case 'A':
dot();
dash();
break;
case 'B':
break;
case 'C':
dash();
dot();
dash();
dot();
break;
case 'D':
dash();
dot();
dot();
break;
case 'E':
dot();
break;
case 'F':
dot();
dot();
dash();
dot();
break;
case 'G':
dash();
dash();
dot();
break;
case 'H':
dot();
dot();
dot();
dot();
break;
case 'I':
dot();
dot();
break;
case 'J':
dot();
dash();
dash();
dash();
break;
case 'K':
dash();
dot();
dash();
break;
case 'L':
dot();
dash();
dot();
dot();
break;
case 'M':
dash();
dash();
break;
case 'N':
dash();
dot();
break;
case 'O':
dash();
dash();
dash();
break;
case 'P':
dot();
dash();
dash();
dot();
break;
case 'Q':
dash();
dash();
dot();
dash();
break;
case 'R':
dot();
dash();
dot();
break;
case 'S':
dot();
dot();
dot();
break;
case 'T':
dash();
break;
case 'U':
dot();
dot();
dash();
break;
case 'V':
dot();
dot();
dot();
dash();
break;
case 'W':
dot();
dash();
dash();
break;
case 'X':
dash();
dot();
dot();
dash();
break;
case 'Y':
dash();
dot();
dash();
dash();
break;
case 'Z':
dash();
dash();
dot();
dot();
break;
case ' ':
// spaces will have 3 dots before it, and 2 dots after (from the pervious letter and the end of this letter) so the space will need 2
extra dots to make a total of 7
delay(2*dotLength);
break;
default:
Serial.print("unknown letter [");
Serial.print(letter);
Serial.println("]");
}
delay(2*dotLength);// the space between letters is 3 dots but the end of the last part of the letter will have a 1 dot delay (to finish that letter) so
e only do 2 dots here.
}
void loop()
{
if(Serial.available())
{
counter = 0;
while(Serial.available())
{
// get the new sentence
sentence[counter] = Serial.read();
if(sentence[counter] >= 'a' && sentence[counter] <= 'z')
{
sentence[counter] = sentence[counter] - 'a' + 'A';
}
if((sentence[counter] < 'A' || sentence[counter] > 'Z') && sentence[counter] != ' ')
{
Serial.println('non valid letter, replacing with space');
sentence[counter] = ' ';
}
counter++;
if(counter > 254)
{
// we have hit the limit of our sentence so abport
Serial.println("Sentence is too long, ignoring the rest.");
break;
}
}// end while we have available data on the serial port
sentence[counter] = '\0';
Serial.flush();
}// end if
// if there is nothing available (we havent gotten anything) then display the currect message
// echo it out to the serial port
Serial.print("Displaying code for: ");
Serial.println(sentence);
counter = 0;
while(sentence[counter] != '\0')
{
Serial.print(sentence[counter]);
Serial.print("- ");
displayLetter(sentence[counter]);
counter++;
}
Serial.println("\nEnd message");
delay(500);
}
~(' ')~

No comments:
Post a Comment