I’ve been working on a code for a while and it gives the following error when compiled:
morse_code.cpp: In function 'int Message(String)':
morse_code.cpp:21:35: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
^
morse_code.cpp:30:30: error: expected initializer before '<' token
else if(currentChar >= 'A' && currentChar <= 'Z') value = currentChar - 'A'; //use ascii values so that A = 0, B = 1 etc.
^
morse_code.cpp:30:30: error: expected ';' before '<' token
morse_code.cpp:30:30: error: expected primary-expression before '<' token
morse_code.cpp:105:1: warning: control reaches end of non-void function [-Wreturn-type]
delay(600); //delay between the two letters
^
make[1]: *** [../build/target/user/platform-6morse_code.o] Error 1
make: *** [user] Error 2
Error: Could not compile. Please review your code.
I do not understand how I’m supposed to fix the code and I neeed help. Basically what I’m trying to achieve with it is to flash a messageon an LED light, taken via phone, using morse code. Thank you in advance.
Here is the whole code:
const int led = D7; //pin for the led
int i = 0;
//String morse;
//int len2;
//int len; //the three variables will be explained later
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
}
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(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' + 26; //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 800 milliseconds because it's a space
}
delay(200);//delay between letters
}
}