Frustrated with response time

Hoping someone has a moment to look at this.

I’m using a Photon to control an LED strip. To help map out patterns and such, I’m trying to write a VERY simple sketch that increases or decreases the number of LEDs lit by adjusting start and end values updated by button taps from a MIT App Inventor project.

The app has 5 buttons: Start -, Start +, End -, End +, Refresh. The first 4 increase or decrease the head or tail of the section of LEDs and that works great. From there, I added two text fields that, via a GET operation, reads the values exposed on the device. In this case: startLit and endLit . The Refresh button executes the GET, reads the values and updates the two text fields. Again, this seems to work really well. The command even works to get the values with no hiccups as the screen initializes on the phone.

Where it starts to get hairy is when I try to add the GET function to pull the values immediately after the POST function changes the value. Sometimes it works. More often though I either get a "not found" message or nothing happens, even though the values have indeed increased and I can see that properly if I hit the Refresh button. Should I expect a reasonable amount of delay in communications between a POST and a GET function? I've brought it up in the Particle.io forums as well an, so far, the consensus has been that the microcontroller is plenty speedy at these things, so it's likely the App Inventor. Below are the blocks of my app and below that is the Particle-specific arduino sketch.

// This #include statement was automatically added by the Particle IDE.
//#define FASTLED_ALLOW_INTERRUPTS 0
#include <FastLED.h>
#include "Particle.h"

/*
  LED Map
*/
FASTLED_USING_NAMESPACE;

#define LED_PIN     D4
#define COLOR_ORDER GRB
#define CHIPSET     WS2812B
#define NUM_LEDS 300 //88 for testing, 300 for prodction
#define BRIGHTNESS  200
#define FRAMES_PER_SECOND 24

int startLit = 0;
int endLit = 1;
int addStart(String command);
int subStart(String command);
int addEnd(String command);
int subEnd(String command);
int doUpdate(String command);

NSFastLED :: CRGB leds[NUM_LEDS];

void setup() {
  delay(3000); // sanity delay
  FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  FastLED.setBrightness( 10 );
  FastLED.clear();
  fill_solid(leds,NUM_LEDS,CRGB::Black);
  //leds[0] = CHSV(255,255,255);
  set_max_power_in_volts_and_milliamps(5, 1000);
  updateStrip();
  FastLED.show();
  
  Particle.variable("startLit", startLit);
  Particle.variable("endLit", endLit);
  Particle.function("doUpdate", doUpdate);
}


void loop() {
    
}

int doUpdate(String command){
    if(command=="addStart"){
        if(startLit<endLit){
            startLit++;
        }
    }else if(command=="subStart"){
        if(startLit>=0){
            startLit--;
        }
    }else if(command=="addEnd"){
        if(endLit<NUM_LEDS-1){
            endLit++;
        }
    }else if(command=="subEnd"){
        if(endLit>startLit){
            endLit--;
        }
    }
    updateStrip();
    return 1;
}


void updateStrip(){
    for(int i=0;i<NUM_LEDS;i++){
        if((i>=startLit) && (i<=endLit)){
            leds[i] = CHSV(255,255,255);
        }else{
            leds[i] = CHSV(0,0,0);
        }
    }
    FastLED.show();
}

You are using the PostText method to send something, but before the result has been received in the GotText event, you are calling the getResults method to send 2 other Get requests...

Unbenannt

The blocks do not wait... The model of event processing in App Inventor by Lyn

Do do really need to call getResults after posting? If yes, just call the getResults method in the GotText event after the result of the PostText method has been received... you might want to add a global variable to find out, which result (Post o Get) is being received in the GotText event...

Taifun


Trying to push the limits! Snippets, Tutorials and Extensions from Pura Vida Apps by icon24 Taifun.

I'm not sure if I understand you correctly.

I do need to get and display the results after the post, and I understand that, as I have it now, they're basically happening at the same time (is that right?). Where I'm confused by your direction of calling getResults in the GotText event, because the GotText is is triggered only because of the getResults in the first place. So maybe I'm using the wrong blocks in the first place. Here's the order of events I want to happen, and what I've used to make it happen. Please tell me where I went wrong:

Button on App is pressed (startUp) -> App sends a command that increases a variable on the controller (SendToParticle) then triggers a request to read all the current variables (getResults) -> getResults just combos two GetFromParticle calls that read both of the variables -> When the text returns from GetFromParticle (Web1.GotText), parse the info based on specified criteria and display it on the App.

Correct me if I'm wrong, and there's a very strong chance I am, but nothing happens after the Web1.PostText call, because that's just sending the command, not requesting anything. It's the Web1.Get call that causes the JSON info to be output for the App to read.

yes

usually all requests return something... i.e. usually after sending your Post request in the GotText Event you receive if it was successful or not

and you also have the line
return 1
in your sketch at the end of the doUpdate function...

it looks like you never tested what will be returned in the GotText event after sending the Post request? To find it out, just display it in a label....

see also this example, which is an automatic gate opener controlled by an App Inventor app and a Photon...

Taifun