
int dotLen = 50;     // length of the morse code 'dot'
int dashLen = dotLen * 3;    // length of the morse code 'dash'
int elemPause = dotLen;  // length of the pause between elements of a character
int Space() = dotLen * 3;     // length of the Space s between characters
int wordPause = dotLen * 7;  // length of the pause between words
//Serial.readString()  
char b;
string a; 
char buf[512]; 
int Len;

void setup() 
{  
  Serial.begin(9600); // opens serial port, sets data rate To 9600 bps 
  pinMode(13,OUTPUT);
  pinMode(12,OUTPUT);  
}  

void loop() 
{  
  
  while (Serial.available())
    {  
     a = Serial.readString();// read the incoming data As String 
     Len = a.length();
     a.toCharArray(buf, Len);
      
     for (int j = 0; j <= Len+1; j++)
        {
           encode(buf[j]);
           Serial.print(buf[j]);
        }
       
        Serial.println();
    }
} 

 
// *** Characters to Morse Code Conversion *** //
void encode(char tmpChar)
{
  // Take the passed character And use a switch case to find the morse code for that character
  switch(tmpChar)
  {
    case 'a': 
    MorseDot();
    MorseDash();
    delay(Space);
    break;
    
    case 'b':
    MorseDash();
    MorseDot();
    MorseDot();
    MorseDot();
    delay(Space);
    break;
    
    case 'c':
    MorseDash();
    MorseDot();
    MorseDash();
    MorseDot();
     delay(Space);
    break;
    
    case 'd':
    MorseDash();   
    MorseDash();     
    MorseDot();
     delay(Space); 
    break;
    
    case 'e':
    MorseDot();
    delay(Space); 
    break;
    
    case 'f':
    MorseDot();     
    MorseDot();     
    MorseDash();     
    MorseDot();
    delay(Space);
    break;
    
    case 'g':
    MorseDash();
    MorseDash();     
    MorseDot();
    delay(Space);
    break;
    
    case 'h':
    MorseDot();     
    MorseDot();     
    MorseDot();    
    MorseDot();
    delay(Space);
    break;
    
    case 'i':
    MorseDot();     
    MorseDot();
    delay(Space); 
    break;
    
    case 'j':
    MorseDot();     
    MorseDash();     
    MorseDash();     
    MorseDash();
    delay(Space); 
    break;
    
      case 'k':
    MorseDash();     
    MorseDot();     
    MorseDash();
    delay(Space); 
    break;
    
    case 'l':
    MorseDot();     
    MorseDash();     
    MorseDot();     
    MorseDot();
    delay(Space); 
    break;
    
    case 'm':
    MorseDash();     
    MorseDash();
    delay(Space); 
    break;
    
    case 'n':
    MorseDash();     
    MorseDot();
    delay(Space);
    break;
    
    case 'o':
    MorseDash();     
    MorseDash();     
    MorseDash();
    delay(Space);
    break;
    
    case 'p':
    MorseDot();    
    MorseDash();     
    MorseDash();     
    MorseDot();
    delay(Space);
    break;
    
    case 'q':
    MorseDash();    
    MorseDash();     
    MorseDot();     
    MorseDash();
    delay(Space);
    break;
    
    case 'r':
    MorseDot();     
    MorseDash();
    MorseDot();
    delay(Space);
    break;
    
    case 's':
    MorseDot();    
    MorseDot();     
    MorseDot();
    delay(Space); 
    break;
    
    case 't':
    MorseDash();
    delay(Space);     
    break;
    
    case 'u':
    MorseDot();     
    MorseDot();     
    MorseDash();
    delay(Space); 
    break;
    
    case 'v':
    MorseDot();     
    MorseDot();
    MorseDot();
    MorseDash();
    delay(Space);
    break;
    
    case 'w':
    MorseDot();    
    MorseDash();    
    MorseDash();
    delay(Space);
    break;
    
    case 'x':
    MorseDash();     
    MorseDot();     
    MorseDot();     
    MorseDash();
    delay(Space);
    break;
    
    case 'y':
    MorseDash();    
    MorseDot();     
    MorseDash();     
    MorseDash();
    delay(Space);
    break;
    
    case 'z':
    MorseDash();     
    MorseDash();     
    MorseDot();    
    MorseDot();
    delay(Space); 
    break;
    
    case ' ':
    Morse_word(); 
    delay(Space);    
    break;
    
    case '0':
    MorseDash();    
    MorseDash();     
    MorseDash();    
    MorseDash();    
    MorseDash();
    delay(Space); 
    break;
    
    case '1':
    MorseDot();     
    MorseDash();     
    MorseDash();     
    MorseDash();     
    MorseDash();
    delay(Space); 
    break;
    
    case '2':
    MorseDot();     
    MorseDot();     
    MorseDash();     
    MorseDash();    
    MorseDash();
    delay(Space);
    break;
    
    case '3':
    MorseDot();     
    MorseDot();     
    MorseDot();     
    MorseDash();    
    MorseDash();
    delay(Space);
    break;
    
    case '4':
    MorseDot();     
    MorseDot();     
    MorseDot();    
    MorseDot();    
    MorseDash();
    delay(Space);  
    break;
    
    case '5':
    MorseDot();     
    MorseDot();     
    MorseDot();    
    MorseDot();   
    MorseDot();
    delay(Space); 
    break;
    
    case '6':
    MorseDash();     
    MorseDot();
    MorseDot();
    MorseDot();
    MorseDot();
    delay(Space );
    break;
    
    case '7':
    MorseDash();    
    MorseDash();     
    MorseDot();     
    MorseDot();     
    MorseDot();
    delay(Space );
    break;
    
    case '8':
    MorseDash();     
    MorseDash();     
    MorseDash();     
    MorseDot();    
    MorseDot();
    delay(Space ); 
    break;
    
    case '9':
    MorseDash();    
    MorseDash();    
    MorseDash();    
    MorseDash();   
    MorseDot();
    delay(Space );
    break;
          
  }
}

// DOT
 
void MorseDot()
{
  digitalWrite(13, HIGH);    // LED on 
  digitalWrite(12, LOW);     // cw key low  
  delay(dotLen);             // hold in this position
  digitalWrite(13, LOW);     // LED off 
  digitalWrite(12, HIGH);    // cw key high
  delay(elemPause);
}

// DASH
void MorseDash()
{
  digitalWrite(13, HIGH);    // LED on 
  digitalWrite(12, LOW);     // cw key low
  delay(dashLen);            // hold in this position
  digitalWrite(13, LOW);     // led off
  digitalWrite(12, HIGH);    // cw key high
  delay(elemPause);   
}

void Morse_word()
{ 
  digitalWrite(13, LOW);     // led off
  digitalWrite(12, HIGH);    // cw key high
  delay(wordPause);          // hold in this position
  digitalWrite(13, LOW);     // led off
  digitalWrite(12, HIGH);    // cw key high
}