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 29
#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 a set of events published when an IFTTT widget is triggered by 
  a tweeting. The first one is connected to the account of a teammate for demo 
  purposes, the second one is connected to donaldTrump's twitter acound and the 
  last one is independent of a username so any other account can be used.*/
  Particle.subscribe("ChenMutiatLama/2019/Group4/LamaAlFulaij", action );
  Particle.subscribe("ChenMutiatLama/2019/Group4/realDonaldTrump", action );
  Particle.subscribe("ChenMutiatLama/2019/Group4/tweet", action );
  Particle.variable ("Motion",motion);
  
  //PIR is readable
  pinMode (PIRpin, INPUT_PULLDOWN);
 
  //Initialize all pixels to 'off'
  strip.begin();
  strip.show();
   
}
void loop()
{
    
    //Stores the value output by the PIR based on whether or not
    //It detects motion by reading the PIR pin.
    int currentMotion = digitalRead (PIRpin);
    
    //If there is motion the global variable motion is set to true
    if (currentMotion==HIGH){
      motion = true;
    }
    //Otherwise set the global variable motion is set to false
    else{
      motion = false;
    }
    
    //wait a little
    delay(100);
}
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();
    }
    
    checkReaction(data);
    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);
}
//makes every neopixel breath once
void pulse(){
  //ascending from 0 to 225
  for (int i=0; i < 225 ; i++) {
    //cycle through every pixel
    for(int j=0; j<strip.numPixels(); j++) {
      //ascending through shades of white
      strip.setPixelColor(j, strip.Color(i,i,i)); 
    }
    //show the change
    strip.show();
    //wait a bit
    delay(10);
  }
  //descending from 225 to 0
  for (int i = 225; i > 0; i--) {
    //cycle through every pixel
    for(int j=0; j<strip.numPixels(); j++) {
      //descending through shades of white
      strip.setPixelColor(j, strip.Color(i,i,i));
    }
    //show the change
    strip.show();
    //wait a bit
    delay(10);
  }
}
//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 );
        }
    }
}
void checkReaction(String data){
    
    delay(3000);
    if(motion==true){
        //Publish an event to indicate that there was a motion 
        //based reaction and the first bus stop. The tweet data
        //is published as the data, while "reaction" is the 
        //event name
        publishEvent("Reaction", data);
    }
    else{
        //Publish an event to indicate that there was not a 
        //motion based reaction and the first bus stop and there 
        //is no data in the event 
        publishEvent("NoReaction", "");
    }
    //pulse the lights onece
    pulse();
    //take a pause
    delay(1000);
    //publish the motion variable
    //this is more so to allow the developer or commissioned 
    //entity to know when the stop is triggered and if there was a reaction.
    publishEvent("Motion", (String)motion);
    
    
}
Click to Expand

Content Rating

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

0