Back to Parent

// CODE FOR IR DISTANCE SENSOR + NEOPIXEL WITH FLICKERING EFFECT AFTER CONNECTING BOTH PHOTONS AND PUBLISHING EVENTS

// This #include statement was automatically added by the Particle IDE.

#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 ledPin = D0;
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;


// color variables: mix RGB (0-255) for desired yellow
int redPx = 255;
int grnHigh = 100; //110-120 for 5v, 135 for 3.3v
int bluePx = 10; //10 for 5v, 15 for 3.3v

// animation time variables, with recommendations
int burnDepth = 10; //10 for 5v, 14 for 3.3v -- how much green dips below grnHigh for normal burn -
int flutterDepth = 25; //25 for 5v, 30 for 3.3v -- maximum dip for flutter
int cycleTime = 250; //120 -- duration of one dip in milliseconds

int fDelay;
int fRep;
int flickerDepth;
int burnDelay;
int burnLow;
int flickDelay;
int flickLow;
int flutDelay;
int flutLow;

int state = 0;

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

void setup() {

  pinMode( PIXEL_PIN, OUTPUT );

  flickerDepth = (burnDepth + flutterDepth) / 2.4;
  burnLow = grnHigh - burnDepth;
  burnDelay = (cycleTime / 2) / burnDepth;
  flickLow = grnHigh - flickerDepth;
  flickDelay = (cycleTime / 2) / flickerDepth;
  flutLow = grnHigh - flutterDepth;
  flutDelay = ((cycleTime / 2) / flutterDepth);

  strip.begin();
  strip.show();
  RGB.control(true);
  Serial.begin(9600);
  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);
  //Particle.variable("is3",state);

}

void loop() {
  Particle.subscribe("imhome/CMUIoT", handlesharedevent); //"210032000d47343438323536"
  distance = sampleProximity();
  Serial.println( distance );

  if(distance > 300){
    a = 'h';
    publishmyEvent(a);
  }

  if(distance <= 300){
   
    a = 'l';
    publishmyEvent(a);

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

// basic fire funciton - not called in main loop
void fire(int grnLow) {
  for (int grnPx = grnHigh; grnPx > grnLow; grnPx--) {
    for (int i = 0; i < PIXEL_COUNT; i++ ){
      strip.setPixelColor(i,redPx, grnPx, bluePx); // THIS IS WHAT WE EDITED
      strip.show();
  }
    delay(fDelay);
  }
  for (int grnPx = grnLow; grnPx < grnHigh; grnPx++) {
    for (int i = 0; i < PIXEL_COUNT; i++ ){
      strip.setPixelColor(i,redPx, grnPx, bluePx); // THIS IS WHAT WE EDITED
      strip.show();
  }
    delay(fDelay);
  }
}

// fire animation
void on(int f) {
  fRep = f * 1000;
  int grnPx = grnHigh - 5;
  for (int i = 0; i < PIXEL_COUNT; i++ ){
    strip.setPixelColor(i,redPx, grnPx, bluePx); // THIS IS WHAT WE EDITED
    strip.show();
}
  delay(fRep);
}

void burn(int f) {
  fRep = f * 8;
  fDelay = burnDelay;
  for (int var = 0; var < fRep; var++) {
    fire(burnLow);
  }
}

void flicker(int f) {
  fRep = f * 8;
  fDelay = burnDelay;
  fire(burnLow);
  fDelay = flickDelay;
  for (int var = 0; var < fRep; var++) {
    fire(flickLow);
  }
  fDelay = burnDelay;
  fire(burnLow);
  fire(burnLow);
  fire(burnLow);
}

void flutter(int f) {
  fRep = f * 8;
  fDelay = burnDelay;
  fire(burnLow);
  fDelay = flickDelay;
  fire(flickLow);
  fDelay = flutDelay;
  for (int var = 0; var < fRep; var++) {
    fire(flutLow);
  }
  fDelay = flickDelay;
  fire(flickLow);
  fire(flickLow);
  fDelay = burnDelay;
  fire(burnLow);
  fire(burnLow);
}

void checkturnoff(){
  Particle.connect();
  distance = sampleProximity();
  Serial.println( distance );
  if(distance > 500){
    colorWipe(strip.Color(0, 0, 0), 25);
  }
}
Click to Expand

Content Rating

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

0