Arduino Oscillator with Morse Code Decoder

Arduino Oscillator and Decoder
Arduino Oscillator and Decoder

This week we will be adding a morse code decoder to our Arduino oscillator. The main physical change is the LCD display. Most of the work is done in the programming. At the bottom of the article, I linked several sites that have good programming examples.

The first step is wiring the LCD screen. I like the displays that have the I2C board built on. This cuts the wiring down quite a bite. I2C only requires 4 connections (GND, 5V+, SDA, and SCL). On the Arduino UNO pin A4 is SDA and pin A5 is SCL. For this example a 16 x 2 screen is being used. This can very easily be changed to 20 x 4. To test the LCD, follow the tutorial in the following link.
https://www.ardumotive.com/i2clcden.html

To minimize a slight dimming of the LCD display while the key is pressed, the +5 volts that was connected to the has key been changed to +3.3 volts. No other changes are needed to wiring.

Advertisements
Arduino Oscillator and Decoder
Connect SDA And SCL Lines. Change power on to key 3.3 volts to minimize LCD dimming.

The programming for this project is a mix of the original oscillator project and code from other peoples projects. I found a morse code project uploaded from a school project that used serial to display code instead of a LCD. I was able to use its alphabet but it did not have numbers. After adding numbers to the code and testing with serial, it was time to add the LCD to the code.

In the example the bottom line of the LCD is used to display the code. Once the line is full, it will automatically clear the line and display the last character decoded. If you enter 10 dots in a row it will clear the display. For your project you can use both lines to display code and have it reset once the screen is full.

Advertisements

Arduino Sketch

/* Arduino Oscillator Decoder LCD
 *  More info https://KM4NMP.com/
 *  Author Matthew Carr KM4NMP 4/5/19
 *  
 *  THIS SITE HAS THE LIBRARY USED FOR THE I2C LCD ALSO A GOOD TUTORIAL
 * I2C LCD 16x2 Arduino Tutorial
 * More info http://www.ardumotive.com/i2clcd(en).html
 * Dev: Michalis Vasilakis Date: 19/11/2016 Ver. 1.0 */
/*
  MORSE CODE DECODING PORTION IS BASED OFF THIS CODE. NUMBERS WHERE ADDED TO THE ALPHABET.  
  PROGRAM TO DECIPHER MORSE CODE USING A PUSH BUTTON AND DISPLAY IT ON THE SERIAL MONITOR
  DATE: 20 JANUARY 2017
  AUTHORS: PINAKI SADHUKHAN AND PRIYANKA SADHUKHAN
*/
//Libraries 
#include <Wire.h>  
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address

unsigned long signal_len,t1,t2;   //time for which button is pressed
int inputPin = 2;                 //input pin for push button                  
String code = "";                 //string in which one alphabet is stored
const int keyPin = 2;     // the number of the straight key pin
int audio3 = 3;      // output on pin 3
int note = 587;      // oscillator note/pitch
int buttonState = 0;         // variable for reading if key is pressed 
int cycle = 1;
int lcdSlot = 1;
char LastLetter;
void setup() {
 lcd.begin(16,2);   
  lcd.backlight();   // Turn on the backligt  lcd.noBaklight() to turn it off
  lcd.setCursor(0,0); 
  lcd.print("MorseCodeDecoder");
  lcd.setCursor(2,1);
  lcd.print("*KM4NMP.COM*");
  
  Serial.begin(9600);
  pinMode(inputPin, INPUT);
  pinMode(ledPin,OUTPUT);
  lcd.setCursor(0,1);
  delay (3000);
  lcd.setCursor(0,1);
  lcd.print("               ");
  lcd.setCursor(0,1);
}

