// This #include statement was automatically added by the Particle IDE.
#include <neopixel.h>
#define PIXEL_PIN D2
#define PIXEL_COUNT 16
#define PIXEL_TYPE WS2812
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
int r = 0;
int b = 0;
int g = 0;
long lastPublishedAt = 0;
int publishAfter = 100; //the initial delay
bool blueOn = false; //color for Lily = blue
void setup() {
strip.begin();
Particle.subscribe("diot2021blossomblue" , handleSharedEvent);
Particle.function("diot2021blossomnblue", BluePixelOn); //to turn on Lily's neopixel from the console
}
void loop() {
// strip.show(); neopixel strip is off in the beginning
publishPixelon(); //to give ddetails of the event to be published to all devices
}
int BluePixelOn(String b){ //functions for particle.function start with an int and take only string commands
int bValue = b.toInt(); //first:convert string to number
uint32_t blue = strip.Color(0, 0, bValue); //second: use the newly converted int to define blue
uint16_t i;
if (blueOn == true){
for( int i = 0; i < strip.numPixels(); i++ ){
strip.setPixelColor(i, blue); // each pixel to turn on blue one by one
strip.show();
delay( 200 );
}
blueOn = false;
}
delay(100);
return 1;
}
void publishPixelon(){
if ( lastPublishedAt + publishAfter < millis() ){
String pixelOn = "diot2021blossomblue";
if(blueOn == false){
Particle.publish(pixelOn);
}
lastPublishedAt = millis();
}
}
void handleSharedEvent(const char *event, const char *data){
String pixelOn = String(event); // this is reading the function as per particle.publish, convert to a string object first
String deviceID = System.deviceID();
// device id =
// event being triggered = "diot2021blossomneopixel"
if( pixelOn.indexOf( deviceID) != -1 ){
return;
}
else{
blueOn = true;
}
}
Click to Expand
Content Rating
Is this a good/useful/informative piece of content to include in the project? Have your say!
You must login before you can post a comment. .