#include <Adafruit_NeoPixel.h>

#define PIN 6
#define NUMPIXELS 24
#include <NewPing.h>

int distance;
int ledFadeTime = 2;

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
#define TRIGGER_PIN 10 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 9 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {
Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
pixels.begin();
for (int i = 0; i < NUMPIXELS; i++) {
pixels.setPixelColor(i, pixels.Color(0, 255, 0));
pixels.show(); // Initialize all pixels to ‘off’
delay(100);
}
}

void loop() {
delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
distance = uS / US_ROUNDTRIP_CM;
Serial.println(distance); // Convert ping time to distance in cm and print result (0 = outside set distance range)

if (distance > 15 || distance == 0) {
for (int i = 0; i < 255; i++){
pixels.setPixelColor(i, pixels.Color(0, 0, 255));
pixels.show();

}
}

if ((distance > 5 && distance <= 15) && distance != 1149){ //want to set this so once active it remains for 10 seconds
rgbFadeInAndOut(0, 255, 0, ledFadeTime);
pixels.show();

// for (int i = 0; i < NUMPIXELS; i++) { (yellow circle ring for succeeded entry)
// pixels.setPixelColor(i, pixels.Color(255, 255, 0));
// pixels.show(); // Initialize all pixels to ‘off’
// delay(70);

}
}
void rgbFadeInAndOut(uint8_t red, uint8_t green, uint8_t blue, uint8_t wait) {
for(uint8_t b = 0; b <255; b++) {
for(uint8_t i=0; i < pixels.numPixels(); i++) {
pixels.setPixelColor(i, red * b/255, green * b/255, blue * b/255);
}

pixels.show();
delay(wait);
};

for(uint8_t b=255; b > 0; b–) {
for(uint8_t i = 0; i < pixels.numPixels(); i++) {
pixels.setPixelColor(i, red * b/255, green * b/255, blue * b/255);
}
pixels.show();
delay(wait);
};
};

 

IMG_0347.jpg