#include <Adafruit_NeoPixel.h>
#define PIXEL_PIN 8
#define PIXEL_COUNT 20
#define PIN_TRIG 3
#define PIN_ECHO 2
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);
// Parameter 1 = number of pixels in strip, neopixel stick has 8
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_RGB Pixels are wired for RGB bitstream
// NEO_GRB Pixels are wired for GRB bitstream, correct for neopixel stick
// NEO_KHZ400 400 KHz bitstream (e.g. FLORA pixels)
// NEO_KHZ800 800 KHz bitstream (e.g. High Density LED strip), correct for neopixel stick
void setup() {
//setup ring
strip.begin();
strip.setBrightness(255);
//setup ultrasonic
Serial.begin(115200);
pinMode(PIN_TRIG, OUTPUT);
pinMode(PIN_ECHO, INPUT);
}
void loop() {
digitalWrite(PIN_TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(PIN_TRIG, LOW);
int duration = pulseIn(PIN_ECHO, HIGH);
int distance = duration / 58;
strip.clear(); //clear ring
//ring distance
if (distance < 404){
for(int i=0; i<3; i++){
strip.setPixelColor(i, strip.Color(218, 247, 166));
}
}
if (distance < 330){
for(int i=3; i<6; i++){
strip.setPixelColor(i, strip.Color(255, 195, 0));
}
}
if (distance < 260){
for(int i=6; i<9; i++){
strip.setPixelColor(i, strip.Color(255, 87, 51));
}
}
if (distance < 190){
for(int i=9; i<12; i++){
strip.setPixelColor(i, strip.Color(199, 0, 57));
}
}
if (distance < 120){
for(int i=12; i<(14); i++){
strip.setPixelColor(i, strip.Color(144, 12, 63));
}
}
if (distance < 70){
for(int i=14; i<16; i++){
strip.setPixelColor(i, strip.Color(88, 24, 69));
}
}
strip.show();
}