I’ve been able to blink an led, and rotate a servo separately.
Now I’m trying to do both, but I’m missing something: either the LED blinks, or the servo moves, but I can’t control them both separately. I’m sure I’m doing something fundamentally wrong.
Any help/advice appreciated.
thanks!
DC
Here’s my code, with some commentary from one of the Particle examples:
Servo serv;
int pos = 0;
int led1 = D6;
int led2 = D7;
void setup() {
serv.attach(D0);
Particle.function("setpos", setPos);
Particle.variable("getpos", &pos, INT);
// Here's the pin configuration.
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
// We are also going to declare a Particle.function so that we can turn the LED on and off from the cloud.
Particle.function("led",ledToggle);
// This is saying that when we ask the cloud for the function "led", it will employ the function ledToggle() from this app.
// For good measure, let's also make sure both LEDs are off when we start:
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
}
void loop() {
}
int setPos(String pstn) {
pos = pstn.toInt();
serv.write(pos);
return pos;
}
// We're going to have a super cool function now that gets called when a matching API request is sent
// This is the ledToggle function we registered to the "led" Particle.function earlier.
int ledToggle(String command) {
/* Particle.functions always take a string as an argument and return an integer.
Since we can pass a string, it means that we can give the program commands on how the function should be used.
In this case, telling the function "on" will turn the LED on and telling it "off" will turn the LED off.
Then, the function returns a value to us to let us know what happened.
In this case, it will return 1 for the LEDs turning on, 0 for the LEDs turning off,
and -1 if we received a totally bogus command that didn't do anything to the LEDs.
*/
if (command=="on") {
digitalWrite(led1,HIGH);
digitalWrite(led2,HIGH);
return 1;
}
else if (command=="off") {
digitalWrite(led1,LOW);
digitalWrite(led2,LOW);
return 0;
}
else {
return -1;
}
}
And here’s what I’ve got on my website.
<p>This is an attempt to combine servo code and blink an LED code. And then combine the blink and the continuous rotation web code.</p>
<hr />
<style type="text/css">body {
margin: 1em auto;
max-width: 45em;
padding: 0.8em;
background: white;
}
</style>
<h2>Turn the light on and off</h2>
<form>
<p><input type="radio" value="off" onclick="showValue(this.value)" name="led" /> Off
<input type="radio" value="on" onchange="showValue(this.value)" name="led"/> On
</p>
</form>
<script type="text/javascript">
function showValue(newValue) {
// document.getElementById("range").innerHTML=newValue;
var request = new XMLHttpRequest();
var dev_id = '2a0031000847353138383138';
var access = '55124b34fd82a5e272dc8772438e81e14213710e';
var data = 'params='+newValue+'&access_token='+access;
var url = 'https://api.particle.io/v1/devices/'+dev_id+'/led/';
request.open('POST', url, true);
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
request.send(data);
}
</script></p>
<h2>Continuous servo control</h2>
<style type="text/css">body {
margin: 1em auto;
max-width: 45em;
padding: 0.8em;
background: white;
}
</style>
<form>
<p><input type="radio" value="93" onclick="showValue(this.value)" name="servo" /> Off
<input type="radio" value="0" onchange="showValue(this.value)" name="servo"/> On (right)
<input type="radio" value="180" onchange="showValue(this.value)" name="servo"/> On (left)
</p>
</form>
<script type="text/javascript">
function showValue(newValue) {
// document.getElementById("range").innerHTML=newValue;
var request = new XMLHttpRequest();
var dev_id = '2a0031000847353138383138';
var access = '55124b34fd82a5e272dc8772438e81e14213710e';
var data = 'params='+newValue+'&access_token='+access;
var url = 'https://api.particle.io/v1/devices/'+dev_id+'/setpos/';
request.open('POST', url, true);
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
request.send(data);
}
</script></p>