Author: Jacob Mason Language: c
Description: Arduino UNO morse code recorder and transmitter Timestamp: 2013-06-07 02:24:03 +0000
View raw paste Reply
  1. /* Arduino UNO morse code recorder and transmitter
  2.    written by Jacob Mason
  3.    6/6/2013
  4. */
  5.  
  6. #define DOT 0
  7. #define DASH 1
  8. #define SPACE 2
  9. #define END_MESSAGE 3
  10. #define RESET 13
  11. #define RECORD 12
  12. #define END 8
  13. #define DOT_LED 7
  14. #define DASH_LED 4
  15. #define LED 2
  16. #define MESSAGE_LENGTH 50
  17.  
  18. int message[MESSAGE_LENGTH];
  19. int timeHeld = 0; //time that the record button was pressed
  20. int arrayPosition = 0;
  21. int transmit = 0;
  22. int index = 0;
  23.  
  24. void setup() {
  25.   pinMode(RESET, INPUT);
  26.   pinMode(RECORD, INPUT);
  27.   pinMode(END, INPUT);
  28.   pinMode(LED, OUTPUT);
  29.   pinMode(DOT_LED, OUTPUT);
  30.   pinMode(DASH_LED, OUTPUT);
  31. }
  32.  
  33. void loop() {
  34.   if (digitalRead(RESET)) {
  35.     arrayPosition = 0;
  36.     index = 0;
  37.     for (int i; i < MESSAGE_LENGTH; i++) {
  38.       message[i] = 3;
  39.     }
  40.     flash_led(1, DOT_LED);
  41.     flash_led(1, DASH_LED);
  42.     flash_led(1, LED);
  43.   }
  44.  
  45.   else if (digitalRead(RECORD)) {
  46.     while (digitalRead(RECORD)) {
  47.       delay(1000);
  48.       timeHeld++;
  49.       if (timeHeld < 2) {
  50.         //dot
  51.         flash_led(1, DOT_LED);
  52.       }
  53.       else if (timeHeld == 2) {
  54.         //dash
  55.         flash_led(1, DASH_LED);
  56.       }
  57.       else if (timeHeld > 2) {
  58.         //space
  59.         flash_led(1, LED);
  60.       }
  61.     }
  62.  
  63.     if (timeHeld < 2) {
  64.       //button being held for less than 2 seconds indicates a "dot" should be added to the message
  65.       if (arrayPosition < MESSAGE_LENGTH) {
  66.         message[arrayPosition] = DOT;
  67.         arrayPosition++;
  68.       }
  69.     }
  70.     else if (timeHeld == 2) {
  71.       //button was held for 2 seconds
  72.       //indicating a "dash" is to be added to the message
  73.       if (arrayPosition < MESSAGE_LENGTH) {
  74.         message[arrayPosition] = DASH;
  75.         arrayPosition++;
  76.       }
  77.     }
  78.     else if (timeHeld > 2) {
  79.       //button being held for 3 seconds indicates a space
  80.       if (arrayPosition < MESSAGE_LENGTH) {
  81.         message[arrayPosition] = SPACE;
  82.         arrayPosition++;
  83.       }
  84.     }
  85.     timeHeld = 0;
  86.   }
  87.  
  88.   else if (digitalRead(END)) {
  89.     flash_led(1, DASH_LED);
  90.  
  91.     if (arrayPosition < MESSAGE_LENGTH)
  92.       message[arrayPosition] = END_MESSAGE;
  93.    
  94.     if (transmit == 0)
  95.       transmit = 1;
  96.     else
  97.       transmit = 0;
  98.   }
  99.  
  100.   if (transmit == 1) {
  101.     if ((index < arrayPosition) && (message[index] != END_MESSAGE)) {
  102.       play(message[index]);
  103.       index++;
  104.     }
  105.     else if ((index == arrayPosition) || (message[index] != END_MESSAGE)) {
  106.       index = 0;
  107.       digitalWrite(DOT_LED, HIGH);
  108.       digitalWrite(DASH_LED, HIGH);
  109.       delay(100);
  110.       digitalWrite(DOT_LED, LOW);
  111.       digitalWrite(DASH_LED, LOW);
  112.       delay(100);
  113.       play(SPACE);
  114.     }
  115.   }
  116. }
  117.  
  118. void play(int data) {
  119.   if (data == DOT) {
  120.     digitalWrite(LED, HIGH);
  121.     digitalWrite(DOT_LED, HIGH);
  122.     delay(500);
  123.     digitalWrite(LED, LOW);
  124.     digitalWrite(DOT_LED, LOW);
  125.     delay(200);
  126.   }
  127.   else if (data == DASH) {
  128.     digitalWrite(LED, HIGH);
  129.     digitalWrite(DASH_LED, HIGH);
  130.     delay(1000);
  131.     digitalWrite(LED, LOW);
  132.     digitalWrite(DASH_LED, LOW);
  133.     delay(200);
  134.   }
  135.   else if (data == SPACE) {
  136.     delay(500);
  137.   }
  138. }
  139.  
  140. void flash_led(int repeat, int led) {
  141.   while (repeat) {
  142.     digitalWrite(led, HIGH);
  143.     delay(500);
  144.     digitalWrite(led, LOW);
  145.     delay(500);
  146.     repeat--;
  147.   }
  148. }
  149.  
View raw paste Reply