Hello,
Hello I am new to spark and I’m learning, yesterday managed to make the code below work but today it no longer works, do not know if I changed something and not realized, already copied and pasted the site but clicking the button one or two nothing happens. If I enter the code and put D0 or D1 for high works, just does not work the web
HTML FILE:
<br><br>
<button id="led1onbutton" onclick="switchLED(1,1)">LED 1 On</button>
<button id="led1offbutton" onclick="switchLED(1,0)">LED 1 Off</button>
<br><br>
<button id="led2onbutton" onclick="switchLED(2,1)">LED 2 On</button>
<button id="led2offbutton" onclick="switchLED(2,0)">LED 2 Off</button>
<br><br>
<script type="text/javascript">
var deviceID = "mydevicenumber";
var accessToken = "mytoken";
var setFunc = "led";
function switchLED(ledn,newValue) {
var paramStr = "l" + ledn.toString();
if (newValue==1) {
paramStr = paramStr + ",HIGH";
} else {
paramStr = paramStr + ",LOW";
}
var requestURL = "https://api.spark.io/v1/devices/" +deviceID + "/" + setFunc + "/";
$.post( requestURL, { params: paramStr, access_token: accessToken });
}
</script>
SPARK CODE
int led1 = D0;
int led2 = D1;
void setup()
{
Spark.function(“led”, ledControl);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
}
void loop()
{
}
int ledControl(String command)
{
int state = 0;
int pinNumber = (command.charAt(1) - ‘0’) - 1;
if (pinNumber < 0 || pinNumber > 1) return -1;
if(command.substring(3,7) == “HIGH”) state = 1;
else if(command.substring(3,6) == “LOW”) state = 0;
else return -1;
digitalWrite(pinNumber, state);
return 1;
}