Vail Cypher LED

Made by Leslie Liu

To complete a basic circuit and experiment with controlling LEDs remotely via cloud interface.

Created: October 31st, 2024

0

Intention

Upon completion of the exercises, I sought to extend the communicative capacity of the WiFi-connected LED by taking in text input to display on the bulbs.

0

Process

Continuing with the circuit completed at the close of Exercise 3, I set about watching a couple videos about the Vail Cypher (aka Morse Code), creating a map of alphabet - dot/dash values with which to encrypt text input.

Much of the process was debugging code, which involved somewhat haphazard visits to GeeksforGeeks.org, StackOverflow, the C++ reference website (still need to understand/clarify how the underlying Particle programming language works and which languages it's similar to), and — towards the very end — consulting ChatGPT about the difference between different types of strings. (All code self-written, though.) One thing I wish I grasped more fully was the fact that characters in the language exclusively use single quotes, and strings double quotes...

After my code was in working order (though I am unsure how efficient/generous it is on the Photon 2), I tweaked the delays to more accurately reflect the cypher in use, adding a bookend() function at the start and end of each transmission of textual input to involve the second LED more.

0
// Skills Dev 1: A Simple Internet Appliance (LED). LL for DIOT 2024.

#include "Particle.h"
#include <iostream>
#include <string>
#include <map>
using namespace std;

SYSTEM_MODE(AUTOMATIC);
SerialLogHandler logHandler(LOG_LEVEL_INFO);

int ledPin1 = D2;
int ledPin2 = D0;
int dot = 100;
int dash = dot * 3;
int intra = dot * 3;
int inter = dot * 7;

void bookend() {
    digitalWrite(ledPin2, HIGH);
    delay(intra);
    digitalWrite(ledPin2, LOW);
    // delay(intra);
}

int ledControl(String msg) {
    // convert Particle String to std::string
    std::string stdMsg = msg.c_str();

    // morse code map
    std::map<string, string> umap = {
        {"A", ".-"}, {"B", "-..."}, {"C", "-.-."}, {"D", "-.."}, {"E", "."},
        {"F", "..-."}, {"G", "--."}, {"H", "...."}, {"I", ".."}, {"J", ".---"},
        {"K", "-.-"}, {"L", ".-.."}, {"M", "--"}, {"N", "-."}, {"O", "---"},
        {"P", ".--."}, {"Q", "--.-"}, {"R", ".-."}, {"S", "..."}, {"T", "-"},
        {"U", "..-"}, {"V", "...-"}, {"W", ".--"}, {"X", "-..-"}, {"Y", "-.--"},
        {"Z", "--.."}
    };

    // capitalize string
    transform(stdMsg.begin(), stdMsg.end(), stdMsg.begin(), ::toupper);

    bool traveled = false; 

    // start message
    bookend();

    // loop through each character in the message
    for (char& c : stdMsg) {
        Serial.print(c);
        
        string currChar(1, c);

        if (umap.find(currChar) != umap.end()) {  // current character is in map and can be represented via light
            string morsed = umap.at(currChar);
            for (char& morseChar : morsed) {
                if (morseChar == '.') {  // dot
                    digitalWrite(ledPin1, HIGH);
                    delay(dot);
                    digitalWrite(ledPin1, LOW);
                    delay(dot);
                } else {  // dash
                    digitalWrite(ledPin1, HIGH);
                    delay(dash);
                    digitalWrite(ledPin1, LOW);
                    delay(dot);
                }
            }
        } else {
            if (currChar.compare(" ")==0) {  // space
                delay(inter);
                // delay(dot);
                traveled = true;
            }
        }
        delay(intra);
    }
    
    // end message
    bookend();
    
    if (traveled) return 1;
    else return -1;
}

void setup() {
    Particle.function("led", ledControl);
    pinMode(ledPin1, OUTPUT);  // configure the pins to be outputs
    pinMode(ledPin2, OUTPUT);
    digitalWrite(ledPin1, LOW);  // initialize both the LEDs to be OFF
    digitalWrite(ledPin2, LOW);
}

void loop() {
}
Click to Expand
0

Outcome

My final circuit includes 2 LEDs modulated by 1KΩ resistors, connected via jumper wires to the power rail.

0

Overhead view of circuit.

0
DIOT F24 SD1 (1)
Leslie Liu - https://www.youtube.com/shorts/Jz_0flEqqs8
0

Input from cloud: "SOS".

0
DIOT F24 SD1 (2)
Leslie Liu - https://www.youtube.com/shorts/ILLgnmltHrc
0

Input from cloud: "hello world".

0
DIOT F24 SD1 (3)
Leslie Liu - https://www.youtube.com/shorts/7bx_h4j-2g8
0

Input from cloud: "DIOT".

0

Reflection

As mentioned in my notes on the process, in future projects I'd like to clarify the basics of the programming language before fully diving in and making substantial changes — there was quite a bit of back and forth as I wasn't familiar with the syntax, though these trials were immensely helpful for me as I was motivated to push through.

Another consideration I entertained was incorporating paper to adorn the LEDs, perhaps as small flower petals, for instance. Some adjacent questions to explore, then, include circuit construction best practices and basic safety awareness.

x
Share this Project


Focused on
Skills
About

To complete a basic circuit and experiment with controlling LEDs remotely via cloud interface.