[SOLVED] Code doesn't work

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
    }
}

I think that the “message” function is taking too long. You need to copy the String from the message function into a global String var and then run all the code you run in message today up in loop instead.

1 Like

It still doesn’t work. Here’s what I did:

    const int led = D7; //pin for the led
    int value;
    String message;
    
    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() {
        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
        }
        
        delay(1000); //delay a second between the two times which the message is sent
        
    }
    
    int Message(String x) { //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
        message = x;
        return 1;
    }

@EK51 in your Message() function you should set a flag indicating a new message is available. In loop(), when the flag is set, reset the flag and do your parsing. As is stands, you are constantly running the parsing code whether a new message is available or not!

You can also add Serial.print() statements in the parsing code to see that it is working as expected. :wink:

1 Like

I tried adding the flag. Still returns an error. Thanks anyways :slight_smile:

Here’s the new code:

    const int led = D7; //pin for the led
    int value;
    String message;
    
    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() {
        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
        }
        
        delay(1000); //delay a second between the two times which the message is sent
        
    }
    
    int Message(String x) { //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
        message = x;
        return 1;
    }

@EK51, where is the flag? I am thinking more along these lines:

const int led = D7; //pin for the led
int value;
String message;
bool newMessage = false;
    
    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() {
      if (newMessage) {
        newMessage = false;
        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
        }
      }
        delay(1000); //delay a second between the two times which the message is sent
        
    }
    
    int Message(String x) { //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
        message = x;
        newMessage = true;
        return 1;
    }

BTW, what do you mean by “Still returns an error”?

To be honest I’m not sure I understand what you exactly mean by “a flag”. Could you please give me a brief explanation?

By “still returns an error” I mean that when I call the Particle.function it returns “Error”, just as in the first code I posted.

Could you try renaming your function to something without blank spaces? Blank spaces often cause havoc, and this is also a chance to see if your code is actually being updated, since there’s no reason it’d return “error”.

@EK51, the flag is defined as bool newMessage = false; and is used to indicate when a new message is passed via the Particle.function(). That way loop() only parses when a new message is received.

How are you calling the Particle.function? Can you paste the exact error you are getting?

@peekay123 I am truly sorry about the flag issue. I had added it but somehow i sent the older code again. When you asked where it is I thought I had misunderstood what a flag is. This partially happened because I have been posting on my phone and partially because of my lack of attention. Sorry. I am currently not home so I will post the new code as soon as I get home to my computer.

I call the Particle.function via my IOS device using the Particle applicaton. I do not get any details about the error. All I see is “Error” next to the name of the function.

@EK51, which Particle application? You may want to consider testing with either Oak Terminal or this other nifty site.

[quote=“Moors7, post:8, topic:26529, full:true”]
Could you try renaming your function to something without blank spaces? Blank spaces often cause havoc, and this is also a chance to see if your code is actually being updated, since there’s no reason it’d return “error”.
[/quote] @Moors7 with the help of your reply above I solved my problem. All I had to do was to delete the space in my function’s name just as you said. Thank you very much.

@peekay123 Thank you for all your help. Even though you might/might not have solved an other problem in my code, you did remind me the concept of flags and how/where I could use them, after a bunch of misunderstandings :joy:. I thank you for that as much as i do for all your help.

3 Likes

Thanks you @Moors7 @peekay123 for the help! @EK51 I’m glad you were able to get your code fixed!