Created
March 18, 2018 15:04
-
-
Save vlna/ecaaa86c80c4835f4520895f82db82bf to your computer and use it in GitHub Desktop.
morse encoder for Arduino + multi-function shield
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// morse encoder for Arduino + multi-function shield | |
// (c) Vladimír Návrat, all rights reserved. | |
//--------------------------------------------------------------------------- | |
// ISC License | |
// | |
// Permission to use, copy, modify, and/or distribute this software for any | |
// purpose with or without fee is hereby granted, provided that the above | |
// copyright notice and this permission notice appear in all copies. | |
// | |
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR | |
// IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
// partialy based on Public Domain SerialEvent example by Tom Igoe | |
#define BUZZER 3 | |
#define SPEED 100 //dot length | |
String inputString = ""; // a String to hold incoming data | |
boolean stringComplete = false; // whether the string is complete | |
void setup() { | |
// initialize serial: | |
Serial.begin(9600); | |
// reserve 256 bytes for the inputString: | |
inputString.reserve(256); | |
pinMode(BUZZER, OUTPUT); | |
//delay(100); optional buzzer test | |
digitalWrite(BUZZER, HIGH); | |
Serial.println("RDY"); | |
} | |
void loop() { | |
// broadcast the string when a newline arrives: | |
if (stringComplete) { | |
Serial.println(inputString); | |
int c = inputString.length(); | |
for (int i = 0; i < c; i++) { | |
morse(inputString[i]); | |
} | |
// clear the string: | |
inputString = ""; | |
stringComplete = false; | |
} | |
} | |
// Morse encoder, partialy based on code (C)2016 Radu Motisan , www.pocketmagic.net | |
const char *latin = "**ETIANMSURWDKGOHVF*L*PJBXCYZQ**54*3***2*******16*******7***8*90"; //64 | |
void dash() { | |
Serial.print("-"); | |
digitalWrite(BUZZER, LOW); | |
delay(3 * SPEED); | |
digitalWrite(BUZZER, HIGH); | |
delay(SPEED); | |
} | |
void dot() { | |
Serial.print("."); | |
digitalWrite(BUZZER, LOW); | |
delay(SPEED); | |
digitalWrite(BUZZER, HIGH); | |
delay(SPEED); | |
} | |
void conv(uint8_t decimal) { | |
if (decimal) { | |
conv(decimal / 2); | |
if (decimal != 1) decimal % 2 ? dash() : dot(); | |
} | |
} | |
void morse(char c) { | |
if (c >= 'a' && c <= 'z') { | |
c -= 32; // convert to uppercase | |
} | |
if (c == ' ') { | |
Serial.print("/"); | |
delay(4 * SPEED); | |
} | |
if ((c < 'A' || c > 'Z') && (c < '0' || c > '9')) return; | |
int i = 0; | |
while (latin[++i] != c); | |
conv(i); | |
Serial.print("/"); | |
delay(2 * SPEED); | |
} | |
/* | |
SerialEvent occurs whenever a new data comes in the hardware serial RX. This | |
routine is run between each time loop() runs, so using delay inside loop can | |
delay response. Multiple bytes of data may be available. | |
*/ | |
void serialEvent() { | |
while (Serial.available()) { | |
// get the new byte: | |
char inChar = (char)Serial.read(); | |
// add it to the inputString: | |
inputString += inChar; | |
// if the incoming character is a newline, set a flag so the main loop can | |
// do something about it: | |
Serial.println(inChar); | |
if (inChar == '\n') { | |
stringComplete = true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment