lmaono
Published

Morse code display

A system where you can type in messages using morse code.

BeginnerShowcase (no instructions)2
Morse code display

Things used in this project

Hardware components

Alphanumeric LCD, 16 x 2
Alphanumeric LCD, 16 x 2
×1
Arduino Mega 2560 & Genuino Mega 2560
Arduino Mega 2560 & Genuino Mega 2560
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Buzzer
Buzzer
×1
Resistor 10k ohm
Resistor 10k ohm
×1
Switch Actuator, Head for spring return push-button
Switch Actuator, Head for spring return push-button
×1
Rotary potentiometer (generic)
Rotary potentiometer (generic)
×1

Story

Read more

Schematics

morse_bb_kDJ2RCc5U5.png

Code

morse.ino

C/C++
This controls all the logic that is needed to get the letters from the beeps. Most of it is actually dedicated to getting when the dots and dashes are gotten, not turning them into letters
#include <LiquidCrystal.h>
#include <Thread.h>

const int ALPHABET_SIZE = 26;

const char alpha[ALPHABET_SIZE] = {'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'}; 
const String morse[81] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};

const int TIME_UNIT = 70;
const int dot = TIME_UNIT;
const int dash = TIME_UNIT * 3;
const int symbol_space = TIME_UNIT; 
const int letter_space = TIME_UNIT * 3;
const int word_space = TIME_UNIT * 7;

const String reset = "......";

const int MAX_LENGTH = 4;

String set_text = "";
String morse_text = "";
String new_morse = "";
String display_text = "";

const int COLUMNS = 16, ROWS = 2;

bool scroll = true;

unsigned long counter = 0;
int last_value = 0;

// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 23, en = 22, d4 = 39, d5 = 41, d6 = 38, d7 = 40;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(COLUMNS, ROWS);
  lcd.clear();

  Serial.begin(9600);
  /*
  Thread myThread(scroll_until_not, "Welcome, morse code user! To begin, start pressing the button. To clear your text, enter 10 dots");
  myThread.run();
  */

  for (int i = 0; i < ALPHABET_SIZE; i ++){
    Serial.println(String(alpha[i]) + ": " + morse[i]);
  }
}

bool can_enter = false;

void loop() {
  int value = digitalRead(48);

  bool new_result = false;
  if (last_value != value){
    new_result = rel(value);
    // Serial.println(counter);
    counter = 0;
  }
  else{
    new_result = hold(value);
  }

  counter ++;

  if (new_result){
    scroll = false;
    // // Serial.println("Stopped scrolling");
    display_text = set_text;
    
    lcd.clear();
    int l = (display_text + morse_text + new_morse).length();
  
    if (l > 16){
      String new_text = "";
      for (int i = l - COLUMNS; i < display_text.length(); i ++){
        new_text += display_text[i];
      }
      display_text = new_text;
    }
    lcd.print(display_text + morse_text + new_morse);
    lcd.setCursor(0, 1);
    lcd.print("");
    lcd.setCursor(0, 0);
  }
  
  last_value = value;

  delay(1);
}

String get_letter(){
  String letter = "";
  
  for (int i = 0; i < ALPHABET_SIZE; i ++){
    if (morse_text == morse[i]){
      letter = alpha[i];
    }
  }

  return letter;
}

bool hold(int v){
  String past_morse = new_morse, past_morse_text = morse_text, past_set_text = set_text;
  if (v == 1){
    if (counter <= dot){
      new_morse = '.';
    }
    else if (counter >= dash){
      new_morse = '-';  
    }
  }
  else {
    if (counter >= letter_space){
      String l = get_letter();
   
      if (l != ""){
        set_text += l;

      }
      if (morse_text == reset){
        clear_text();
      }

      morse_text = "";
    }
  }
  return past_morse != new_morse || past_morse_text != morse_text || past_set_text != set_text;
}

bool rel(int v){
  String pm = morse_text, pt = set_text;
  if (v == 0){
    morse_text += new_morse;
    new_morse = "";
    
  }
  else {
    if (counter >= word_space){
      set_text += " ";
    }
  }
  return pm != morse_text || pt != set_text;  
}

void clear_text(){
  set_text = "";
}

/*
void scroll_until_not(String text){
  // Serial.println("entered");
  // Serial.println(text);
  while (scroll){
    // Serial.println("scrolling1");
    String temp_text = text;
    if (text.length() > 16){
      lcd.clear();
      lcd.print(text);
      delay(1000);
      for (int i = 1; i < text.length() - 15; i ++){
        // Serial.println("scrolling2");
        if (!scroll){
          break;
        }
        
        lcd.clear();
        
        temp_text = substr(text, i, text.length());
        
        lcd.print(temp_text);
        
        delay(300);
      }
    }
    else {
      // Serial.println("scrolling3");
      lcd.clear();
      lcd.print(text);
    }
    if (!scroll){
      break;
    }
    delay(1000);
  }
}

String substr(String t, int s, int l){
  String b = "";
  for (int i = s; i < s + l; i ++){
    if (i >= t.length()){
      break;
    }
    b += t[i];
  }
  return b;
}
*/

Credits

lmaono

lmaono

0 projects • 0 followers

Comments