Substring causing function to hang - halp!

Having Trouble with substring with the code below. The string is coming through correctly, the comma locations are identified, but it seems to hang before extracting substrings. On the console, the last message I get is " {“data”:“6”,“ttl”:“60”,“published_at”:“2017-02-11T14:09:51.191Z”,“coreid”:“1e0026001247343339383037”,“name”:“Comma Index 2 Set”}"

I’m sort of stuck… I’ve used this same method in another script without issue, so I’m unsure of why it’s hanging now.

This is on a Photon running V.6 of the firmware.

Any help would be greatly appreciated!

    /* ======================= includes ================================= */
    #include "application.h"

    /* ====================== Definitions =============================== */

    //Basic Globals
    int led = D7;   // built-in tiny LED
    //Warm LED Strip above window
    int warm_led = D0;
    int warm_led_value = 0;
    //int maxFreq = analogWriteMaxFrequency(warm_led);

    void setup() {

      // On board LED initialized as output

      pinMode(led, OUTPUT);     
      Particle.publish("Running Setup");
      //Warm LED strip above window
      Spark.function("WARM_STRIP", warm_fade);
      pinMode(warm_led, OUTPUT);
      analogWriteResolution(warm_led, 12); // sets analogWrite resolution to 12 bits


    void loop (){
        Particle.publish("Warm Light test");


    // The following function is called Using a Cloud API
    // Specifically CoAP packets sent over a TCP socket encrypted through an RSA asymmetric key handshake passed off to an AES encrypted tunnel

    int warm_fade(String args){
        Particle.publish("Entering warm_fade", String(args));
        //Decipher args
        int commaIndex          = args.indexOf(',');  //Create index of arguments separated by commas
        int secondCommaIndex    = args.indexOf(',', commaIndex+1);
        Particle.publish("Comma Index 1 Set", String(commaIndex));
        Particle.publish("Comma Index 2 Set", String(secondCommaIndex));

        //String id_led           = args.substring(0, commaIndex);
        String id_led           = args.substring(0, 1);
        Particle.publish("Substrings Id_led", String(id_led));
        //String target_level     = args.substring(commaIndex+1, secondCommaIndex);
        String target_level     = args.substring(2, 6);
        Particle.publish("Substrings target_level", String(target_level));
        //String dim_time         = args.substring(secondCommaIndex+1);
        String dim_time         = args.substring(7);
        Particle.publish("Substrings dim_time", String(dim_time));

        int id_led_int          = id_led.toInt();
        int target_level_int    = target_level.toInt();
        int dim_time_int        = dim_time.toInt();
        Particle.publish("Integers Set");
        int dim_time_step;
        int dim_percent;

        if( target_level_int > 4095 ) return -1;
        if( target_level_int < 0 ) return -1;
        Particle.publish("Target levels Calculated");

        dim_time_step  = int(dim_time_int/target_level_int*1000);
        dim_percent = int(target_level_int/4095*100);
        //dim_distance   = int(target_level_int - warm_led_value);

        Particle.publish("Dim Time Calculated");

        analogWrite(warm_led, warm_led_value, 1000); // ensure we're starting at target brightness

        if (warm_led_value < target_level_int){                     //Fade up if target is greater than current
            for (warm_led_value; warm_led_value < target_level_int; warm_led_value++){
                analogWrite(warm_led, warm_led_value, 1000); // 4095 represents 100%
                delay(dim_time_step);                               // Wait for per step ms
            Particle.publish("Warm Light brightened");
            return 1;

        if (warm_led_value > target_level_int){                     //Fade down if target is less than current
            for (warm_led_value; warm_led_value > target_level_int; warm_led_value--){
                analogWrite(warm_led, warm_led_value, 1000); // 4095 represents 100%
                delay(dim_time_step);                               // Wait for per step ms
            Particle.publish("Warm Light Dimmed");
            return 1;

        Particle.publish("Warm light is already at requested value");
        return 1; //Assumption is that the target brightness is equal to the current brightness

I’d say it’s not substring() but you are violating the rate limit of 1/sec for Particle.publish()

1 Like

It was… lol…

Posting solution here: Integer maths…

dim_time_step = int(dim_time_int/target_level_int*1000); was resulting in zero because time int/level int was less than 1.

Changed to
dim_time_step = int(dim_time_int*1000/target_level_int); and the problem was solved.

Now I have another issue, where the particle goes into slow strobing green after the function completes.

You definetly need to get your publish rate reduced.
Hammering the events out that fast will not be accepted and may have side effects.
And as long it’s not corrected, this point will keep getting addresses first by anybody that might read your code :wink:

BTW, after which function completes?