# \[SOLVED\] Code doesn't work

**URL:** <https://community.particle.io/t/solved-code-doesnt-work/26529>\
**Category:** General\
**Created:** [October 14, 2016, 2:57pm UTC](https://community.particle.io/t/solved-code-doesnt-work/26529 "2016-10-14T14:57:21Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![EK51](https://avatars.discourse-cdn.com/v4/letter/e/9e8a1a/32.png) [@EK51](https://community.particle.io/u/EK51)\
**Post date:** [October 14, 2016, 2:57pm UTC](https://community.particle.io/t/solved-code-doesnt-work/26529/1 "2016-10-14T14:57:21Z")

</div>

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:

```auto

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

```

---

<div class="post-metadata">

**Author:** ![bko](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bko/32/20704_2.png) [@bko](https://community.particle.io/u/bko)\
**Post date:** [October 14, 2016, 2:59pm UTC](https://community.particle.io/t/solved-code-doesnt-work/26529/2 "2016-10-14T14:59:31Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![EK51](https://avatars.discourse-cdn.com/v4/letter/e/9e8a1a/32.png) [@EK51](https://community.particle.io/u/EK51)\
**Post date:** [October 14, 2016, 3:06pm UTC](https://community.particle.io/t/solved-code-doesnt-work/26529/3 "2016-10-14T15:06:44Z")

</div>

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

```auto
    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;
    }

```

---

<div class="post-metadata">

**Author:** ![peekay123](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/peekay123/32/810_2.png) [@peekay123](https://community.particle.io/u/peekay123)\
**Post date:** [October 14, 2016, 3:13pm UTC](https://community.particle.io/t/solved-code-doesnt-work/26529/4 "2016-10-14T15:13:18Z")

</div>

@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. 😉

---

<div class="post-metadata">

**Author:** ![EK51](https://avatars.discourse-cdn.com/v4/letter/e/9e8a1a/32.png) [@EK51](https://community.particle.io/u/EK51)\
**Post date:** [October 14, 2016, 3:20pm UTC](https://community.particle.io/t/solved-code-doesnt-work/26529/5 "2016-10-14T15:20:55Z")

</div>

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

Here’s the new code:

```auto
    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;
    }

```

---

<div class="post-metadata">

**Author:** ![peekay123](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/peekay123/32/810_2.png) [@peekay123](https://community.particle.io/u/peekay123)\
**Post date:** [October 14, 2016, 3:26pm UTC](https://community.particle.io/t/solved-code-doesnt-work/26529/6 "2016-10-14T15:26:31Z")

</div>

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

```auto
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”?

---

<div class="post-metadata">

**Author:** ![EK51](https://avatars.discourse-cdn.com/v4/letter/e/9e8a1a/32.png) [@EK51](https://community.particle.io/u/EK51)\
**Post date:** [October 14, 2016, 3:32pm UTC](https://community.particle.io/t/solved-code-doesnt-work/26529/7 "2016-10-14T15:32:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Moors7](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/moors7/32/588_2.png) [@Moors7](https://community.particle.io/u/Moors7)\
**Post date:** [October 14, 2016, 3:35pm UTC](https://community.particle.io/t/solved-code-doesnt-work/26529/8 "2016-10-14T15:35:25Z")

</div>

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”.

---

<div class="post-metadata">

**Author:** ![peekay123](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/peekay123/32/810_2.png) [@peekay123](https://community.particle.io/u/peekay123)\
**Post date:** [October 14, 2016, 3:35pm UTC](https://community.particle.io/t/solved-code-doesnt-work/26529/9 "2016-10-14T15:35:38Z")

</div>

@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?

---

<div class="post-metadata">

**Author:** ![EK51](https://avatars.discourse-cdn.com/v4/letter/e/9e8a1a/32.png) [@EK51](https://community.particle.io/u/EK51)\
**Post date:** [October 14, 2016, 3:48pm UTC](https://community.particle.io/t/solved-code-doesnt-work/26529/10 "2016-10-14T15:48:08Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![peekay123](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/peekay123/32/810_2.png) [@peekay123](https://community.particle.io/u/peekay123)\
**Post date:** [October 14, 2016, 3:52pm UTC](https://community.particle.io/t/solved-code-doesnt-work/26529/11 "2016-10-14T15:52:51Z")

</div>

> [@EK51](#):
>
> Particle applicaton

@EK51, which Particle application? You may want to consider testing with either [Oak Terminal](http://rawgit.com/kh90909/OakTerm/master/index.html) or [this other nifty site](http://suda.github.io/particle-web-interface/).

---

<div class="post-metadata">

**Author:** ![EK51](https://avatars.discourse-cdn.com/v4/letter/e/9e8a1a/32.png) [@EK51](https://community.particle.io/u/EK51)\
**Post date:** [October 14, 2016, 7:14pm UTC](https://community.particle.io/t/solved-code-doesnt-work/26529/12 "2016-10-14T19:14:47Z")

</div>

[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 😂. I thank you for that as much as i do for all your help.

---

<div class="post-metadata">

**Author:** ![KyleG](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/kyleg/32/11077_2.png) [@KyleG](https://community.particle.io/u/KyleG)\
**Post date:** [October 15, 2016, 7:58pm UTC](https://community.particle.io/t/solved-code-doesnt-work/26529/13 "2016-10-15T19:58:47Z")

</div>

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