(H/T to @will for the suggestion on this)
Since sketches are restricted to only allow a maximum of four functions exposed via Spark.function(), you can create a function “router” or “switcher” (or whatever) that wraps many functions inside a single function exposed via Spark.function(). Since I have several sensors attached to a single Spark, I had to use this method to get all the values I wanted. I’ve pared it down and provided some sample code to illustrate how my function router works. You can even poll the API for the lastCommand variable to get the last command that was issued to fnRouter().
DISCLAIMER: This code compiles in the web build IDE, but I haven’t flashed it to a Spark to test it in the real world.
// LED COLORS
uint8_t colorRed[3] = { 255, 0, 0 };
uint8_t colorOrange[3] = { 255, 165, 0 };
uint8_t colorYellow[3] = { 255, 255, 0 };
uint8_t colorGreen[3] = { 0, 255, 0 };
uint8_t colorBlue[3] = { 0, 0, 255 };
uint8_t colorPurple[3] = { 128, 0, 128 };
uint8_t colorWhite[3] = { 255, 255, 255 };
uint8_t colorPink[3] = { 255, 192, 203 };
// String to hold last command issued
char lastCommand[64] = "NONE";
void setup() {
// Cloud functions
Spark.function("fnrouter", fnRouter);
// Cloud variables
Spark.variable("lastCommand", &lastCommand, STRING);
}
void loop() {
// Do nothing
}
int fnRouter(String command) {
command.trim();
command.toUpperCase();
command.toCharArray(lastCommand, 64);
if(command.equals("SECONDS"))
return millis()/1000;
else if(command.equals("MILLIS"))
return millis();
else if(command.equals("LEDRED"))
return blinkLed(colorRed);
else if(command.equals("LEDBLUE"))
return blinkLed(colorBlue);
else if(command.equals("LEDGREEN"))
return blinkLed(colorGreen);
else if(command.equals("LEDWHITE"))
return blinkLed(colorWhite);
else if(command.equals("LEDYELLOW"))
return blinkLed(colorYellow);
else if(command.equals("LEDORANGE"))
return blinkLed(colorOrange);
else if(command.equals("LEDPURPLE"))
return blinkLed(colorPurple);
else if(command.equals("LEDPINK"))
return blinkLed(colorPink);
else if(command.equals("D7BLINK"))
return blinkD7();
else {
blinkLedRapid(colorRed);
return -1000;
}
}
int blinkLed(uint8_t color[3]) {
RGB.control(true);
for(int i=0; i<3; i++) {
RGB.color(color[0], color[1], color[2]);
delay(250);
RGB.color(0, 0, 0);
delay(250);
}
RGB.control(false);
return 1;
}
int blinkLedRapid(uint8_t color[3]) {
RGB.control(true);
for(int i=0; i<10; i++) {
RGB.color(color[0], color[1], color[2]);
delay(25);
RGB.color(0, 0, 0);
delay(25);
}
RGB.control(false);
return 1;
}
int blinkD7() {
pinMode(D7, OUTPUT);
for(int i=0; i<3; i++) {
digitalWrite(D7, HIGH);
delay(250);
digitalWrite(D7, LOW);
delay(250);
}
return 1;
}
[NEVERMIND] Also, if anyone has any tips on how to paste in code with spaces and empty lines without having to use excessive
and <br>
, please share! [/NEVERMIND]