Friday, June 15, 2012

0 Arduino Project 3 - Morse Code Translator

This project uses the same apparatus as the projects 1 and 2, but has an upgraded sketch that allows us to write a sentence on the computer and the arduino translates that for us in long blinks and short blink. (Morse Code)

Hardware, Materials and Construction

Refer to the Project 1

Software

For this project to work, we first have to understand the concept of loops and arrays.

Loops:
As in project 2, when we had to flash the LED 3 times, we had no problem in repeating the command. But what if we want the LED to flash say 100 times? In that case we use loop function. An example is given below

for (int i = 0; i < 100; i ++)
{
flash(200);
}

The 'for' loop is a function that takes three arguments to perform the work given to it. As long as the conditions are satisfied, the function is valid. After that the function is void and Arduino follows the command after that. The first command 'int i = 0' sets the initial value of the variable to 0. The second command sets the condition that as long as the value of variable is less than 100, the function is valid. The third command says that after each blink, the value of variable increases by 1. So the LED will blink a 100 times. The code on curly braces tells the arduino what to do (in this case flash).
Note - To use this code by itself, you have to define the function 'flash' first.

Arrays:
Arrays are used to contain any number of values. Loops only contain one value usually 'int'. You can access the variables by its position in an array. Now let's see an array of durations.

int durations[] = {200, 200, 200, 500,
500, 500, 200, 200, 200}

The ' [] ' indicates that the function is an array. As in the previous array, if you set the variables, you need not specify any quantity in the square brackets. But if you have not specified the variables, you have to specify the number of variables used. As in 

int durations[10];

This is the modified loop method which uses an array.

void loop()
// run over and over again
{
for (int i = 0; i < 9; i++)
{
flash(durations[i]);
}
delay(1000);
// wait 1 second before we start
// again
}

Having said that, let's have a look at the actual code for the morse translator.

int ledPin = 12;
char* letters[] = {
".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", // A-I
".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", // J-R
"...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.." // S-Z
};
char* numbers[] = {"-----", ".----", "..---", "...--", "....-", ".....", "-....",
"--...", "---..", "----."};
int dotDelay = 200;
void setup()
{
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
char ch;
if (Serial.available()) // is there anything to be read from USB?

{
ch = Serial.read(); // read a single letter
if (ch >= 'a' && ch <= 'z')
{
flashSequence(letters[ch - 'a']);
}
else if (ch >= 'A' && ch <= 'Z')
{
flashSequence(letters[ch - 'A']);
}
else if (ch >= '0' && ch <= '9')
{
flashSequence(numbers[ch - '0']);
}
else if (ch == ' ')
{
delay(dotDelay * 4); // gap between words
}
}
}
void flashSequence(char* sequence)
{
int i = 0;
while (sequence[i] != NULL)
{
flashDotOrDash(sequence[i]);
i++;
}
delay(dotDelay * 3); // gap between letters
}
void flashDotOrDash(char dotOrDash)
{
digitalWrite(ledPin, HIGH);
if (dotOrDash == '.')
{
delay(dotDelay);
}
else // must be a -
{
delay(dotDelay * 3);
}
digitalWrite(ledPin, LOW);
delay(dotDelay); // gap between flashes


Paste the above sketch in the Arduino environment and load it to the arduino board. To use the Morse code translator, we need to use a part of the arduino environment called the serial monitor. To access it, click the rightmost icon on the top bar. A popup will open. The serial monitor is used to send commands to the arduino and see it's reply. It is here that you will write your sentences and see your arduino translate it into morse code. Enjoy !! Bewilder the nieghbours with your gadget! 
Au revoir jusqu'à ce que le prochain post!

0 comments:

Post a Comment

Do Comment on Anything you like or dont like!

 

The Engineer's Spot! Copyright © 2011 - |- Template created by O Pregador - |- Powered by Blogger Templates