Corona

[Arduino] Morse Parser

May 8th, 2014
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.92 KB | None | 0 0
  1. char alphabetReference[30] = {' ', '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'}; //Alphabetical Reference Table
  2. char *morseTable[30] = {"dddd", "sl", "lsss", "lsls", "lss", "s", "ssls", "lls", "ssss", "ss", "slll", "lsl", "slss", "ll", "ls", "lll", "slls", "llsl", "sls", "sss", "l", "ssl", "sssl", "sll", "lssl", "lsll", "llss"}; //Morsecode Reference Table, each code corresponds to the same index in the  Alphabetical Reference table.
  3. int sDelay = 200; //Delaytime of the short signal, "."
  4. int lDelay = 600; //Delaytime of the long signal, "-"
  5. int staticPin = 13; //Pin, where to output the signals.
  6. char morseString[] = "SOS"; //String of characters to morse.
  7.  
  8. void illuminate(int time, int pin) {
  9.   //illuminates the pin for the given amount of time.
  10.   digitalWrite(pin, HIGH);
  11.   delay(time);
  12.   digitalWrite(pin, LOW);
  13.   delay(sDelay);
  14. }
  15.  
  16. void morse(int ref, boolean justPrint) {
  17.   //parses the instruction for the corresponding character index, passed with "ref", and then calls the illuminate() function.
  18.   if (!justPrint) {
  19.     Serial.print("\nMorsing \"");
  20.     Serial.print(alphabetReference[ref]);
  21.     Serial.println("\"...");
  22.   }
  23.   char *strtmp = morseTable[ref];//passes the string on index "ref" in the morseTable Array.
  24.  
  25.   for (int j = 0; j < strlen(strtmp); j++) {
  26.     //executes the code, by ripping it from the string, character by character.
  27.     if (strtmp[j] == 's') {
  28.       Serial.print(". ");
  29.       if (!justPrint) {
  30.         illuminate(sDelay, staticPin);
  31.       }
  32.     } else if (strtmp[j] == 'l') {
  33.       Serial.print("- ");
  34.       if (!justPrint) {
  35.         illuminate(lDelay, staticPin);
  36.       }
  37.     } else if (strtmp[j] == 'd') {
  38.       Serial.print("_ ");
  39.       if (!justPrint) {
  40.         delay(sDelay);
  41.       }
  42.     } else {
  43.       Serial.print("Illegal Operation \"");
  44.       Serial.print(strtmp[j]);
  45.       Serial.print("\" for character \"");
  46.       Serial.print(alphabetReference[ref]);
  47.       Serial.println("\"");
  48.     }
  49.   }
  50.   if (!justPrint) {
  51.     delay(sDelay * 2);
  52.   }
  53. }
  54.  
  55. void setup() {
  56.   Serial.begin(9600);
  57.   pinMode(staticPin, OUTPUT);
  58.   Serial.println("Character Mapping:");
  59.   for (int i = 0; i < strlen(alphabetReference); i++) {
  60.     //prints out all characters and morse code counterparts
  61.     Serial.print(alphabetReference[i]);
  62.     Serial.print("\t");
  63.     morse(i, true);
  64.     Serial.println();
  65.   }
  66.  
  67. }
  68.  
  69. void loop() {
  70.   Serial.println("\n-------------------------------------------");
  71.   Serial.print("Preparing to morse \"");
  72.   Serial.print(morseString);
  73.   Serial.println("\"");
  74.   for (int i = 0; i < strlen(morseString); i++) {
  75.     //splits the string into single chars and passes them individually.
  76.     for (int j = 0; j < strlen(alphabetReference); j++) {
  77.       if (toupper(morseString[i]) == toupper(alphabetReference[j])) {
  78.         morse(j, false);
  79.       }
  80.     }
  81.   }
  82.   morse(0, false);
  83. }
Advertisement
Add Comment
Please, Sign In to add comment