MorseFlasher.ino 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Created by ChatGPT, and symbols added by Bing Chat
  2. const int LED = 4;
  3. const int dotDuration = 350;
  4. const int dashDuration = 3 * dotDuration;
  5. const int interLetterPause = 2 * dotDuration;
  6. const int interWordPause = 4 * dotDuration;
  7. const int bufferSize = 50; // max length of the input buffer
  8. char* letters[] = {
  9. ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", // A-I
  10. ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", // J-R
  11. "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.." // S-Z
  12. };
  13. //For Numbers
  14. char* numbers[] = {
  15. "-----", ".----", "..---", "...--", "....-", ".....",
  16. "-....", "--...", "---..", "----."
  17. };
  18. char* symbols[] = {
  19. ".-.-.-", "--..--", "..--..", ".----.", "-.-.--", "-..-.", // . , ? , etc.
  20. "-.--.", "-.--.-", ".-...", "---...", "-.-.-.", // ( ), :, etc.
  21. "-...-", ".-.-.", "--...-", ".-..-.", ".--.-.", // = , + , / , @ , etc.
  22. "..--.-", "-....-", // _ , -
  23. };
  24. char inputBuffer[bufferSize + 1]; // input buffer
  25. int bufferIndex = 0; // current index in the input buffer
  26. void morse_setup() {
  27. pinMode(LED, OUTPUT);
  28. //Serial.begin(9600);
  29. }
  30. void morse_loop() {
  31. if (Serial.available()) {
  32. bufferIndex = 0;
  33. Serial.print("Message: ");
  34. while (Serial.available()) {
  35. char input = Serial.read();
  36. Serial.print(input);
  37. // add the input to the buffer
  38. inputBuffer[bufferIndex] = tolower(input);
  39. bufferIndex = (bufferIndex + 1) % bufferSize;
  40. if (input == 10 || input == 13) {
  41. inputBuffer[bufferIndex++] = ' ';
  42. Serial.flush();
  43. break;
  44. }
  45. }
  46. }
  47. // repeat the last input forever
  48. for (int i = 0; i < bufferIndex; i++) {
  49. if (Serial.available()) {
  50. Serial.println();
  51. break;
  52. }
  53. char input = inputBuffer[i];
  54. if (input == ' ' || (input >= 'a' && input <= 'z') || (input >= '0' && input <= '9')) {
  55. Serial.println();
  56. Serial.print(input);
  57. Serial.print(": ");
  58. const char* pattern;
  59. if (input >= 'a' && input <= 'z')
  60. pattern = letters[input - 'a'];
  61. else if (input >= '0' && input <= '9')
  62. pattern = numbers[input - '0'];
  63. else
  64. pattern = symbols[input - '.' + 6];
  65. if (pattern) {
  66. for (int j = 0; pattern[j]; j++) {
  67. if (pattern[j] == '.') dot();
  68. else if (pattern[j] == '-') dash();
  69. }
  70. } else {
  71. space();
  72. }
  73. shortspace();
  74. }
  75. }
  76. }
  77. void dot() {
  78. Serial.print(".");
  79. digitalWrite(LED, HIGH);
  80. delay(dotDuration);
  81. digitalWrite(LED, LOW);
  82. delay(dotDuration);
  83. }
  84. void dash() {
  85. Serial.print("-");
  86. digitalWrite(LED, HIGH);
  87. delay(dashDuration);
  88. digitalWrite(LED, LOW);
  89. delay(dotDuration);
  90. }
  91. void shortspace() {
  92. delay(interLetterPause);
  93. }
  94. void space() {
  95. digitalWrite(LED, LOW);
  96. delay(interWordPause);
  97. }