[SOLVED]Setting Variable through Function?

@BulldogLowell said I should be able to set a variable that is used in a function. With that in mind after some sleuthing I found the post [Tutorial] below and was able to set the Variable mysleepyTime through a Function setsleeptime in my code down below. Again, it works. If I enter a value in setsleeptime function, then enter “sleep” in the sleep function it’ll sleep for that long! Yippee!

Here’s what I don’t understand. I don’t get a cloud variable for getsleepytime Nothing shows up even if I do a CLI particle list I don’t see it listed. I kinda was expecting I could see whatever value I had last entered in setsleeptime And when ever I enter a Parameter for setsleeptime the Result is always 0.

-Jose

The inspiration:

Continuing the discussion from Tutorial: Spark Variable and Function on One Web Page:

[quote=“bko, post:5, topic:4181, full:false”]…

int myParam = 0; // or other initial value

void setup()

{
Spark.function(“setparam”, setParam);
Spark.variable(“getparam”, &myParam, INT);
// whatever other setup you need
}

void loop() {
//you code that uses myParam
}

int setParam(String paramValue) {
myParam = paramValue.toInt();
return 0;
}
[/quote]

Here’s my interpretation:

// Variable to set sleep duration in minutes
int mysleepyTime = 0;

void setup() {

    // Bunch of pin declarations
       
    Serial.begin(9600);    // Initialize serial communications with the PC

    // These functions are useful for remote diagnostics. Read more below.
    Particle.function("sleep", sleepMode);
    Particle.function("setsleeptime", setsleepTime);
    Particle.variable("getsleepytime", &mysleepyTime, INT);
     }

void loop(){
//The Loop
    } 
int setsleepTime(String paramValue) {
    mysleepyTime = paramValue.toInt();
    return 0;
    }

// Lets you remotely invoke sleep mode by calling the function "sleep"
int sleepMode(String command){
    if(command == "sleep")
    {
        System.sleep(SLEEP_MODE_DEEP, mysleepyTime * 60);
        return 1;
    }
    else return -1;
    }
}

Your variable name getsleepytime is too long. The maximum name length is 12 characters.

https://docs.particle.io/reference/firmware/photon/#cloud-functions

4 Likes

OMG! Thank you.

Totally works as expected!

2 Likes