I asked for help on my code earlier due to an error. Now that the error is gone, the code doesn’t do what I want it to do.
My aim is to make the built in LED on pin D7 flash a message, which is recieved via a Particle.function, in morse code. When I try to call the function with any string, it returns “Error”.
Could anyone please help me out with working this out? Thanks a lot in advance.
I added highly detailed comments in my code to make it as easily understandable as possible.
Here is my code:
const int led = D7; //pin for the led
int value;
String letters[] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", "-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----."};
//morse code combinations for all english letters and numbers (0-9)
void setup() {
pinMode(led, OUTPUT); //set led as output
Particle.function("send message", Message);//create a particle.function called message and tell the photon to run the function sendMessage when the particle.function message is called
digitalWrite(led, LOW);
}
void loop() {
}
int Message(String message) { //this is the function that will be used in the particle.function message. the string message will be taken from the user when the particle.function is called
for(int i = 0; i < message.length(); i++){ //do this as many times as the length of the message
char currentChar = message.charAt(i); //create a char storing the ith char of the message
if(currentChar >= 'a' && currentChar <= 'z') {
value = currentChar - 'a';//use ascii values so that a = 0, b = 1 etc.
}
else if(currentChar >= 'A' && currentChar <= 'Z') {
value = currentChar - 'A';//use ascii values so that A = 0, B = 1 etc.
}
else if(currentChar >= '0' && currentChar <= '9') {
value = currentChar - '0' + 27;//use ascii values so that 0 = 27, 1 = 28 etc. Those are where the numbers are in the array letters
}
else if(currentChar == ' ') {
value = 40;//if it's a space make the value something lardger that 36. We don't have a morse combination for it.
}
if(value < 40) { //if the value is smaller than 40, the value for space, it's for a number or a letter. so flash it.
String morse = letters[value]; //create a string storing your morse combination for the letter
for(int a = 0; a < morse.length(); a++){ //do this as many times as the morse combination's length
if(morse.charAt(a) == '.'){ //if the combination starts with a "." flash a "."
//return 1; //return one meaning you will flash a dot
digitalWrite(led, HIGH); //send a pulse of light lasting 200 milliseconds meaning a dot
delay(200);
digitalWrite(led, LOW);
delay(200); //delay for 200 milliseconds in between the dots/dashes
} else { //if not a dot, a dash. so flash it
//return 2; //return two meaning you will flash a dash
digitalWrite(led, HIGH); //send a pulse of light lasting 600 milliseconds meaning a dash
delay(600);
digitalWrite(led, LOW);
delay(200);
}
}
} else {
//return 0; //return a zero meaning there is a space
delay(400); //delay for 400 milliseconds because it's a space
}
delay(200);//delay between letters
}
}