// 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
Content Rating
Is this a good/useful/informative piece of content to include in the project? Have your say!
You must login before you can post a comment. .