Back to Parent

// This #include statement was automatically added by the Particle IDE.
#include <neopixel.h>


//initialize the boolean variable for movement to false.
bool motion = false;
//set the speed of the lights to cycle through one 
int lightSpeed = 100;
//define pin for the PIR sensor
int PIRpin = D2;


//define neopixel ring
#define PIXEL_PIN D3
#define PIXEL_COUNT 14
#define PIXEL_TYPE WS2812
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
//RGBW and CYMK colors as global variables
uint32_t w = strip.Color(255, 255, 255);// white (all on)
uint32_t r = strip.Color(255, 0, 0);    // red
uint32_t y = strip.Color(255, 255, 0);  // yellow
uint32_t g = strip.Color(0, 255, 0);    // green
uint32_t c = strip.Color(0, 255, 255);  // cyan
uint32_t b = strip.Color(0, 0, 255);    // blue
uint32_t m = strip.Color(255, 0, 255);  // magenta (pink/purple)
uint32_t k = strip.Color(0, 0, 0);      // black (all off)

void setup()
{

  /*Subscribe to an event that is only published if there is a reaction 
  at another bus stop.*/
  Particle.subscribe(  "ChenMutiatLama/2019/Group4/Reaction", action );
  
  //Initialize all pixels to 'off'
  strip.begin();
  strip.show();

}

void loop()
{
    
}

void publishEvent(String eventID, String data)
{
 /* Remember our subscribe is matching  "ChenMutiatLama/2019/Group4/"
  We'll append the device id to get more specific about where the 
  event came from and an event ID to specify what the event is 
  supposed to trigger. 
  System.deviceID() provides an easy way to extract the device ID of 
  your device. It returns a String object of the device ID, which is 
  used to identify your device.*/
  String eventName = "ChenMutiatLama/2019/Group4/"+ eventID + "/" + System.deviceID();
  // now we have something like "diot/2019/paired/0123456789abcdef"
  // and that corresponds to this devices info
  // then we publish it
  Particle.publish( eventName, data );
}


// Our event handler requires two bits of information
// This gives us:
// A character array that consists of the event name
// A character array that contains the data published in the event 
// we're responding to.
void action(const char *event, const char *data){
    //Particle.publish("acting");
    String m = strstr(data, "mexic" );
    String mCaps = strstr(data, "Mexic" );
    String r = strstr(data, "russia" );
    String rCaps = strstr(data, "Russia" );
    String s = strstr(data, "syria" );
    String sCaps = strstr(data, "Syria" );
    String i = strstr(data, "israel" );
    String iCaps = strstr(data, "Israel" );
    String v = strstr(data, "venezuela" );
    String vCaps = strstr(data, "Venezuela" );
    String c = strstr(data, "chin" );
    String cCaps = strstr(data, "Chin" );
    String country = "no";
    if(m!=NULL|| mCaps!=NULL){
        mexicanFlag();
     
    }
    else if(r!=NULL|| rCaps!=NULL){
        russianFlag();
    }
    else if(s!=NULL|| sCaps!=NULL){
        syrianFlag();
    }
    else if(i!=NULL|| iCaps!=NULL){
        israeliFlag();
    }
    else if(v!=NULL|| vCaps!=NULL){
        venezuelanFlag();
    }
    else if(c!=NULL|| cCaps!=NULL){
        chineseFlag();
    }
    else{
        rainbow();
    }
    turnoff();
    
}

// Call the flag colors function for the country or people 
// from the following countries. Using predefinied lists of colors
//mexico
void mexicanFlag(){
    delay(100);
    uint32_t flag[] = {r,w,g,k};
    flagColors(flag);
}
//syria
void syrianFlag(){
    delay(100);
    uint32_t flagA[] = {r,w,g,w};
    uint32_t flagB[] = {g,w,k,k};
    flagColors(flagA);
    flagColors(flagB);
    
}
//russia
void russianFlag(){
    delay(100);
    uint32_t flag[] = {w,b,r,k};
    flagColors(flag);
}
//israel
void israeliFlag(){
    delay(100);
    uint32_t flag[] = {b,w,b,k};
    flagColors(flag);
}
//venezuela
void venezuelanFlag(){
    delay(100);
    uint32_t flag[] = {y,b,r,k};
    flagColors(flag);
}
//china
void chineseFlag(){
    delay(100);
    uint32_t flag[] = {r,r,y,r};
    flagColors(flag);
}
//Shows a rainbow
void rainbow(){

    uint32_t rainbow[] = {w,r,y,g,c,b,m,k};
    for(int c=0; c<8; c++) {
        for(uint16_t p=0; p< strip.numPixels(); p++) {
        	//change the color
    		strip.setPixelColor(p, rainbow[c] );
        	//show the color
    		strip.show();
        	delay( lightSpeed );
        }
    }
}
//Flashes neopixel leaving all the lights off
void turnoff(){
    //run through flag colors with all colors set to off
    uint32_t onoff[] = {k,k,k,k};
    flagColors(onoff);
}

//Takes a list of up to four colors and shows them on the neopixels
void flagColors(uint32_t colors[]){
    for(int c=0; c< sizeof(colors); c++) {
        for(uint16_t p=0; p< strip.numPixels(); p++) {
            //change the color
            strip.setPixelColor(p, colors[c] );
            //show the color
            strip.show();
            delay( lightSpeed );
        }
    }
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0