Connecting Neopixels on 2 particles
Made by ihans
Made by ihans
Developing paired devices and networked interactions with Particle.publish
Created: November 30th, 2021
These are smaller exercises that build on each other to help get to the final prototype for team blossom (for the purpose of documentation and to trace steps back if needed at any point.)
// 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("diot2021blossomneopixel" , handleSharedEvent);
}
void loop() {
// strip.show(); neopixel strip is off in the beginning
uint16_t i;
uint32_t c = strip.Color(0, 0, 255); // color Blue
publishPixelon();
if (blueOn == true){
for( int i = 0; i < strip.numPixels(); i++ ){
strip.setPixelColor(i, c); // each pixel turning on blue one by one
strip.show();
delay( 200 );
}
for ( int i = 15; i > 0; i-- ){
strip.setPixelColor(i, c); // each pixel turning off one by one
strip.show();
delay( 200 );
}
blueOn = false;
}
delay (100);
}
void publishPixelon(){
if ( lastPublishedAt + publishAfter < millis() ){
String pixelOn = "diot2021blossomneopixel";
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
// 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