Back to Parent

//CODE FOR IR DISTANCE SENSOR + NEOPIXEL

#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; // Tranition Time in ms

const int sampleWindow = 50;

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

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;
}

void loop() {
  distance = sampleProximity();
  Serial.println( distance );
  if(distance <= 300){
    rainbow(300);
  }

  // int unsigned long finish = millis();
  // timeElapsed = finish - start;
  //
  // if(timeElapsed >= 3000){
  //   rainbow(300);
  // digitalWrite(ledPin, HIGH);
  // delay(500);
  // digitalWrite(ledPin, LOW);
  // delay(500);
  // start = millis();
  // }
  // delay(500);
}

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
*/
void rainbow(int wait) {
  uint16_t i, j;
  for(j=0; j<85; j++) {
    for(i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel((j) & 255));
    }
    strip.show();
    delay(wait);
    Particle.connect();
    distance = sampleProximity();
    Serial.println( distance );
    if(distance > 500){
      colorWipe(strip.Color(0, 0, 0), 25);
      break;
    }
  }
  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);
  }
}
Click to Expand

Content Rating

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

0