Advertisement
Guest User

Morse Keyboard

a guest
Sep 19th, 2017
721
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.91 KB | None | 0 0
  1. /*
  2.  * MORSE KEYBOARD v0.9
  3.  *
  4.  * By Saumil Shah @therealsaumil
  5.  *
  6.  * Switch S1 will drive the red LED. When pressed the LED shall
  7.  * illuminate and when released the LED will turn off.
  8.  *
  9.  * Detect transitions - from OFF to ON and ON to OFF
  10.  * Note down the time at each transition, in milliseconds
  11.  * Record the time duration of each pulse in milliseconds
  12.  * Identify DITs and DAHs based on pulse duration
  13.  * Identify letter gaps
  14.  * Represent Morse Symbols as numbers
  15.  *    Each DIT represented by 1
  16.  *    Each DAH represented by 3
  17.  *    So H = 1111
  18.  *       I =   11
  19.  *       D =  311
  20.  *       I =   11
  21.  *       O =  333
  22.  *       T =    3
  23.  * Decode A Morse Code into an English Alphabet
  24.  * Incorporate de-bounce
  25.  */
  26.  
  27. #include "DigiKeyboard.h"
  28.  
  29. #define  SWITCH_OFF  HIGH
  30. #define  SWITCH_ON   LOW
  31.  
  32. // constants for pin numbers
  33. const int switch1 = 0;
  34. const int redLED =  1;
  35.  
  36. char alphabet[26] = {
  37.    'A', 'B', 'C', 'D', 'E',
  38.    'F', 'G', 'H', 'I', 'J',
  39.    'K', 'L', 'M', 'N', 'O',
  40.    'P', 'Q', 'R', 'S', 'T',
  41.    'U', 'V', 'W', 'X', 'Y',
  42.    'Z'
  43. };
  44.  
  45. int code[26] = {
  46.      13, 3111, 3131,  311,    1,
  47.    1131,  331, 1111,   11, 1333,
  48.     313, 1311,   33,   31,  333,
  49.    1331, 3313,  131,  111,    3,
  50.     113, 1113,  133, 3113, 3133,
  51.    3311
  52. };
  53.  
  54. int unit;
  55. int dit_length, dah_length;
  56. int letter_gap, word_gap;
  57. int numeric_morse_letter;
  58.  
  59. const int debounce = 2;    // 2 milliseconds for debouncing
  60.  
  61. boolean stateS1, previous_state, symbol_observed;
  62.  
  63. unsigned long time_key_pressed, time_key_released;
  64.  
  65. void setup() {
  66.    unit = 120;             // 10 WPM
  67.  
  68.    dit_length = unit;
  69.    dah_length = 3 * unit;
  70.  
  71.    letter_gap = 3 * unit;
  72.    word_gap = 7 * unit;
  73.    
  74.    // initialize the LED pin as an output:
  75.    pinMode(redLED, OUTPUT);
  76.    // initialize the pushbuttons as input:
  77.    pinMode(switch1, INPUT_PULLUP);
  78.  
  79.    previous_state = SWITCH_OFF;
  80.    symbol_observed = false;
  81.    numeric_morse_letter = 0;
  82.  
  83.    // send a NULL keystroke
  84.    DigiKeyboard.sendKeyStroke(0);
  85. }
  86.  
  87. void loop() {
  88.    stateS1 = digitalRead(switch1);
  89.  
  90.    if(stateS1 == SWITCH_ON) {
  91.       key_pressed();
  92.       digitalWrite(redLED, HIGH);
  93.    }
  94.    else {
  95.       key_released();
  96.       digitalWrite(redLED, LOW);
  97.    }
  98.    previous_state = stateS1;
  99. }
  100.  
  101. void key_pressed() {
  102.    if(stateS1 != previous_state) {
  103.       // TRANSITION - we just went from OFF to ON
  104.       time_key_pressed = millis();
  105.    }
  106. }
  107.  
  108. void key_released() {
  109.    int position;
  110.    
  111.    if(stateS1 != previous_state) {
  112.       // TRANSITION - we just went from ON to OFF
  113.       time_key_released = millis();
  114.       dit_or_dah(time_key_released - time_key_pressed);
  115.       symbol_observed = true;
  116.    }
  117.    else {
  118.       if(symbol_observed == true) {
  119.          if(millis() - time_key_released > letter_gap) {
  120.             position = search_code(numeric_morse_letter);
  121.             if(position != 26) {
  122.                DigiKeyboard.println(alphabet[position]);
  123.             }
  124.             else {
  125.                DigiKeyboard.println('?');
  126.             }
  127.             numeric_morse_letter = 0;
  128.             symbol_observed = false;
  129.          }
  130.       }
  131.    }
  132. }
  133.  
  134. void dit_or_dah(unsigned long t) {
  135.    char symbol = '?';
  136.  
  137.    // if the width is less than debounce delay, then do nothing
  138.    if(t <= debounce) {
  139.       return;
  140.    }
  141.    
  142.    if(t <= dit_length * 2) {
  143.       symbol = '.';
  144.       numeric_morse_letter = numeric_morse_letter * 10;
  145.       numeric_morse_letter = numeric_morse_letter + 1;
  146.    }
  147.    if(t > dah_length * 0.75) {
  148.       symbol = '-';
  149.       numeric_morse_letter = numeric_morse_letter * 10;
  150.       numeric_morse_letter = numeric_morse_letter + 3;
  151.    }
  152.  
  153.    DigiKeyboard.print(symbol);
  154. }
  155.  
  156.  
  157. // Function to look up a numeric morse code pattern
  158. // and return its index position
  159. int search_code(int n) {
  160.    int index = 0;
  161.  
  162.    while(index < 26) {
  163.       if(n == code[index]) {
  164.          break;
  165.       }
  166.       index = index + 1;
  167.    }
  168.    return(index);
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement