Arnov Sharma
Published © MIT

Morse Master

Morse Master is a Morse Code translator device in which we can input Morse code using a web application or manually via a push button.

BeginnerFull instructions provided1 hour195
Morse Master

Things used in this project

Hardware components

Raspberry Pi Pico W
Raspberry Pi Pico W
×1
PCBWay Custom PCB
PCBWay Custom PCB
×1

Software apps and online services

Fusion
Autodesk Fusion
Arduino IDE
Arduino IDE

Hand tools and fabrication machines

3D Printer (generic)
3D Printer (generic)

Story

Read more

Custom parts and enclosures

STEP FILE

Schematics

SCH

Code

code

C/C++
#include <WiFi.h>
#include <WebServer.h>

// Replace with your Wi-Fi credentials
const char* ssid = "UR SSID";
const char* password = "UR PASS";

// Set up GPIO pins
const int LED_PINS[] = {0, 15, 16, 17}; // Array of GPIO pins for LEDs
const int BUTTON_PIN = 1;              // Push button for manual input

// Morse code timings
const int dotDuration = 200;    // Duration of a dot in milliseconds
const int dashDuration = 600;   // Duration of a dash in milliseconds
const int pauseDuration = 200;  // Pause between symbols
const int wordPause = 800;      // Pause between words
const int buttonThreshold = 500; // Threshold for differentiating dot/dash

// Web server instance
WebServer server(80);
String morseMessage = "";

// Morse code dictionary (simplified)
String translateToMorse(char c) {
  switch (toupper(c)) {
    case 'A': return ".-";
    case 'B': return "-...";
    case 'C': return "-.-.";
    case 'D': return "-..";
    case 'E': return ".";
    case 'F': return "..-.";
    case 'G': return "--.";
    case 'H': return "....";
    case 'I': return "..";
    case 'J': return ".---";
    case 'K': return "-.-";
    case 'L': return ".-..";
    case 'M': return "--";
    case 'N': return "-.";
    case 'O': return "---";
    case 'P': return ".--.";
    case 'Q': return "--.-";
    case 'R': return ".-.";
    case 'S': return "...";
    case 'T': return "-";
    case 'U': return "..-";
    case 'V': return "...-";
    case 'W': return ".--";
    case 'X': return "-..-";
    case 'Y': return "-.--";
    case 'Z': return "--..";
    case ' ': return " ";
    default: return ""; // Ignore unsupported characters
  }
}

// Function to initialize LEDs
void initializeLEDs() {
  for (int i = 0; i < 4; i++) {
    pinMode(LED_PINS[i], OUTPUT);
    digitalWrite(LED_PINS[i], HIGH); // Ensure LEDs are ON initially
  }
}

// Function to set the state of all LEDs
void setLEDState(bool state) {
  for (int i = 0; i < 4; i++) {
    digitalWrite(LED_PINS[i], state ? HIGH : LOW); // Set LEDs ON or OFF
  }
}

// Function to blink LEDs for Morse code
void blinkMorse(String morseCode) {
  for (int i = 0; i < morseCode.length(); i++) {
    char c = morseCode[i];
    if (c == '.') {
      setLEDState(false); // Turn LEDs OFF
      delay(dotDuration);
      setLEDState(true); // Turn LEDs ON
      delay(pauseDuration);
    } else if (c == '-') {
      setLEDState(false); // Turn LEDs OFF
      delay(dashDuration);
      setLEDState(true); // Turn LEDs ON
      delay(pauseDuration);
    } else if (c == ' ') {
      delay(wordPause);
    }
  }
}

// Updated Function to handle button presses
void manualMorseInput() {
  static unsigned long pressStartTime = 0;
  static bool buttonPressed = false;

  if (digitalRead(BUTTON_PIN) == LOW) { // Button is pressed
    if (!buttonPressed) { // Button was not already pressed
      pressStartTime = millis();
      buttonPressed = true;
    }
    setLEDState(false); // Keep LEDs off while the button is pressed
  } else { // Button is released
    if (buttonPressed) { // Only process if button was previously pressed
      unsigned long pressDuration = millis() - pressStartTime;
      if (pressDuration < buttonThreshold) {
        blinkMorse("."); // Short press = dot
      } else {
        blinkMorse("-"); // Long press = dash
      }
      buttonPressed = false;
    }
    setLEDState(true); // Turn LEDs back on when button is not pressed
  }
}

// Convert text to Morse and blink it
void displayMorseCode(String text) {
  for (int i = 0; i < text.length(); i++) {
    String morseCode = translateToMorse(text[i]);
    blinkMorse(morseCode);
    delay(pauseDuration); // Pause between letters
  }
}

// Handle root page
void handleRoot() {
  server.send(200, "text/html",
              "<!DOCTYPE html>"
              "<html>"
              // (HTML content omitted for brevity)
              "</html>");
}

// Handle message submission
void handleSend() {
  if (server.hasArg("message")) {
    morseMessage = server.arg("message");
    server.send(200, "text/plain", "Message received: " + morseMessage);
    displayMorseCode(morseMessage);
  } else {
    server.send(400, "text/plain", "Invalid Request");
  }
}

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

  initializeLEDs(); // Initialize multi-LED setup
  pinMode(BUTTON_PIN, INPUT_PULLUP); // Configure the button as input

  // Connect to Wi-Fi
  WiFi.begin(ssid, password);
  Serial.println("Connecting to Wi-Fi...");
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }
  Serial.println("\nConnected to Wi-Fi!");
  Serial.print("IP Address: ");
  Serial.println(WiFi.localIP());

  // Start the web server
  server.on("/", handleRoot);
  server.on("/send", handleSend);
  server.begin();
  Serial.println("Web server started!");
}

void loop() {
  server.handleClient(); // Handle web server requests
  manualMorseInput();    // Monitor button presses for Morse input
}

Credits

Arnov Sharma
338 projects • 344 followers
Just your average MAKER
Contact

Comments