Monday, June 11, 2012

0 Arduino Project 2 - Morse S.O.S. Code Flasher


So to start this project, you should have the Arduino interfaced with your operating system. If not, you can read the tutorial here. We will use the Arduino project 1 as a base for this project. So if you didn't already do that project, it is absolutely necessary that you do it now. Just for the knowledge S.O.S in Morse code is            '...---...' So Morse code flashing will consist of three short blinks, followed by three long blinks, followed by three short ones again.

Equipment Required

Same as project 1
  • Arduino Board
  • 5-mm Red LED
  • 270 ohm metal film resistor
The arrangement remains the same too.

Software

Here is the link to the Project 1's code file that you can upload to the board and have your base ready. After this we're gonna make some modifications to the code so that your arduino does not only blink but blink S.O.S. The first step is to modify only the loop function so that it appears as shown here:

void loop()
{
digitalWrite(ledPin, HIGH);      // first dot start
delay(200);
digitalWrite(ledPin, LOW);
delay(200);
digitalWrite(ledPin, HIGH);     // second dot start
delay(200);
digitalWrite(ledPin, LOW);
delay(200);
digitalWrite(ledPin, HIGH);     // third dot start
delay(200);
digitalWrite(ledPin, LOW);
delay(500);
digitalWrite(ledPin, HIGH);     // first dash start
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
digitalWrite(ledPin, HIGH);     // second dash start
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
digitalWrite(ledPin, HIGH);     // third dash start
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
digitalWrite(ledPin, HIGH);     // first dot
delay(200);
digitalWrite(ledPin, LOW);
delay(200);
digitalWrite(ledPin, HIGH);     // second dot
delay(200);
digitalWrite(ledPin, LOW);
delay(200);
digitalWrite(ledPin, HIGH);
// third dot
delay(200);
digitalWrite(ledPin, LOW);
delay(1000);                            // wait 1 second before we start again
}

A copy paste will be highly recommended here! This would work just fine but isn't it too long? We will now try to shorten the code a little bit. We can do that by replacing four lines of code with only a one line function, the function of which we will describe in another line of code.(Don't worry you'll get it when I explain the code). After the above given loop's final curly bracket, add the following function(this was the function that I was talking about):

void flash(int duration)
{
digitalWrite(ledPin, HIGH);
delay(duration);
digitalWrite(ledPin, LOW);
delay(duration);
}

Then replace the loop function with this:

void loop()
{
flash(200); flash(200); flash(200);             
delay(300);                                                 // otherwise the flashes run together
flash(500); flash(500); flash(500);            
flash(200); flash(200); flash(200);           
delay(1000);                                             // wait 1 second before we start again
}
I strongly recommend that you do all of these steps carefully and fully before you read on.
Now I will explain the logic behind this. In the first sketch of this post, we instructed the arduino brain ( how do I explain this?) like we would explain to a 5 year old, explicitly detailing all the work that has to be done. 

But that would be a very tedious task wouldn't it? So we made the board old enough to remember some things in his memory. This is why we created a function after the loop (the second code) . By doing this we made the 12 year old board remember a piece of information. Having done that, we gave a set of instructions (the third code) to the boy for which he would have to refer to the piece of information we gave him earlier and told him how to use it. In this case our work is greatly reduced.
Notice that the name of the function (the second piece) we created is flash. In the third piece of code we just typed out the name 'flash' and the duration for which we want it running in brackets and the Arduino brain immediately referred to the function for help and finished the work!
Here I have uploaded the full sketch for the Morse flasher, but use it only if you didn't get it before. Identify your mistakes and then use it.
With this said folks, I end my post. Will keep on updating the blog with more projects and the next one will have nothing to do with LED's (except a little bit). The next project will be something related to privacy and its gonna be legen...........wait for it..................just comin..............dary!

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