2

I'm using the Arduino software to create a Morse code decoder using the serial monitor.

I enter a Morse code such as .- as a text string and it's displayed in the LCD as a alphanumeric text. So if I enter .- into the serial Monitor, A is displayed on the LCD. But it only works for single symbols, not the dot and dash together.

Is there a way to make the if statement as seen below accept both the dots and dashes together?

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup()
{
  lcd.begin(16, 2);

 Serial.begin(9600);
}

void loop()
{
  char schar;

  if (Serial.available())
  {
    schar = Serial.read();

    Serial.write(schar);

    if (schar== '.-' ){
      Serial.write('A');
      lcd.print('a');
    }   
  }  
}

3 Answers 3

4

Your first problem is that there is no way for schar to contain two characters in the first place; Since schar is declared as a char, it can only contain one character.

You need to make a String out of the serial input. Serial.read() only returns one character at a time, so you will need to make sure you iterate over all the input. Then you can compare the whole String with another, and see if both characters match at once. Just look at this code to see different ways of manipulating Strings: http://arduino.cc/en/Tutorial/StringComparisonOperators

Notice that string literals use double quotes, not single quotes like characters.

Also, be aware of the fact that string and String are different things. lowercase 's' is an array of characters, capitol 'S' is the class that has those useful string comparison member functions. The class is more resource intensive, but I think in your case the readability and convenience are worth it.

3
  • i'm still new but should i use char schar[2]; Commented Dec 14, 2014 at 14:07
  • That would be equivalent to a string (lowercase) on arduino, sometimes called a "c style string". It can work, but using the functions built in to arduino's String class might be easier. that would be String schar; Commented Dec 14, 2014 at 17:52
  • A string seems like overkill here. The lowercase one would use much less memory (if(data[0] == '.' && data[1] == '-') would be more efficient) Commented Dec 15, 2014 at 0:06
2

You could use C strings, (type char*) or C++ String objects (type String). Both would work.

If you use C strings, you'll need to use functions like strcmp() from the C library to see if your input string matches a particular morse sequence.

If you use C++ String objects, you'll need to use the methods of that class. I'm less familiar with that class.

If you have single Morse characters, matching them up is pretty easy. If you have a continuous stream of dots and dashes, though, it's a much thornier problem. You need a way to figure out the boundary between characters. Morse characters can be sequences of between 1 and 5 dots and dashes.

Unless there are extra delays between the dots/dashes for a character, I'm not sure how you figure out where the boundaries are. There are already computer Morse code readers, though, so this must be a "solved problem". I'd do some googling.

1
1

schar is a char variable of size 1. It makes no sense to compare it with '.-', which is supposed to be a string. Declare a char[3] array, fill it as string and use the strcmp function. It's elementary C knowledge.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.