int dot = 76; int dash = dot*3; int pause = dash; int wait = dot*7; int speaker = 4; int light = 13; int button = 2; bool lastBstate = false; unsigned long buttonStart = 0; unsigned long buttonEnd = 0; unsigned long holdButton = 0; String cache = ""; void setup() { // initialize digital pins as input/output. pinMode(light, OUTPUT); pinMode(speaker, OUTPUT); pinMode(button, INPUT); // Open serial communications and wait for port to open: Serial.begin(9600); while (!Serial) { ; // wait for serial port to connect. Needed for native USB port only } // send an intro: Serial.println("Send me text, and I will broadcast in Morse!"); Serial.println(); } void loop() { // get any incoming bytes: if (Serial.available() > 0) { char thisChar = toupper(Serial.read()); // say what was sent: Serial.print("Broadcasting: "); Serial.println(thisChar); switch (thisChar) { case 'A': tap(dot); tap(dash); delay(pause); break; // •- case 'B': tap(dash); tap(dash); tap(dash); tap(dot); delay(pause); break; // ---• case 'C': tap(dash); tap(dot); tap(dash); tap(dot); delay(pause); break; // -•-• case 'D': tap(dash); tap(dot); tap(dot); delay(pause); break; // -•• case 'E': tap(dot); delay(pause); break; // • case 'F': tap(dot); tap(dot); tap(dash); tap(dot); delay(pause); break; // ••-• case 'G': tap(dash); tap(dash); tap(dot); delay(pause); break; // --• case 'H': tap(dot); tap(dot); tap(dot); tap(dot); delay(pause); break; // •••• case 'I': tap(dot); tap(dot); delay(pause); break; // •• case 'J': tap(dot); tap(dash); tap(dash); tap(dash); delay(pause); break; // •--- case 'K': tap(dash); tap(dot); tap(dash); delay(pause); break; // -•- case 'L': tap(dot); tap(dash); tap(dot); tap(dot); delay(pause); break; // •-•• case 'M': tap(dash); tap(dash); delay(pause); break; // -- case 'N': tap(dash); tap(dot); delay(pause); break; // -• case 'O': tap(dash); tap(dash); tap(dash); delay(pause); break; // --- case 'P': tap(dot); tap(dash); tap(dash); tap(dot); delay(pause); break; // •--• case 'Q': tap(dash); tap(dash); tap(dot); tap(dash); delay(pause); break; // --•- case 'R': tap(dot); tap(dash); tap(dot); delay(pause); break; // •-• case 'S': tap(dot); tap(dot); tap(dot); delay(pause); break; // ••• case 'T': tap(dash); delay(pause); break; // - case 'U': tap(dot); tap(dot); tap(dash); delay(pause); break; // ••- case 'V': tap(dot); tap(dot); tap(dot); tap(dash); delay(pause); break; // •••- case 'W': tap(dot); tap(dash); tap(dash); delay(pause); break; // •-- case 'X': tap(dash); tap(dot); tap(dot); tap(dash); delay(pause); break; // -••- case 'Y': tap(dash); tap(dot); tap(dash); tap(dash); delay(pause); break; // -•-- case 'Z': tap(dash); tap(dash); tap(dot); tap(dot); delay(pause); break; // --•• case '0': tap(dash); tap(dash); tap(dash); tap(dash); tap(dash); delay(pause); break; // ----- case '1': tap(dot); tap(dash); tap(dash); tap(dash); tap(dash); delay(pause); break; // •---- case '2': tap(dot); tap(dot); tap(dash); tap(dash); tap(dash); delay(pause); break; // ••--- case '3': tap(dot); tap(dot); tap(dot); tap(dash); tap(dash); delay(pause); break; // •••-- case '4': tap(dot); tap(dot); tap(dot); tap(dot); tap(dash); delay(pause); break; // ••••- case '5': tap(dot); tap(dot); tap(dot); tap(dot); tap(dot); delay(pause); break; // ••••• case '6': tap(dash); tap(dot); tap(dot); tap(dot); tap(dot); delay(pause); break; // -•••• case '7': tap(dash); tap(dash); tap(dot); tap(dot); tap(dot); delay(pause); break; // --••• case '8': tap(dash); tap(dash); tap(dash); tap(dot); tap(dot); delay(pause); break; // ---•• case '9': tap(dash); tap(dash); tap(dash); tap(dash); tap(dot); delay(pause); break; // ----• case '/': tap(dash); tap(dot); tap(dot); tap(dash); tap(dot); delay(pause); break; // -••-• case '+': tap(dot); tap(dash); tap(dot); tap(dash); tap(dot); delay(pause); break; // •-•-• case '=': tap(dash); tap(dot); tap(dot); tap(dot); tap(dash); delay(pause); break; // -•••- case '&': tap(dot); tap(dash); tap(dot); tap(dot); tap(dot); delay(pause); break; // •-••• case '(': tap(dash); tap(dot); tap(dash); tap(dash); tap(dot); delay(pause); break; // -•--• case ')': tap(dash); tap(dot); tap(dash); tap(dash); tap(dot); tap(dash); delay(pause); break; // -•--•- case '!': tap(dash); tap(dot); tap(dash); tap(dot); tap(dash); tap(dash); delay(pause); break; // -•-•-- case '.': tap(dot); tap(dash); tap(dot); tap(dash); tap(dot); tap(dash); delay(pause); break; // •-•-•- case ',': tap(dash); tap(dash); tap(dot); tap(dot); tap(dash); tap(dash); delay(pause); break; // --••-- case ':': tap(dash); tap(dash); tap(dash); tap(dot); tap(dot); tap(dot); delay(pause); break; // ---••• case ';': tap(dash); tap(dot); tap(dash); tap(dot); tap(dash); tap(dot); delay(pause); break; // -•-•-• case '?': tap(dot); tap(dot); tap(dash); tap(dash); tap(dot); tap(dot); delay(pause); break; // ••--•• case '-': tap(dash); tap(dot); tap(dot); tap(dot); tap(dot); tap(dash); delay(pause); break; // -••••- case '"': tap(dot); tap(dash); tap(dot); tap(dot); tap(dash); tap(dot); delay(pause); break; // •-••-• case '@': tap(dot); tap(dash); tap(dash); tap(dot); tap(dash); tap(dot); delay(pause); break; // •--•-• default: delay(wait); break; } } else {// read the state of the pushbutton int buttonState = digitalRead(button); // check if pushbutton is pressed. if (buttonState == LOW){ // check if it's time for a new letter if((millis()-buttonEnd) >150) { if(cache != "") { Serial.print("Received: "); Serial.println(translate(cache)); empty(); lastBstate = true; } } if((millis() - buttonEnd) > 380) { if(lastBstate == true) { Serial.println(" "); lastBstate = false; } } } // if it is, the button state is HIGH else if (buttonState == HIGH) { buttonStart = millis(); digitalWrite(LED_BUILTIN, HIGH); tone(speaker, 3000, dot); updateState(); digitalWrite(LED_BUILTIN, LOW); } delay(10); // Delay a little bit to improve simulation performance } } void tap(int toneLength) { digitalWrite(light, HIGH); tone(speaker, 3000, toneLength); delay(toneLength); digitalWrite(light, LOW); delay(dot); } void updateState() { //if the button has been pressed while(digitalRead(button) == HIGH) { buttonEnd = millis(); } //after the button is released holdButton = buttonEnd - buttonStart; Serial.println(holdButton); if(holdButton < dash) { cache = cache + "•"; } else if (holdButton >= dash) { cache = cache + "-"; } } char translate(String morse) { if (morse == "•-") return 'A'; else if (morse == "-•••") return 'B'; else if (morse == "-•-•") return 'C'; else if (morse == "-••") return 'D'; else if (morse == "•") return 'E'; else if (morse == "••-•") return 'F'; else if (morse == "--•") return 'G'; else if (morse == "••••") return 'H'; else if (morse == "••") return 'I'; else if (morse == "•---") return 'J'; else if (morse == "-•-") return 'K'; else if (morse == "•-••") return 'L'; else if (morse == "--") return 'M'; else if (morse == "-•") return 'N'; else if (morse == "---") return 'O'; else if (morse == "•--•") return 'P'; else if (morse == "--•-") return 'Q'; else if (morse == "•-•") return 'R'; else if (morse == "•••") return 'S'; else if (morse == "-") return 'T'; else if (morse == "••-") return 'U'; else if (morse == "•••-") return 'V'; else if (morse == "•--") return 'W'; else if (morse == "-••-") return 'X'; else if (morse == "-•--") return 'Y'; else if (morse == "--••") return 'Z'; else if (morse == "-----") return '0'; else if (morse == "•----") return '1'; else if (morse == "••---") return '2'; else if (morse == "•••--") return '3'; else if (morse == "••••-") return '4'; else if (morse == "•••••") return '5'; else if (morse == "-••••") return '6'; else if (morse == "--•••") return '7'; else if (morse == "---••") return '8'; else if (morse == "----•") return '9'; else if (morse == "-••-•") return '/'; else if (morse == "•-•-•") return '+'; else if (morse == "-•••-") return '='; else if (morse == "•-•••") return '&'; else if (morse == "-•--•") return '('; else if (morse == "-•--•-") return ')'; else if (morse == "-•-•--") return '!'; else if (morse == "•-•-•-") return '.'; else if (morse == "--••--") return ','; else if (morse == "---•••") return ':'; else if (morse == "-•-•-•") return ';'; else if (morse == "••--••") return '?'; else if (morse == "-••••-") return '-'; else if (morse == "•-••-•") return '"'; else if (morse == "•--•-•") return '@'; else return '*'; // unknown pattern } void empty() { cache = ""; }