void loop()
{
  if (lcdSlot == 16)
  {
    lcd.clear();
    cycle = 1 + cycle;
    if ( cycle == 4){
      cycle = 1;
    }
    
    if (cycle == 1) {
      lcd.setCursor(2,0);
      lcd.print("KM4NMP.COM");
    }


    if (cycle == 2) {
        lcd.setCursor(0,0);
        lcd.print("MacarrLabs.com");
    }
    
    if (cycle == 3) {
        lcd.setCursor(0,0);
        lcd.print("MorseCodeDecoder");
    }
    
    lcd.setCursor(0,1);
    lcd.print ( LastLetter );
    lcdSlot = 0;
  }
NextDotDash:
  while (digitalRead(inputPin) == LOW) {}
  t1 = millis();                            //time at button press
  tone(audio3, note);
  while (digitalRead(inputPin) == HIGH) {}
  t2 = millis();                            //time at button release
  noTone(audio3);
  signal_len = t2 - t1;                     //time for which button is pressed
  if (signal_len > 50)                      //to account for switch debouncing
  {
    code += readio();                       //function to read dot or dash
  }
  while ((millis() - t2) < 500)           //if time between button press greater than 0.5sec, skip loop and go to next alphabet
  {     
    if (digitalRead(inputPin) == HIGH)
    {
      goto NextDotDash;
    }
  }
  convertor();                          //function to decipher code into alphabet
}

char readio()
{
  if (signal_len < 300 && signal_len > 50)
  {
    return '.';                        //if button press less than 0.3sec, it is a dot
  }
  else if (signal_len > 300)
  {
    return '-';                        //if button press more than 0.3sec, it is a dash
  }
}
Advertisements
void convertor()
{
  static String letters[] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-",
                             ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", "E"
                            };
  int i = 0;

  if (code == "..........")
    {
    lcd.clear();   
    lcd.setCursor(0,0);
    lcd.print("KM4NMP.com");
    lcd.setCursor(0,1);
    lcdSlot = 0; 
    }
  
  if (code == ".----")
    {
    Serial.print("1");        //1
    lcd.print("1");
    lcdSlot = lcdSlot + 1;
    LastLetter = 1; 
    }
    
    if (code == "..---")
    {
    Serial.print("2");        //2
    lcd.print("2");
    lcdSlot = lcdSlot + 1;
    LastLetter = 1;
    }

    if (code == "...--")
    {
    Serial.print("3");        //3
    lcd.print("3");
    lcdSlot = lcdSlot + 1;
    LastLetter = 3;
    }
 
  if (code == "....-")
    {
    Serial.print("4");
    lcd.print("4");           //4
    lcdSlot = lcdSlot + 1;
    LastLetter = 4;
    }
    
  if (code == ".....")
    {
    Serial.print("5");        //5
    lcd.print("5");
    lcdSlot = lcdSlot + 1;
    LastLetter = 5;
    }

  if (code == "-....")
    {
    Serial.print("6");        //6
    lcd.print("6");
    lcdSlot = lcdSlot + 1;
    LastLetter = 6;
    }

  if (code == "--...")
    {
    Serial.print("7");        //7
    lcd.print("7");
    lcdSlot = lcdSlot + 1;
    LastLetter = 7;
    }

  if (code == "---..")
    {
    Serial.print("8");        //8
    lcd.print("8");
    lcdSlot = lcdSlot + 1;
    LastLetter = 8;
    }

  if (code == "----.")
    {
    Serial.print("9");        //9
    lcd.print("9");
    lcdSlot = lcdSlot + 1;
    LastLetter = 9;
    }      

  if (code == "-----")
    {
    Serial.print("0");        //0
    lcd.print("0");
    lcdSlot = lcdSlot + 1;
    LastLetter = 0;
    }      
      
  if (code == ".-.-.-")
  {
    Serial.print(".");        //for break
    lcd.print(".");
    lcdSlot = lcdSlot + 1;
  }
  else
  {
    while (letters[i] != "E")  //loop for comparing input code with letters array
    {
      if (letters[i] == code)
      {
        Serial.print(char('A' + i));
        lcd.print(char('A' + i));
        lcdSlot = lcdSlot + 1;
        LastLetter = ('A' + i);
        break;
      }
      i++;
    }
    if (letters[i] == "E")
    {
      Serial.println("<Wrong input>");  //if input code doesn't match any letter, error
    }
  }
  code = "";                            //reset code to blank string
}

This is a great project for any DIY person. If you want to learn Morse code, this is a great way to practice. Stay tuned soon we will add a speed adjustment knob.

Arduino Oscillator and Decoder
Arduino Oscillator and Decoder
Advertisements

https://km4nmp.com/2019/03/31/arduino-morse-code-practice-oscillator-project/

https://www.instructables.com/id/Morse-Code-Decoder/

https://www.ardumotive.com/i2clcden.html

https://www.roboshala.com/arduino-lcd-full-programming-tutorial/

Latest Post

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this:
search previous next tag category expand menu location phone mail time cart zoom edit close