Sinisterly
Arduino Morse Code Translator - Printable Version

+- Sinisterly (https://sinister.li)
+-- Forum: Coding (https://sinister.li/Forum-Coding)
+--- Forum: C, C++, & Obj-C (https://sinister.li/Forum-C-C-Obj-C)
+--- Thread: Arduino Morse Code Translator (/Thread-Arduino-Morse-Code-Translator)



Arduino Morse Code Translator - Inori - 03-10-2016

I got my first microcontroller yesterday, a Sainsmart Nano V3 (which runs Arduino's firmware). I got bored in CompSci, having already finished my class project, so I made this. It's pretty cross-platform, in the realm of 8-bit controllers. Anyway, have fun with it Tongue

Note: I'm working on getting Serial input, which is difficult, to say the least, but will post the updated code if or when it's done.

Note 2: Possible microcontroller sub in C/C++/Obj-C @Oni/@Skullmeat?

Code:
#define LEDPin 13 // pin value for LED to blink #define Str "dongs" // string to encode #define Space 100 // delay between blinks #define Blink 175 // short blink time #define Multi 2 // long blink multiplier // new struct for k/v (char/pattern) pairs struct mH{char k,*v;}; // map char/pattern pairs mH morseHash[36]={ {'a',".-"}, {'b',"-..."}, {'c',"-.-."}, {'d',"-.."}, {'e',"."}, {'f',"..-."}, {'g',"--."}, {'h',"...."}, {'i',".."}, {'j',".---"}, {'k',".-.-"}, {'l',".-.."}, {'m',"--"}, {'n',"-."}, {'o',"---"}, {'p',".--."}, {'q',"--.-"}, {'r',".-."}, {'s',"..."}, {'t',"-"}, {'u',"..-"}, {'v',"...-"}, {'w',".--"}, {'x',"-..-"}, {'y',"-.--"}, {'z',"--.."}, {'1',".----"}, {'2',"..---"}, {'3',"...--"}, {'4',"....-"}, {'5',"....."}, {'6',"-...."}, {'7',"--..."}, {'8',"---.."}, {'9',"----."}, {'0',"-----"} }; // encode an ASCII string to morse pattern, taking a string as argument String encode(String string){ // create empty string to store patterns String mWord=""; // loop through string for(size_t i=0;string[i];i++){ // loop through char/pattern pairs for(size_t n=0;n<sizeof morseHash/sizeof *morseHash;n++){ // if a match is found, concatenate the found char's pattern to mWord if(string[i]==morseHash[n].k) mWord+=morseHash[n].v; break; } } return mWord; } // blink encoded patterns via LED, accepts string as argument void emit(String str){ // encode string to pattern String morse=encode(str); // loop through encoded string for(int i=0;i<=morse.length();i++){ // turn on LED digitalWrite(LEDPin,HIGH); // delay for `Blink` milliseconds // times the multiplier, if needed delay(Blink*morse[i]=='-'?Multi:1); // turn off the LED digitalWrite(LEDPin,LOW); // delay for `Space` milliseconds delay(Space); } } void setup(){ // set defined LED pin to output pinMode(LEDPin,OUTPUT); // emit set string emit(Str); } void loop(){}



RE: Arduino Morse Code Translator - Skullmeat - 03-10-2016

(03-10-2016, 01:57 AM)Inori Wrote: Note 2: Possible microcontroller sub in C/C++/Obj-C @Oni/@Skullmeat?

Hmm. That sounds interesting. I have a Raspberry Pi B+ myself.


RE: Arduino Morse Code Translator - Inori - 03-10-2016

(03-10-2016, 02:09 AM)Skullmeat Wrote: I have a Raspberry Pi B+ myself.

Ugh, I was going to get a Pi 2 A+ or a Pi Zero, but they're never in stock ANYWHERE.


RE: Arduino Morse Code Translator - BurritoBeans - 03-10-2016

Wow, sweet code. I'm a HAM Radio operator so stuff like this is useful to me, as I still haven't learned Morse Code but there's a good amount of people that use it.

And with microcontrollers, I have too many... Models A, B, 2B, 3 on the way and Zero for RPis, two CHIP boards, a BeagleBone Black, PandaBoard, Intel Galileo microcontroller board, lots of little Arduinos I've made/bought over the years, and two of those 18-core Paralellas. I'd definitely try to make a few posts if there was a sub for it.


RE: Arduino Morse Code Translator - 0xDEAD10CC - 06-19-2016

Code is not bad, but shouldn't this:
Code:
for (int i = 0; i <= morse.length(); i++)

Be:
Code:
for (int i = 0; i < morse.length(); i++)

Since array indexing is 0-based which is the length - 1, and so you don't want to loop up to < or = length(), and just < length()? length() if it returns the length up to the null-terminator will return the # of characters before it, which means string[length()] would be the null terminator, which you're including in that loop you've got going on there...


RE: Arduino Morse Code Translator - Inori - 06-19-2016

(06-19-2016, 09:39 AM)0xDEAD10CC Wrote: Code is not bad, but shouldn't this:
Code:
for (int i = 0; i <= morse.length(); i++)

Be:
Code:
for (int i = 0; i < morse.length(); i++)

Since array indexing is 0-based which is the length - 1, and so you don't want to loop up to < or = length(), and just < length()? length() if it returns the length up to the null-terminator will return the # of characters before it, which means string[length()] would be the null terminator, which you're including in that loop you've got going on there...

Made this so long ago that I forget my rationale for using the first example, but it worked as intended iirc


RE: Arduino Morse Code Translator - 0xDEAD10CC - 06-19-2016

(06-19-2016, 11:00 AM)Emilia Wrote:
(06-19-2016, 09:39 AM)0xDEAD10CC Wrote: Code is not bad, but shouldn't this:
Code:
for (int i = 0; i <= morse.length(); i++)

Be:
Code:
for (int i = 0; i < morse.length(); i++)

Since array indexing is 0-based which is the length - 1, and so you don't want to loop up to < or = length(), and just < length()? length() if it returns the length up to the null-terminator will return the # of characters before it, which means string[length()] would be the null terminator, which you're including in that loop you've got going on there...

Made this so long ago that I forget my rationale for using the first example, but it worked as intended iirc

I doubt that it worked as intended, I just don't think you caught the issue - https://www.arduino.cc/en/Reference/String

These strings are null terminated, so you would have been including the null terminator in the morse code loop this whole time. And this line:
Code:
delay(Blink*morse[i]=='-'?Multi:1);

Would have delayed:
Code:
delay(Blink);

Because '\0' != '-'