/* Project: LED, active piezo buzzer. Translator of symbols (letters, numbers, words, sentences) to Morse code Function: Converts to Morse code, prints in Serial monitor, flashes with LED connected to digital pin 7 and sounds the signal through active piezo buzzer. */ //******************************************************************************* const int ledPin = 7; //LED attached to digital pin 7 const int buzzerPin = 6;//piezo active buzzer attached to digital pin 6 char* letters[] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."}; //letters from A to Z char* numbers[] = {"-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----."}; //numbers from 0-9 int dotDuration = 1000; //sets dot duration //******************************************************************************* void setup() { pinMode(ledPin, OUTPUT); //sets the ledPin as OUTPUT pinMode(buzzerPin,OUTPUT);//sets the buzzerPin as OUTPUT Serial.begin(9600);//initialise the serial communication on 9600 bps Serial.println("Please enter your symbols to convert"); Serial.println("************************************"); Serial.println("See your symbols converted to Morse code below: "); } //******************************************************************************* void loop() { char symbol; if (Serial.available())//check if something typed in Serial monitor { symbol = Serial.read();//read a single symbol if (symbol >= 'a' && symbol <= 'z') { flashSequence(letters[symbol - 'a']); } else if (symbol >= 'A' && symbol <= 'Z') { flashSequence(letters[symbol - 'A']); } else if (symbol >= '0' && symbol <= '9') { flashSequence(numbers[symbol - '0']); } else if (symbol == ' ') { delay(dotDuration * 10); //duration between different signals(words) Serial.print(" "); } } } //****************************************************** void flashSequence(char* sequence) { int i = 0; while (sequence[i] != NULL) { flashDotOrDash(sequence[i]); i++; } } //******************************************************** void flashDotOrDash(char dotOrDash) { digitalWrite(ledPin, HIGH);//LED is on if (dotOrDash == '.') //must be a dot { digitalWrite(buzzerPin,HIGH); delay(dotDuration);//duration of dot digitalWrite(buzzerPin,LOW); Serial.print("."); } else // must be a dash { digitalWrite(buzzerPin,HIGH); delay(dotDuration * 4);//duration of dash can be from 4 to 6 seconds digitalWrite(buzzerPin,LOW); Serial.print("-"); } digitalWrite(ledPin, LOW);//LED is off delay(dotDuration); // duration between flashes }