// written by Emeria for Cari
int button = 2; // connect button to socket 2
int timeUnit = 200; // duration of 1 time unit.
bool buttonState = false; // current state of the button
bool lastButtonState = false; // previous state of the button
unsigned long startPressed = 0; // the moment the button was pressed
unsigned long endPressed = 0; // the moment the button was released
unsigned long holdTime = 0; // how long the button was held
bool decode = false; // flag for if a decode is needed
String morse = ""; // current morse code sequence
void setup() {
pinMode(button, INPUT); // initialize the button pin as a input
Serial.begin(9600); // initialize serial communication
Serial.print("Current time unit is ");
Serial.print(timeUnit);
Serial.println(" milliseconds.\nA dot is 1 time unit.\nA dash is 3 time units.\nPause between each letter is 3 time units.\n\nPlease input your Morse code:");
}
void loop() {
buttonState = digitalRead(button); // read the button input
if (buttonState != lastButtonState) { // button state changed
updateState();
}
if (!buttonState && decode && millis() - endPressed > timeUnit * 3) { // starts decoding if button is released and decode status is true and the pause is long than 3 time units
//Serial.println(endPressed);
//Serial.println(millis());
char result = morseDecode(morse); // decode the sequence
//Serial.print(morse); // uncomment this if you want to see your morse code input
Serial.print(result); // print the result
decode = false; // the current sequence was just decoded so decode status is reset
morse = ""; // reset morse sequence
}
lastButtonState = buttonState; // save state for next loop
}
void updateState() {
if (buttonState) { // the button has just been pressed
//Serial.println("High");
startPressed = millis(); // record button press start time
} else { // the button has just been released
//Serial.println("Low");
endPressed = millis(); // record button release time
//Serial.println(endPressed);
holdTime = endPressed - startPressed; // find out how long the button was pressed for
if (holdTime < 30) { // button presses shorter than 30ms are ignored
return;
}
else { // if button press is longer than 30ms, it is a valid input and thus we need decoding
decode = true;
}
if (holdTime < timeUnit * 2) { // add a dot to the sequence if it's a short press
//Serial.print(".");
morse += ".";
}
if (holdTime >= timeUnit * 2) { // add a dash to the sequence if it's a long press
//Serial.print("-");
morse += "-";
}
}
}
char morseDecode(String morse) {
// decode the given morse sequence into a character
static String letters[] = {
".-", // 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
};
static String numbers[] = {
"-----", // 0
".----", // 1
"..---", // 2
"...--", // 3
"....-", // 4
".....", // 5
"-....", // 6
"--...", // 7
"---..", // 8
"----.", // 9
};
for (int i = 0; i < 26; i++) { // search for a match within the letters
if (letters[i] == morse) {
return char(i+65); // convert to capital letter using the ASCII table
// change 65 to 97 if you want lower case letters
}
}
for (int i = 0; i < 10; i++) { // search for a mtach within the numbers
if (numbers[i] == morse) {
return char(i+48); // convert to numbers using the ASCII table
}
}
return '!'; // if no match is found, return an '!' for error
}