Feedback requested on my first Arduino project

Hi all,

Yesterday I received my first Arduino kit and last night I happily started working on my first project. Inspired by the blink-example I wanted to create a sketch that converts a text string (baked into the sketch) into morse output on LED and buzzer. I took me some time, but after a couple of hours I had it working as I wanted. But, although it does what it should, I am interested to learn if I did it right and if there is room for improvement (I’m pretty sure there is ;).

Some specific questions:

  • I used the HashMap library (http://www.arduino.cc/playground/Code/HashMap) because I couldn’t find a way to use regular Arduino arrays to assign morse patterns (S = short; L = long) to alphabetic letters, but perhaps this is overkill and there is a better way without requiring additional libraries?
  • I used 2 220 Ohm resistors, one for the LED and one for the buzzer. I did this because I was afraid to kill the LED, buzzer or Arduino, and 220 Ohm was the lowest resistor I had. Does this make sense? Do I need the resistors here?
  • I put the LED and buzzer on separate pins, but perhaps I can serialize them? The buzzer sounds using a analogWrite of 128 to pin 11, but perhaps I could use a digitalWrite here as well? Would I still need two resistors, or would that lower the power too much?

Apart from these questions I am very much interested to get any kind of feedback. All advice is greatly appreciated. My goal is to learn as much as possible about clean hardware and code set-up now, to prevent me from teaching myself bad standards.

This is my hardware set-up:

And this is the code:

/*
  morse code v0.1
  7/Dec/2011 - Vincent van Scherpenseel
*/

#include <HashMap.h>
const byte HASH_SIZE = 26;
HashType<char,char*> hashRawArray[HASH_SIZE];
HashMap<char,char*> hashMap = HashMap<char,char*>( hashRawArray , HASH_SIZE );

void setup() {  

  //setup hashmap
  hashMap[0]('A',"SL");
  hashMap[1]('B',"LSSS");
  hashMap[2]('C',"LSLS");
  hashMap[3]('D',"LSS");
  hashMap[4]('E',"S");
  hashMap[5]('F',"SSLS");
  hashMap[6]('G',"LLS");
  hashMap[7]('H',"SSSS");
  hashMap[8]('I',"SS");
  hashMap[9]('J',"SLLL");
  hashMap[10]('K',"LSL");
  hashMap[11]('L',"SLSS");
  hashMap[12]('M',"LL");
  hashMap[13]('N',"LS");
  hashMap[14]('O',"LLL");
  hashMap[15]('P',"SLLS");
  hashMap[16]('Q',"LLSL");
  hashMap[17]('R',"SLS");
  hashMap[18]('S',"SSS");
  hashMap[19]('T',"L");
  hashMap[20]('U',"SSL");
  hashMap[21]('V',"SSSL");
  hashMap[22]('W',"SLL");
  hashMap[23]('X',"LSSL");
  hashMap[24]('Y',"LSLL");
  hashMap[25]('Z',"LLSS");
 
  Serial.begin(9600);
  pinMode(11, OUTPUT);  // buzzer
  pinMode(13, OUTPUT);  // LED
}

String textMsg = "FOOBAR";

void loop() {  
  Serial.print( "Input text: " ); Serial.println( textMsg );
  
  for (int i = 0; i < textMsg.length(); i++) {
    String morseCode = hashMap.getValueOf( textMsg.charAt(i) );
    Serial.print( textMsg.charAt(i) );
  
    for (int j = 0; j < morseCode.length(); j++) {
      if ( morseCode[j] == 83 ) {    // S: short blink
        morseBlink(500);
      } else if (morseCode[j] == 76) {    // L: long blink
        morseBlink(1000);
      }
      delay(100);
    }
  }
  
  delay(3000);  // Repeat same word after 3 seconds of silence.
  
}

void morseBlink(int blinkDelay) {
  analogWrite(11,128);
  digitalWrite(13, HIGH);
  delay(blinkDelay);
  digitalWrite(11, LOW);
  digitalWrite(13, LOW);
}

Thank you very much for your time,
Vincent

vvanscherpenseel:

  • I used 2 220 Ohm resistors, one for the LED and one for the buzzer. I did this because I was afraid to kill the LED, buzzer or Arduino, and 220 Ohm was the lowest resistor I had. Does this make sense? Do I need the resistors here?

Hi , when you use a LED It's recommended to use a resistor, because if you don't do it the life of the LED will be shorter!

Look here to calculate the value of the resistor:
http://led.linear1.org/1led.wiz

Bye.

hashMap[0]('A',"SL");
  hashMap[1]('B',"LSSS");
  hashMap[2]('C',"LSLS");
  hashMap[3]('D',"LSS");
  hashMap[4]('E',"S");
  hashMap[5]('F',"SSLS");
  hashMap[6]('G',"LLS");
  hashMap[7]('H',"SSSS");

etc, could be much simpler:

const char* morseCode [] = {"SL", "LSSS", "LSLS", "LSS"  etc };

Then char* code = morseCode [letter - 'A'] ;
(with appropriate checks to ensure that "letter" is uppercase and in the range A - Z)

Also:

 if ( morseCode[j] == 'S' ) {    //  short blink
        morseBlink(500);
      } else if (morseCode[j] == 'L') {    // L: long blink

is much easier to read.

Thank you both very much for your replies.

@AWOL:
In your code example:

const char* morseCode [] = {"SL", "LSSS", "LSLS", "LSS"  etc };

I understand that I can create an array like this, but how do I get the right key for a letter? I don't see that 'A' = "SL" in this example.

What does 'letter' hold?:

char* code = morseCode [letter - 'A'] ;

but how do I get the right key for a letter? I don’t see that ‘A’ = “SL” in this example.

‘A’ is the first letter in the alphabet, so it is the first letter in the array.
‘Z’ is the last in the alphabet, and the last letter in the array.

char* text = "THE QUICK BROWN FOX";
for (int i = 0; i < strlen (text); ++i) {
  char letter = text [i];
  char* code = morseCode [letter - 'A'] ;
  transmitCode (code);
}

(sorry, I don’t like/use String)

Ah yes, I get it now :slight_smile: Thanks!