
// Made by Go2 (thx Vladimir)

// ProgGo2v2.0

/*
  LiquidCrystal Library
  
 The LiquidCrystal library works with all LCD displays that are
 compatible with the Hitachi HD44780 driver. There are many of
 them out there, and you can usually tell them by the 16-pin interface.

  The circuit:
 * LCD RS pin to digital pin 12
 * LCD Enable pin to digital pin 11
 * LCD D4 pin to digital pin 5
 * LCD D5 pin to digital pin 4
 * LCD D6 pin to digital pin 3
 * LCD D7 pin to digital pin 2
 * LCD R/W pin to ground
 * LCD VSS pin to ground
 * LCD VCC pin to 5V
 * 10K resistor:
 * ends to +5V and ground
 * wiper to LCD VO pin (pin 3)

 Library originally added 18 Apr 2008
 by David A. Mellis
 library modified 5 Jul 2009
 by Limor Fried (http://www.ladyada.net)
  */

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(10, 11, 5, 4, 3, 2);

const int buttonPin = 8;
const int ledPin = 13;
const int buzzer = 9;

int buttonState = LOW;
int lastButtonState = LOW;

int pause_value = 100;  // depending on your skill and how fast your fingers are you can change this value to make typing a message faster or slower
long signal_length = 0;
long pause = 0;

String morse = "";
String dash = "-";
String dot = ".";

boolean cheker = false;
boolean linecheker = false;

String text = "";

void setup()
{
  Serial.begin(9600);

  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(buzzer, OUTPUT);
  
  Serial.println("Welcome to Arduino-Uno morse machine");
  Serial.println("Using these values print out your message in morse and read out the message in the serial monitor");
  Serial.println("https://bit.ly/2ZqrJQH morse code values for learning");

  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("Lumieres de nuit");

  while(!digitalRead(buttonPin))
  ;
  
}

void loop()
{
  buttonState = digitalRead(buttonPin);

  if (buttonState && lastButtonState)       // basic state machine depending on the state of the signal from the button
  {
    signal_length++;
    if (signal_length<2*pause_value)
    {
      tone(buzzer, 1500) ;
    }
    else
    {
      tone(buzzer, 1000) ;
    }
  }
  else if(!buttonState && lastButtonState)   // this part of the code happens when the button is released and it send either . or - into the buffer
  {
    if (signal_length>50 && signal_length<2*pause_value )
    {
      morse =  morse + dot;
    }
    else if (signal_length>2*pause_value)
    {
      morse = morse +  dash;
    }
    signal_length=0;
    digitalWrite(13, LOW);
    noTone(buzzer);
  }
  else if(buttonState && !lastButtonState)    // this part happens when the button is pressed and its use to reset several values
  {
    pause=0;
    digitalWrite(13, HIGH);
    cheker = true;
    linecheker = true;
  }
  else if (!buttonState && !lastButtonState)
  {
    pause++;
    if (( pause>15*pause_value ) && (cheker))
    {
      coding(morse);
      cheker = false;
      morse = "";
    }
    if ((pause>50*pause_value) && (linecheker))
    {
      Serial.println();
      text = "";
      linecheker = false;
    }
  }
  lastButtonState=buttonState;
  delay(1);
}

