// sourced from https://www.deviceplus.com/arduino/how-to-create-a-morse-code-generator-using-arduino/

/* python code to generate the phrase constants
import re
while True:
    a = input("Phrase:")
    a = re.sub(r"[^\w\d\s]","",a)
    a = list(a)
    a.insert(0, len(a))
    print(a)
*/
#define BUZZER_PIN 8 // Assign BUZZER_PIN to pin 8

#define TIME_UNIT 60 // 60 = 20wpm, per https://morsecode.world/international/timing.html

const char PHRASE_1[] PROGMEM = {8, 'y', 'o', 'u', 'r', ' ', 'm', 'u', 'm'};
const char PHRASE_2[] PROGMEM = {15, 'd', 'u', 's', 't', 'y', ' ', 'o', 'l', 'd', ' ', 'b', 'o', 'n', 'e', 's'};
const char PHRASE_3[] PROGMEM = {16, 'a', 'l', 'w', 'a', 'y', 's', ' ', 'l', 'i', 's', 't', 'e', 'n', 'i', 'n', 'g'};
const char PHRASE_4[] PROGMEM = {15, 'a', 'l', 'w', 'a', 'y', 's', ' ', 'w', 'a', 't', 'c', 'h', 'i', 'n', 'g'};
const char PHRASE_5[] PROGMEM = {12, 'c', 'o', 'm', 'e', ' ', 'f', 'i', 'n', 'd', ' ', 'm', 'e'};
const char PHRASE_6[] PROGMEM = {10, 'w', 'h', 'e', 'r', 'e', ' ', 'a', 'm', ' ', 'i'};
const char PHRASE_7[] PROGMEM = {9, 'i', ' ', 's', 'e', 'e', ' ', 'y', 'o', 'u'};

#define NUMBER_OF_PHRASES 7
const int PHRASES[] PROGMEM = {PHRASE_1,PHRASE_2,PHRASE_3,PHRASE_4,PHRASE_5,PHRASE_6,PHRASE_7};


void setup () {
  pinMode(BUZZER_PIN, OUTPUT);
}


void loop () {
  int phrase = pgm_read_byte_near(PHRASES+random(NUMBER_OF_PHRASES));
  for(byte i=1; i<pgm_read_byte_near(phrase); i++){
    SendChar(pgm_read_byte_near(phrase+i));
  }
  delay(300000);
}

void SendChar(char input){
if (input == 'a' || input == 'A') {lA();}//if the input is a or A go to function lA
if (input == 'b' || input == 'B') {lB();}//same but with b letter
if (input == 'c' || input == 'C') {lC();}
if (input == 'd' || input == 'D') {lD();}
if (input == 'e' || input == 'E') {lE();}
if (input == 'f' || input == 'F') {lF();}
if (input == 'g' || input == 'G') {lG();}
if (input == 'h' || input == 'H') {lH();}
if (input == 'i' || input == 'I') {lI();}
if (input == 'j' || input == 'J') {lJ();}
if (input == 'k' || input == 'K') {lK();}
if (input == 'l' || input == 'L') {lL();}
if (input == 'm' || input == 'M') {lM();}
if (input == 'n' || input == 'N') {lN();}
if (input == 'o' || input == 'O') {lO();}
if (input == 'p' || input == 'P') {lP();}
if (input == 'q' || input == 'Q') {lQ();}
if (input == 'r' || input == 'R') {lR();}
if (input == 's' || input == 'S') {lS();}
if (input == 't' || input == 'T') {lT();}
if (input == 'u' || input == 'U') {lU();}
if (input == 'v' || input == 'V') {lV();}
if (input == 'w' || input == 'W') {lW();}
if (input == 'x' || input == 'X') {lX();}
if (input == 'y' || input == 'Y') {lY();}
if (input == 'z' || input == 'Z') {lZ();}
if (input == '0') {l0();}
if (input == '1') {l1();}
if (input == '2') {l2();}
if (input == '3') {l3();}
if (input == '4') {l4();}
if (input == '5') {l5();}
if (input == '6') {l6();}
if (input == '7') {l7();}
if (input == '8') {l8();}
if (input == '9') {l9();}
if (input == ' ') {wordPause();}
}




//Sound functions
void lA () {dit();dah();letterPause();}//letter A in morse code!
void lB () {dah();dit();dit();dit();letterPause();}//same for B
void lC () {dah();dit();dah();dit();letterPause();}
void lD () {dah();dit();dit();letterPause();}
void lE () {dit();letterPause();}
void lF () {dit();dit();dah();dit();letterPause();}
void lG () {dah();dah();dit();letterPause();}
void lH () {dit();dit();dit();dit();letterPause();}
void lI () {dit();dit();letterPause();}
void lJ () {dit();dah();dah();dah();letterPause();}
void lK () {dah();dit();dah();letterPause();}
void lL () {dit();dah();dit();dit();letterPause();}
void lM () {dah();dah();letterPause();}
void lN () {dah();dit();letterPause();}
void lO () {dah();dah();dah();letterPause();}
void lP () {dit();dah();dah();dit();letterPause();}
void lQ () {dah();dah();dit();dah();letterPause();}
void lR () {dit();dah();dit();letterPause();}
void lS () {dit();dit();dit();letterPause();}
void lT () {dah();letterPause();}
void lU () {dit();dit();dah();letterPause();}
void lV () {dit();dit();dit();dah();letterPause();}
void lW () {dit();dah();dah();letterPause();}
void lX () {dah();dit();dit();dah();letterPause();}
void lY () {dah();dit();dah();dah();letterPause();}
void lZ () {dah();dah();dit();dit();letterPause();}
void l1 () {dit();dah();dah();dah();dah();letterPause();}
void l2 () {dit();dit();dah();dah();dah();letterPause();}
void l3 () {dit();dit();dit();dah();dah();letterPause();}
void l4 () {dit();dit();dit();dit();dah();letterPause();}
void l5 () {dit();dit();dit();dit();dit();letterPause();}
void l6 () {dah();dit();dit();dit();dit();letterPause();}
void l7 () {dah();dah();dit();dit();dit();letterPause();}
void l8 () {dah();dah();dah();dit();dit();letterPause();}
void l9 () {dah();dah();dah();dah();dit();letterPause();}
void l0 () {dah();dah();dah();dah();dah();letterPause();}

void dit(){
  digitalWrite(BUZZER_PIN,HIGH);
  delay(TIME_UNIT);
  digitalWrite(BUZZER_PIN,LOW);
  delay(TIME_UNIT);
}

void dah(){
  digitalWrite(BUZZER_PIN,HIGH);
  delay(TIME_UNIT * 3);
  digitalWrite(BUZZER_PIN,LOW);
  delay(TIME_UNIT);
}

void letterPause(){
  delay(TIME_UNIT * 3);
}

void wordPause(){
  delay (TIME_UNIT * 7);
}
