The software:
The parts:
1 - Arduino Due
1 - SunFounder Project Super Starter Kit with Tutorial Book for Arduino
5 - NooElec 1m Addressable RGB LED Strip
1 - Kuman 5pcs Hc-sr04 Ultrasonic Distance Measuring Sensor
1 - DC Power Jack
4 - Male to Female JST SM Plug Connector Wire LED Light
1 - RGB Extension Cable Line for LED Strip
1 - Weatherproof Connection Box
The code:
#include "FastLED.h"
// Data pin that led data will be written out over
#define data_pin 12
// Clock pin only needed for SPI based chipsets when not using hardware SPI
#define clock_pin 13
// How many leds are in the led strip? Which leds start each strip?
#define num_leds 160
#define light_leds 32
#define start_led1 0
#define start_led2 32
#define start_led3 64
#define start_led4 96
#define start_led5 128
// led special effects variables
#define ledeffecteyesize 8
#define ledeffectspeeddelay 10
#define ledeffectreturndelay 20
// ultrosonic sensor variables
#define maxdistance 200
#define statemaxcount 4
#define soundoffdelay 20
#define soundondelay 80
// Which pins are used for the ultrosonic sensors
#define trigPin1 22
#define echoPin1 23
#define trigPin2 24
#define echoPin2 25
#define trigPin3 26
#define echoPin3 27
#define trigPin4 28
#define echoPin4 29
#define trigPin5 30
#define echoPin5 31
int color1 = 1;
int set1 = 0;
int state1close = 0;
int state1far = 0;
int color2 = 2;
int set2 = 0;
int state2close = 0;
int state2far = 0;
int color3 = 3;
int set3 = 0;
int state3close = 0;
int state3far = 0;
int color4 = 4;
int set4 = 0;
int state4close = 0;
int state4far = 0;
int color5 = 5;
int set5 = 0;
int state5close = 0;
int state5far = 0;
// This is an array of leds. One item for each led in your strip.
CRGB leds[num_leds];
// This function sets up the leds and tells the controller about them
void setup() {
Serial.begin (9600);
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
// pinMode(trigPin2, OUTPUT);
// pinMode(echoPin2, INPUT);
// pinMode(trigPin3, OUTPUT);
// pinMode(echoPin3, INPUT);
// pinMode(trigPin4, OUTPUT);
// pinMode(echoPin4, INPUT);
// pinMode(trigPin5, OUTPUT);
// pinMode(echoPin5, INPUT);
FastLED.addLeds<WS2801, data_pin, clock_pin, RGB>(leds, num_leds);
}
// This function runs over and over, and is where you do the magic to light
// your leds.
void loop() {
long distance1, distance2, distance3, distance4, distance5;
distance1 = getdistance(trigPin1, echoPin1, distance1);
// distance2 = getdistance(trigPin2, echoPin2, distance2);
// distance3 = getdistance(trigPin3, echoPin3, distance3);
// distance4 = getdistance(trigPin4, echoPin4, distance4);
// distance5 = getdistance(trigPin5, echoPin5, distance5);
Serial.print(distance1);
Serial.println(" cm1");
if (distance1 < maxdistance) {
// since the sensor gives false positives, this function counts the number of times the sensor enters the close state
state1close = setstate (state1close);
if (state1close == statemaxcount && set1 == 0) {
// sets the color for each led strip
color1 = setledcolor (color1, start_led5);
color2 = setledcolor (color2, start_led4);
color3 = setledcolor (color3, start_led3);
color4 = setledcolor (color4, start_led2);
color5 = setledcolor (color5, start_led1);
set1 = 1;
state1close = 0;
}
state1far = 0;
} else {
// since the sensor gives false positives, this function counts the number of times the sensor enters the far state
state1far = setstate (state1far);
if (state1far == statemaxcount && set1 == 1) {
set1 = 0;
state1far = 0;
}
state1close = 0;
}
/*
if (distance2 < maxdistance) {
state2close = setstate (state2close);
if (state2close == 2 && set2 == 0) {
color2 = setledcolor (color2, start_led2);
set2 = 1;
state2close = 0;
}
state2far = 0;
} else {
state2far = setstate (state2far);
if (state2far == 2 && set2 == 1) {
set2 = 0;
state2far = 0;
}
state2close = 0;
}
if (distance3 < maxdistance) {
state3close = setstate (state3close);
if (state3close == 2 && set3 == 0) {
color3 = setledcolor (color3, start_led3);
set3 = 1;
state3close = 0;
}
state3far = 0;
} else {
state3far = setstate (state3far);
if (state3far == 2 && set3 == 1) {
set3 = 0;
state3far = 0;
}
state3close = 0;
}
if (distance4 < maxdistance) {
state4close = setstate (state4close);
if (state4close == statemaxcount && set4 == 0) {
color4 = setledcolor (color4, start_led4);
set4 = 1;
state4close = 0;
}
state4far = 0;
} else {
state4far = setstate (state4far);
if (state4far == statemaxcount && set4 == 1) {
set4 = 0;
state4far = 0;
}
state4close = 0;
}
if (distance5 < maxdistance) {
state5close = setstate (state5close);
if (state5close == 5 && set5 == 0) {
color5 = setledcolor (color5, start_led5);
set5 = 1;
state5close = 0;
}
state5far = 0;
} else {
state5far = setstate (state5far);
if (state5far == 5 && set5 == 1) {
set5 = 0;
state5far = 0;
}
state5close = 0;
}
*/
}
long getdistance (int trigPin, int echoPin, long distance) {
long duration;
// turns the ultrasonic sensor off, on, then off. Distance is calculated based on the time it takes for the sound to echo.
digitalWrite(trigPin, LOW); // Added this line
delayMicroseconds(soundoffdelay); // Added this line
digitalWrite(trigPin, HIGH);
delayMicroseconds(soundondelay); // Added this line
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
return distance = (duration/2) / 29.1;
}
int setledcolor (int color, int start_led) {
if (color == 1) {
ledeffect(0xFF, 0x00, 0x00, start_led);
color = 7;
} else if (color == 2) {
ledeffect(0x00, 0xFF, 0x00, start_led);
color = 1;
} else if (color == 3) {
ledeffect(0x00, 0x00, 0xFF, start_led);
color = 2;
} else if (color == 4) {
ledeffect(0x00, 0xFF, 0xFF, start_led);
color = 3;
} else if (color == 5) {
ledeffect(0xFF, 0x00, 0xFF, start_led);
color = 4;
} else if (color == 6) {
ledeffect(0xFF, 0xA5, 0x00, start_led);
color = 5;
} else if (color == 7) {
ledeffect(0xFF, 0xFF, 0xFF, start_led);
color = 6;
}
return color;
}
int setstate (int state) {
if (state < statemaxcount) {
state = state + 1;
}
return state;
}
void lightleds (byte red, byte green, byte blue, int start_led) {
// Move a single white led
for (int lednum = start_led; lednum < light_leds + start_led; lednum = lednum + 1) {
// Turn our current led on to white, then show the leds
// leds[lednum] = CRGB::Green;
setpixel(lednum, red, green, blue);
// Show the leds (only one of which is set to white, from above)
FastLED.show();
// Wait a little bit
// delay(50);
// Turn our current led back to black for the next loop around
// leds[lednum] = CRGB::Black;
}
}
void ledeffect(byte red, byte green, byte blue, int start_led){
righttoleft(red, green, blue, start_led);
lefttoright(red, green, blue, start_led);
// outsidetocenter(red, green, blue, start_led);
// CenterToOutside(red, green, blue, start_led);
lightleds(red, green, blue, start_led);
}
void CenterToOutside(byte red, byte green, byte blue, int start_led) {
for(int i =((light_leds+start_led-ledeffecteyesize)/2); i>=start_led; i--) {
// setAll(0,0,0);
setpixel(i, red/10, green/10, blue/10);
for(int j = 1; j <= ledeffecteyesize; j++) {
setpixel(i+j, red, green, blue);
}
setpixel(i+ledeffecteyesize+1, red/10, green/10, blue/10);
setpixel(light_leds-i, red/10, green/10, blue/10);
for(int j = 1; j <= ledeffecteyesize; j++) {
setpixel(light_leds-i-j, red, green, blue);
}
setpixel(light_leds-i-ledeffecteyesize-1, red/10, green/10, blue/10);
FastLED.show();
delay(ledeffectspeeddelay);
}
delay(ledeffectreturndelay);
}
void outsidetocenter(byte red, byte green, byte blue, int start_led) {
for(int i = start_led; i<=((light_leds+start_led-ledeffecteyesize)/2); i++) {
// setAll(0,0,0);
setpixel(i, red/10, green/10, blue/10);
for(int j = 1; j <= ledeffecteyesize; j++) {
setpixel(i+j, red, green, blue);
}
setpixel(i+ledeffecteyesize+1, red/10, green/10, blue/10);
setpixel(light_leds-i, red/10, green/10, blue/10);
for(int j = 1; j <= ledeffecteyesize; j++) {
setpixel(light_leds-i-j, red, green, blue);
}
setpixel(light_leds-i-ledeffecteyesize-1, red/10, green/10, blue/10);
FastLED.show();
delay(ledeffectspeeddelay);
}
delay(ledeffectreturndelay);
}
void lefttoright(byte red, byte green, byte blue, int start_led) {
for(int i = start_led; i < light_leds+start_led-ledeffecteyesize-2; i++) {
// setAll(0,0,0);
setpixel(i, red/10, green/10, blue/10);
for(int j = 1; j <= ledeffecteyesize; j++) {
setpixel(i+j, red, green, blue);
}
setpixel(i+ledeffecteyesize+1, red/10, green/10, blue/10);
FastLED.show();
delay(ledeffectspeeddelay);
}
delay(ledeffectreturndelay);
}
void righttoleft(byte red, byte green, byte blue, int start_led) {
for(int i = light_leds+start_led-ledeffecteyesize-2; i > start_led; i--) {
// setAll(0,0,0);
setpixel(i, red/10, green/10, blue/10);
for(int j = 1; j <= ledeffecteyesize; j++) {
setpixel(i+j, red, green, blue);
}
setpixel(i+ledeffecteyesize+1, red/10, green/10, blue/10);
FastLED.show();
delay(ledeffectspeeddelay);
}
delay(ledeffectreturndelay);
}
void setpixel(int Pixel, byte red, byte green, byte blue) {
// FastLED
leds[Pixel].r = red;
leds[Pixel].g = green;
leds[Pixel].b = blue;
}
Additional special effects code:
https://www.tweaking4all.com/hardware/arduino/adruino-led-strip-effects/