Saturday, March 08, 2014

Arduino–Day 1: My First Project

 

Before getting my hands dirty, I read the first chapter of the Arduino Projects Book to give me an overview of what am I getting myself into.  I was hooked!  After quickly absorbing the types of components, I read about Series & Parallel circuits. 

My first project was lighting up an LED by hooking up a resister, an LED to the 5v power supply.  I admit I also tried the variation with a switch – depress the switch to get the LED going - from the Arduino book.

At this point, I fired up the IDE and ran the ‘Blink’ sample.  I immediately understood the idea of Arduino’s digital pins.  The pins represent 0/1 (On/off) state by sending / stopping electric current through them.  Cool!

I realized I understood enough to start my first project!  Yes, it’s going to be something I come up with on my own!  It was going to be a Morse Messenger!

I will have my circuit blink out a message in Morse Code!  Of course, what am I good at?  Writing programs Smile

Didn’t realize my C++ was so rusty.  How the heck do you write Jagged arrays?  I’m sorry, Mr. Kanetkar, I have forgotten all my pointer lessons. Sad smile

Though I was using Visual Micro, I hadn’t yet figured out how to debug with break points.  Subject of another blog post I guess.  For now, I was able to make do with “Serial.print*” statements.

Morse.ino
  1. // A dot is a '1',  A dash '0'
  2. int letterMorse[][4] = {
  3.   {1, 0, -1, -1},
  4.   {0, 1, 1, 1},
  5.   {0, 1, 0, 1},
  6.   {0, 1, 1, -1},
  7.   {1, -1, -1, -1},
  8.   {1, 1, 0, 1},
  9.   {0, 0, 1, -1},
  10.   {1, 1, 1, 1},
  11.   {1, 1, -1, -1},
  12.   {1, 0, 0, 0},
  13.   {0, 1, 0, -1},
  14.   {1, 0, 1, 1},
  15.   {0, 0, -1, -1},
  16.   {0, 1, -1, -1},
  17.   {0, 0, 0, -1},
  18.   {1, 0, 0, 1},
  19.   {0, 0, 1, 0},
  20.   {1, 0, 1, -1},
  21.   {1, 1, 1, -1},
  22.   {0, -1, -1, -1},
  23.   {1, 1, 0, -1},
  24.   {1, 1, 1, 0},
  25.   {1, 0, 0, -1},
  26.   {0, 1, 1, 0},
  27.   {0, 1, 0, 0},
  28.   {0, 0, 1, 1}
  29. };
  30.  
  31.  
  32. int numberMorse[][5] = {
  33.   { 0, 0, 0, 0, 0 },
  34.   { 1, 0, 0, 0, 0 },
  35.   { 1, 1, 0, 0, 0 },
  36.   { 1, 1, 1, 0, 0 },
  37.   { 1, 1, 1, 1, 0 },
  38.   { 1, 1, 1, 1, 1 },
  39.   { 0, 1, 1, 1, 1 },
  40.   { 0, 0, 1, 1, 1 },
  41.   { 0, 0, 0, 1, 1 },
  42.   { 0, 0, 0, 0, 1 }
  43. };
  44.  
  45. int led = 10;
  46. const int standardPauseInMilliseconds = 500;
  47.  
  48. void setup() {
  49.   // put your setup code here, to run once:
  50.   doMorse("I LOVE ARDUINO");
  51. }
  52.  
  53. void loop() {
  54. }
  55.  
  56. void doMorse(String message)
  57. {
  58.   // put your main code here, to run repeatedly:
  59.   for (int i = 0; i < message.length(); i++)
  60.   {
  61.     char letter = message.charAt(i);
  62.  
  63.     Serial.println(letter);
  64.  
  65.     if (letter == ' ')
  66.     {
  67.       //Words are separated by a space equal to 7 dots
  68.       delay(standardPauseInMilliseconds * 7);
  69.       continue;
  70.     }
  71.  
  72.  
  73.     int value = ((int)letter) - 65;
  74.     bool isNumber = value < 0;
  75.  
  76.     Serial.println(value);
  77.  
  78.     int *component = isNumber ? numberMorse[value] : letterMorse[value];
  79.  
  80.     for (int j = 0; j < 4; j++)
  81.     {
  82.  
  83.       int number = component[j];
  84.       if (number < 0) continue;
  85.  
  86.       bool isDot = (number);
  87.  
  88.       Serial.print(isDot ? "Dot- " : "Dash- ");
  89.  
  90.       //For the duration of a dash (0) is 3 times the duration of a dot (1)
  91.       blink(isDot ? standardPauseInMilliseconds : standardPauseInMilliseconds * 3);
  92.       beep(isDot ? standardPauseInMilliseconds : standardPauseInMilliseconds * 3);
  93.  
  94.       //Each dot / dash is followed by a short silence, equal to the dot duration
  95.       delay(standardPauseInMilliseconds);
  96.     }
  97.  
  98.     Serial.println(' ');
  99.  
  100.     //The space between letters is three units
  101.     delay(standardPauseInMilliseconds * 3);
  102.  
  103.   }
  104. }
  105.  
  106. void blink(int delayInMilliseconds)
  107. {
  108.   pinMode(led, OUTPUT);
  109.   //Turn on the light
  110.   digitalWrite(led, HIGH);
  111.   //For the duration of a dash (0) is 3 times the duration of a dot (1)
  112.   delay(delayInMilliseconds);
  113.   digitalWrite(led, LOW);
  114.  
  115. }
  116.  
  117. void beep(int delayInMilliseconds)
  118. {
  119.   pinMode(led, OUTPUT);
  120.   analogWrite(led, 20);
  121.   delay(delayInMilliseconds);
  122.   analogWrite(led, 0);
  123. }

The first version of the circuit did not have the Piezo.  However, I realized I was not able to read the message (despite the fact I was trying to decipher Morse Code for the first time in my life Smile), and I needed an audible cue to distinguish dashes from dots.  Lo & behold: The parallel circuit with both LED & Piezo.

It’s interesting to note that Arduino allows the use of the same pin (10) for controlling both Piezo  (analog) & LED (digital).

 

WP_20140308_12_46_24_Pro - Copy

1 comment:

for IT the said...

Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from Dot Net Training in Chennai. or learn thru ASP.NET Essential Training Online . Nowadays Dot Net has tons of job opportunities on various vertical industry.