/* * Posted on https://randomnerdtutorials.com * created by http://playground.arduino.cc/Code/NewPing */ #include #include #include #define ECHO_PIN 12 #define TRIGGER_PIN 13 #define MAX_DISTANCE 100 //This is where we adjust things to match our unique project: #define NUM_STRIPS 3 #define NUM_LEDS_PER_STRIP 9 #define NUM_LEDS NUM_LEDS_PER_STRIP * NUM_STRIPS #define BRIGHTNESS 255 // 255 is full brightness, 128 is half #define SATURATION 255 // 0-255, 0 is pure white, 255 is fully saturated color float sinVal; int ledVal; // NewPing setup of pins and maximum distance NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); //int db[] = {5,6,4,7,3,8,2,9,1,10,0,11,15,12,14,13, // 21,22,20,23,19,24,18,25,17,26,16,27,31,28,30,29, // 33,34,32,35,47,36,46,37,45,38,44,39,43,40,42,41}; //CRGB leds[NUM_LEDS]; CRGBArray leds; void setup() { delay( 2000 ); // power-up safety delay // Assign pin 3, 5 and 6 as LED singal pin FastLED.addLeds(leds, 0, NUM_LEDS_PER_STRIP); FastLED.addLeds(leds, NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP); FastLED.addLeds(leds, NUM_LEDS_PER_STRIP*2, NUM_LEDS_PER_STRIP); FastLED.setBrightness( BRIGHTNESS ); } void loop() { unsigned int distance = sonar.ping_cm(); static uint8_t hue=0; leds.fill_rainbow(hue++); FastLED.delay(30); if(hue == 255) { hue = 0; } if (distance > 0 && distance < 12){ fill_breath(NUM_LEDS_PER_STRIP*2,NUM_LEDS_PER_STRIP*3-1); } if (distance > 14 && distance < 26){ fill_breath(NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP*2-1); } if (distance > 30 && distance < 42){ fill_breath(0,NUM_LEDS_PER_STRIP-1); } FastLED.show(); FastLED.delay(10); } void fill_breath(int A,int B) { for(int i = 0; i < 90; i++){ sinVal = sin(i*(PI/180)); ledVal = int(sinVal*255); leds(A,B).fadeLightBy(ledVal); //leds(A,B) = CHSV(128,255,ledVal); FastLED.show(); FastLED.delay(10); } }