Arduino Morse Code Translator 03-10-2016, 01:57 AM
#1
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 
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?

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(){}It's often the outcasts, the iconoclasts ... those who have the least to lose because they
don't have much in the first place, who feel the new currents and ride them the farthest.
don't have much in the first place, who feel the new currents and ride them the farthest.

















![[+]](https://sinister.li/images/modern/collapse_collapsed.png)







![[Image: qcYJ3l.png]](https://i.skull.moe/u/qcYJ3l.png)




