Hello Spark Community
I’ve put together my first spark project recently, a christmas light controller. For the most part the results work pretty well. Here’s a video of it in action:
I put this together by using an HTML5 Simon Says project by Mr. Uxmonk and simply modifying the code to call POST functions to my spark core depending on the note played. Here’s a small snippet of the web side code:
if(note==0){
$.post( "https://api.spark.io/v1/devices/xxx/lightcmd?access_token=yyy", { params: "light1ON" } );
}
if(note==1){
$.post( "https://api.spark.io/v1/devices/xxx/lightcmd?access_token=yyy", { params: "light2ON" } );
}
if(note==2){
$.post( "https://api.spark.io/v1/devices/xxx/lightcmd?access_token=yyy", { params: "light3ON" } );
}
if(note==3){
$.post( "https://api.spark.io/v1/devices/xxx/lightcmd?access_token=yyy", { params: "light4ON" } );
}
function myFunction() {
myVar = setTimeout(funcx, 800);
}
function funcx() {
$.post( "https://api.spark.io/v1/devices/xxx/lightcmd?access_token=yyy", { params: "alllightsOFF" } );
}
And then my spark side code is a pretty simple copy of the led example:
int lightcmd(String command) {
...
if(command == "light1ON"){
digitalWrite(light1, HIGH);
}
if(command == "light1OFF"){
digitalWrite(light1, LOW);
}
if(command == "light2ON"){
digitalWrite(light2, HIGH);
}
if(command == "light2OFF"){
digitalWrite(light2, LOW);
}
...
if(command == "alllightsOFF"){
digitalWrite(light1, LOW);
digitalWrite(light2, LOW);
digitalWrite(light3, LOW);
digitalWrite(light4, LOW);
}
...
It all works pretty well. I was actually surprised to find if someone pressed two notes at the same time, both lights would (seemingly) light up at the same time.
My main problem is that if someone hits the notes too fast, the spark appears to get confused and eventually seems to timeout. Is there some limitation on the number of POST function calls I can expect the spark to handle in a certain period of time? Or is there some sort of watchdog type function that I could potentially add to protect against the spark timing out? Any help would be appreciated.