Hi, I’m trying to turn a NeoPixel Ring on and off, with a Spark Core.
I got it to turn off, but I can’t get it to turn back “on.” I know I’m leaving something out. Something’s missing.
Any help/ideas appreciated.
Here’s the Spark core code:
#include "application.h"
#include "neopixel/neopixel.h" // use for Build IDE
// #include "neopixel.h" // use for local build
SYSTEM_MODE(AUTOMATIC);
// IMPORTANT: Set pixel COUNT, PIN and TYPE
#define PIXEL_PIN D6
#define PIXEL_COUNT 24
#define PIXEL_TYPE WS2812B
int lights_on=0;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
// Prototypes for local build, ok to leave in for Build IDE
void rainbow(uint8_t wait);
uint32_t Wheel(byte WheelPos);
void setup()
{
Particle.function("setlight", setlight);
strip.begin();
strip.setBrightness(128);
strip.show(); // Initialize all pixels to 'off'
}
void loop()
{if (lights_on)
rainbow(20);
else{}
}
int setlight(String pstn) {
lights_on = pstn.toInt();
serv.write(pos);
return pos;
}
void rainbow(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256; j++) {
for(i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel((i+j) & 255));
}
strip.show();
delay(wait);
}
}
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
if(WheelPos < 85) {
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
} else if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else {
WheelPos -= 170;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
}
And here’s what I’ve got on my webpage:
<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 = '53ff6e065075535119361687';
var access = '55124b3412345678972438e81e14213710e';
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>