Mission 13 - Morse Code Device
 DuinoKit Jr. - View Only. See new "Mission Manual" forum for manual and code   Started by Daniel Alich   2017-06-27 18:07:35 -04:00   Comments: 1    Viewed: 835

  1. Daniel Alich
    Daniel Alich Member Staff Member
    252_1498600589_Mission 13-1.jpg

    252_1498600579_Mission 13-2.jpg

    /*
    Although there are countless Morse Code translators when searching on the web,
    for this project we adapted code from:

    Project #3 Morse Code Translator
    30 Arduino Projects for the Evil Genius - Simon Monk

    We added in Mission #5 - Light Dimmer to adjust the volume of the Piezo Buzzer.


    */

    int ledPin = 12;
    int buzzerPin = 11; // Must use PWM pin to control volume
    int volume;


    char* letters[] = {
    ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", // A-I
    ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", // J-R
    "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.." // S-Z
    };

    char* numbers[] = {"-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----."};

    int dotDelay = 200;

    void setup()
    {
    pinMode(ledPin, OUTPUT);
    pinMode(buzzerPin,OUTPUT);
    Serial.begin(9600);
    }

    void loop()
    {
    char ch;
    if (Serial.available()) // is there anything to be read from USB?
    {
    ch = Serial.read(); // read a single letter
    if (ch >= 'a' && ch <= 'z')
    {
    flashSequence(letters[ch - 'a']);
    }
    else if (ch >= 'A' && ch <= 'Z')
    {
    flashSequence(letters[ch - 'A']);
    }
    else if (ch >= '0' && ch <= '9')
    {
    flashSequence(numbers[ch - '0']);
    }
    else if (ch == ' ')
    {
    delay(dotDelay * 4); // gap between words
    }
    }
    }

    void flashSequence(char* sequence)
    {
    int i = 0;
    while (sequence[i] != NULL)
    {
    flashDotOrDash(sequence[i]);
    i++;
    }
    delay(dotDelay * 3); // gap between letters
    }

    void flashDotOrDash(char dotOrDash)
    {
    digitalWrite(ledPin, HIGH);
    volume=map(analogRead(A0),0,1023,1,200); // The Map function
    // potValue = analogRead(potPin);
    analogWrite(buzzerPin, volume);

    if (dotOrDash == '.')
    {
    delay(dotDelay);
    }
    else // must be a -
    {
    delay(dotDelay * 3);
    }
    digitalWrite(ledPin, LOW);
    analogWrite(buzzerPin, LOW);

    delay(dotDelay); // gap between flashes
    }

    Daniel Alich, 2017-06-27 18:07:35 -04:00
  2. (You must log in or sign up to post here)

Report Post

Write your reason

Log in | Sign up
Terms & Conditions!
Help!