Das erste Blink-Tutorial von Arduino brachte mich auf die Idee, einen kleinen Morse-Apparat zu bauen, der sowohl mit Licht als auch durch Töne eine Sequenz von Morsezeichen ausgeben kann. Außerdem soll es ebenso möglich sein, manuell zu morsen.
Der Code generiert zunächst aus einer Zeichenfolge (wobei immer alle Buchstaben großgeschrieben werden müssen) einen Morsecode aus "." und "-". Zusätzlich gibt es noch Trennzeichen zwischen den Signalen, Buchstaben und Wörtern. Dies ist wichtig für die spätere Umwandlung in Licht- bzw. Tonsignale. Im internationalen Morsecode sind die Zeitdauern bei der Signalübermittlung wie folgt:
#define PIN_LED 10
#define PIN_PIEZO 11
#define PIN_BUTTON 8
#define MORSE_DURATION 100
#define MORSE_FREQUENCY 443 // in Hz
String myMessage = "MORSE CODE";
String myMorseCode = "";
void setup()
{
Serial.begin(9600);
pinMode(PIN_LED, OUTPUT);
pinMode(PIN_PIEZO, OUTPUT);
pinMode(PIN_BUTTON, INPUT);
myMorseCode = getMorseCode(myMessage);
Serial.println(myMessage);
Serial.println(myMorseCode);
sendMorse(myMorseCode);
}
void loop()
{
if (digitalRead(PIN_BUTTON) == LOW) {
tone(PIN_PIEZO, MORSE_FREQUENCY);
digitalWrite(PIN_LED, HIGH);
} else {
noTone(PIN_PIEZO);
digitalWrite(PIN_LED, LOW);
}
}
String getMorseCode(String message)
{
String morseCode = "";
for (int i = 0; i < message.length(); i++) {
morseCode += getMorseCode(message[i]);
}
return morseCode;
}
String getMorseCode(char character)
{
String sequence = "";
switch(character) {
case 'A': sequence = ".-"; break;
case 'B': sequence = "-..."; break;
case 'C': sequence = "-.-."; break;
case 'D': sequence = "-.."; break;
case 'E': sequence = "."; break;
case 'F': sequence = "..-."; break;
case 'G': sequence = "--."; break;
case 'H': sequence = "...."; break;
case 'I': sequence = ".."; break;
case 'J': sequence = ".---"; break;
case 'K': sequence = "-.-"; break;
case 'L': sequence = ".-.."; break;
case 'M': sequence = "--"; break;
case 'N': sequence = "-."; break;
case 'O': sequence = "---"; break;
case 'P': sequence = ".--."; break;
case 'Q': sequence = "--.-"; break;
case 'R': sequence = ".-."; break;
case 'S': sequence = "..."; break;
case 'T': sequence = "-"; break;
case 'U': sequence = "..-"; break;
case 'V': sequence = "...-"; break;
case 'W': sequence = ".--"; break;
case 'X': sequence = "-..-"; break;
case 'Y': sequence = "-.--"; break;
case 'Z': sequence = "--.."; break;
case '1': sequence = ".----"; break;
case '2': sequence = "..---"; break;
case '3': sequence = "...--"; break;
case '4': sequence = "....-"; break;
case '5': sequence = "....."; break;
case '6': sequence = "-...."; break;
case '7': sequence = "--..."; break;
case '8': sequence = "---.."; break;
case '9': sequence = "----."; break;
case '0': sequence = "-----"; break;
case ' ': sequence = " "; break;
default: break;
}
if (sequence != " ") {
sequence += "|";
}
return sequence;
}
void sendMorse(String morseCode)
{
for (int i = 0; i < morseCode.length(); i++) {
switch(morseCode[i]) {
case '.':
sendMorseShort();
break;
case '-':
sendMorseLong();
break;
case '|':
delay(MORSE_DURATION * 2); // gap between characters
break;
case ' ':
delay(MORSE_DURATION * 6); // gap between words
break;
}
}
}
void sendMorseShort()
{
digitalWrite(PIN_LED, HIGH);
tone(PIN_PIEZO, MORSE_FREQUENCY);
delay(MORSE_DURATION);
digitalWrite(PIN_LED, LOW);
noTone(PIN_PIEZO);
delay(MORSE_DURATION);
}
void sendMorseLong()
{
digitalWrite(PIN_LED, HIGH);
tone(PIN_PIEZO, MORSE_FREQUENCY);
delay(MORSE_DURATION*3);
digitalWrite(PIN_LED, LOW);
noTone(PIN_PIEZO);
delay(MORSE_DURATION);
}
zurück