// CODE FOR IR DISTANCE SENSOR + NEOPIXEL WITH 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 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;
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);
}
void loop() {
distance = sampleProximity();
Serial.println( distance );
if(distance > 300){
for(int i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, 0,0,0);
strip.show();
RGB.color(0,0,0);
}
}
if(distance <= 300){
state += 1;
state = state % 8;
switch (state) {
case 0:
burn(10);
break;
case 1:
flicker(5);
break;
case 2:
burn(8);
break;
case 3:
flutter(6);
break;
case 4:
burn(3);
break;
case 5:
on(10);
break;
case 6:
burn(10);
break;
case 7:
flicker(10);
break;
}
}
}
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;
}
/*
METHODS
*/
Particle.publish("timeUp");
}
/**
* Scale a value returned from a trig function to a byte value.
* [-1, +1] -> [0, 254]
* Note that we ignore the possible value of 255, for efficiency,
* and because nobody will be able to differentiate between the
* brightness levels of 254 and 255.
*/
byte trigScale(float val) {
val += 1.0; // move range to [0.0, 2.0]
val *= 127.0; // move range to [0.0, 254.0]
return int(val) & 255;
}
/**
* Map an integer so that [0, striplength] -> [0, 2PI]
*/
float map2PI(int i) {
return M_PI*2.0*float(i) / float(strip.numPixels());
}
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
if(WheelPos < 85) {
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
} else if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else {
WheelPos -= 170;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
}
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 functIon - 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!
You must login before you can post a comment. .