void coding(String pulse)
{                          //compare morse string to known morse values and print out the letter or a number
                           //the code is written based on the international morse code https://bit.ly/2ZqrJQH,
                           //one thing i changed is that insted of typing a special string to end the line it happens with enough delay
                           
  if (pulse==".-")            { Serial.print("A"); text = text + "A"; }
  else if (pulse=="-...")     { Serial.print("B"); text = text + "B"; }
  else if (pulse=="-.-.")     { Serial.print("C"); text = text + "C"; }
  else if (pulse=="-..")      { Serial.print("D"); text = text + "D"; }
  else if (pulse==".")        { Serial.print("E"); text = text + "E"; }
  else if (pulse=="..-.")     { Serial.print("F"); text = text + "F"; }
  else if (pulse=="--.")      { Serial.print("G"); text = text + "G"; }
  else if (pulse=="....")     { Serial.print("H"); text = text + "H"; }
  else if (pulse=="..")       { Serial.print("I"); text = text + "I"; }
  else if (pulse==".---")     { Serial.print("J"); text = text + "J"; }
  else if (pulse=="-.-")      { Serial.print("K"); text = text + "K"; }
  else if (pulse==".-..")     { Serial.print("L"); text = text + "L"; }
  else if (pulse=="--")       { Serial.print("M"); text = text + "M"; }
  else if (pulse=="-.")       { Serial.print("N"); text = text + "N"; }
  else if (pulse=="---")      { Serial.print("O"); text = text + "O"; }
  else if (pulse==".--.")     { Serial.print("P"); text = text + "P"; }
  else if (pulse=="--.-")     { Serial.print("Q"); text = text + "Q"; }
  else if (pulse==".-.")      { Serial.print("R"); text = text + "R"; }
  else if (pulse=="...")      { Serial.print("S"); text = text + "S"; }
  else if (pulse=="-")        { Serial.print("T"); text = text + "T"; }
  else if (pulse=="..-")      { Serial.print("U"); text = text + "U"; }
  else if (pulse=="...-")     { Serial.print("V"); text = text + "V"; }
  else if (pulse==".--")      { Serial.print("W"); text = text + "W"; }
  else if (pulse=="-..-")     { Serial.print("X"); text = text + "X"; }
  else if (pulse=="-.--")     { Serial.print("Y"); text = text + "Y"; }
  else if (pulse=="--..")     { Serial.print("Z"); text = text + "Z"; }
  else if (pulse==".----")    { Serial.print("1"); text = text + "1"; }
  else if (pulse=="..---")    { Serial.print("2"); text = text + "2"; }
  else if (pulse=="...--")    { Serial.print("3"); text = text + "3"; }
  else if (pulse=="....-")    { Serial.print("4"); text = text + "4"; }
  else if (pulse==".....")    { Serial.print("5"); text = text + "5"; }
  else if (pulse=="-....")    { Serial.print("6"); text = text + "6"; }
  else if (pulse=="--...")    { Serial.print("7"); text = text + "7"; }
  else if (pulse=="---..")    { Serial.print("8"); text = text + "8"; }
  else if (pulse=="----.")    { Serial.print("9"); text = text + "9"; }
  else if (pulse=="-----")    { Serial.print("0"); text = text + "0"; }
  else if (pulse==".-.-.-")   { Serial.print("."); text = text + "."; }
  else if (pulse=="--..--")   { Serial.print(","); text = text + ","; }
  else if (pulse=="..--..")   { Serial.print("?"); text = text + "?"; }
  else if (pulse==".----.")   { Serial.print("'"); text = text + "'"; }
  else if (pulse=="-.-.--")   { Serial.print("!"); text = text + "!"; }
  else if (pulse=="-..-.")    { Serial.print("/"); text = text + "/"; }
  else if (pulse=="-.--.")    { Serial.print("("); text = text + "("; }
  else if (pulse=="-.--.-")   { Serial.print(")"); text = text + ")"; }
  else if (pulse==".-...")    { Serial.print("&"); text = text + "&"; }
  else if (pulse=="---...")   { Serial.print(":"); text = text + ":"; }
  else if (pulse=="-.-.-.")   { Serial.print(";"); text = text + ";"; }
  else if (pulse=="-...-")    { Serial.print("="); text = text + "="; }
  else if (pulse==".-.-.")    { Serial.print("+"); text = text + "+"; }
  else if (pulse=="-....-")   { Serial.print("-"); text = text + "-"; }
  else if (pulse=="..--.-")   { Serial.print("_"); text = text + "_"; }
  else if (pulse==".-..-.")   { Serial.print("\""); text = text + "\""; }
  else if (pulse=="...-..-")  { Serial.print("$"); text = text + "$"; }
  else if (pulse==".--.-.")   { Serial.print("@"); text = text + "@"; }
  
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  lcd.print(text);
    
  Serial.print(" ");

  pulse=""; 
}
