Back to Parent

// FINAL CODE AFTER CONNECTING BOTH PHOTONS AND PUBLISHING EVENTS ( NO FLICKERING EFFECT )

#include "neopixel.h"
#include <math.h>
// IMPORTANT: Set pixel COUNT, PIN and TYPE
#define PIXEL_PIN D2
#define PIXEL_COUNT 24
#define PIXEL_TYPE WS2812

Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);

int irPin = A0;
int irProximity = 0;
int transitionTime = 5000; // Transition Time in ms

const int sampleWindow = 50;

int distance = 0; // initialize distance variable
int unsigned long start;
int unsigned long timeElapsed;

int lastPublishedAt = 0;
int publishAfter = 1000;
String a = "b";
String neopixel = "";

unsigned long lightedafter = 0;


void handle( const char *event, const char *data){
  for(int i=0; i<strip.numPixels(); i++) {
    strip.setPixelColor(i, 255,173,0);
    strip.show();
}
}




void setup() {
  Serial.println( 9600 ); //enable serial monitor
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
  pinMode( PIXEL_PIN, OUTPUT );
  //pinMode(ledPin, OUTPUT);
  start = millis();
  timeElapsed = 0;
  RGB.control(TRUE);
}

void loop() {
  int sample = analogRead( irPin );
  sample = map( sample, 0, 4095, 1000, 0 );
  Particle.subscribe("imhome/CMUIoT", handlesharedevent); //"210032000d47343438323536"

  if (millis() - lightedafter > 2000){
  if(distance > 300){
    for(int i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, 0,0,0);
      strip.show();
    RGB.color(0,0,0);
    a = 'h';
    publishmyEvent(a);
      }
    }
  }

   distance = sampleProximity();
  Serial.println( distance );
  if(distance <= 300){


    a = 'l';
    publishmyEvent(a);
  // strip.Color(8, 255,173,0);
  // strip.show();
  }


}

void publishmyEvent(String a)
{
  if( lastPublishedAt + publishAfter < millis() )
{
  neopixel = "imhome/CMUIoT" + System.deviceID();
  if (a=="h"){
    Particle.publish(neopixel, "Motion > 300" );
    lastPublishedAt = millis();
  }else if(a == "l"){
    Particle.publish(neopixel, "Motion < 300" );
    lastPublishedAt = millis();
  }
}
}

void handlesharedevent(const char *event, const char *data)
{
 String neopixel2 = String( event ); // convert to a string object
 String deviceID = System.deviceID();
 if( neopixel2.indexOf( deviceID ) != -1 ){
 // The event was published by MY photon
 }else{
   // The event was published by the OTHER photon
   String whatDoesTheDataSay = String(data);
   if(whatDoesTheDataSay == "Motion < 300"){
     //individualServo1();

       for(int i=0; i<strip.numPixels(); i++) {
         strip.setPixelColor(i, 255,173,0);
         strip.show();
        RGB.color(255,173,0);
       lightedafter = millis();
     }
   }


 }
}


int sampleProximity( )
{
  unsigned long startMillis = millis(); // Start of sample window
  int farthest_sample = 0;
  int closest_sample = 1000;
  // collect data for 50 mS
  while (millis() - startMillis < sampleWindow)
  {
    int sample = analogRead( irPin );
    // invert the range, and convert it to a percent
    sample = map( sample, 0, 4095, 1000, 0 );
    // now see if the sample is the lowest;
    if ( sample > farthest_sample ){
    farthest_sample = sample ;
    }
    if ( sample < closest_sample ){
    closest_sample = sample;
    }
  }

  Serial.print( "Farthest = " );
  Serial.println( farthest_sample );
  Serial.print( "Closest = " );
  Serial.println( closest_sample );
  int proximityAverage = (farthest_sample + closest_sample)/2 ;
  return proximityAverage;
}

void colorWipe(uint32_t c, int wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
  }
}
Click to Expand

Content Rating

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

0