Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /////// Code to controll an RGB LED strip. This will be mounted in a door frame to welcome me when I walk in.
- #include <FastLED.h>
- #define echoPin 7
- #define trigPin 8
- #define NUM_LEDS 45
- #define ledPin 12
- float brightness = 0;
- float hue = 0;
- float inc = 0;
- float hueInc = .3;
- float incSpeed;
- int goState = LOW; ///// YES MOTION
- int val = 0;
- long duration;
- long distance;
- long starttime;
- long endtime;
- CRGB leds[NUM_LEDS];
- void setup(){
- Serial.begin(9600);
- pinMode (echoPin, INPUT);
- pinMode (trigPin, OUTPUT);
- LEDS.addLeds<WS2812, ledPin, RGB>(leds, NUM_LEDS);
- LEDS.setBrightness(255);
- incSpeed = random(4, 6) / 100.0;
- clearAll();
- }
- void clearAll() {
- for (int i = 0; i < NUM_LEDS; i++) {
- leds[i] = CRGB( 0, 0, 0);
- delay(5);
- FastLED.show();
- }
- }
- // led code to run when motion is detected
- void animate() {
- creepBootUp();
- fancy(65, 15);
- }
- void readMotion() { ///////////////// Detects with an UltraSonic sensor if someone is close by
- digitalWrite(trigPin, LOW);
- delayMicroseconds(2);
- digitalWrite(trigPin, HIGH);
- delayMicroseconds(10);
- digitalWrite(trigPin,LOW);
- duration = pulseIn (echoPin, HIGH);
- //This gives us distance in cm
- distance = duration/58.2;
- Serial.println (distance);
- //Milliseconds
- delay(50);
- if (distance < 10){
- if(goState == LOW){
- animate();
- goState = HIGH;
- }
- }
- else {
- if (goState == HIGH) {
- clearAll();
- goState = LOW;
- }
- }
- }
- // do that gradual white fill from the middle
- void creepBootUp() {
- float start = NUM_LEDS / 2;
- float bright = 0;
- while (start < NUM_LEDS + 10) {
- start += .5;
- for (int i = NUM_LEDS / 2; i < NUM_LEDS; i++) {
- if (i <= start) {
- bright = (start - i) * 3;
- if (bright > 255) {
- bright = 255;
- }
- leds[i] = CRGB( bright, bright, bright);
- leds[NUM_LEDS / 2 - (i - NUM_LEDS / 2)] = CRGB( bright, bright, bright);
- }
- else {
- leds[i] = CRGB( 0, 0, 0);
- leds[NUM_LEDS / 2 - (i - NUM_LEDS / 2)] = CRGB( 0, 0, 0);
- }
- }
- FastLED.show();
- delay(7);
- }
- }
- void fancy(uint8_t thisSpeed, uint8_t deltaHue) {
- starttime = millis();
- endtime = starttime;
- while ((endtime - starttime) <=3000) // do this loop for up to 5000mS
- {
- uint8_t thisHue = beatsin8(thisSpeed,0,255); // A simple rainbow wave.
- //uint8_t thisHue = beat8(thisSpeed,255); // A simple rainbow march.
- fill_rainbow(leds, NUM_LEDS, thisHue, deltaHue);
- FastLED.show();
- endtime = millis();
- }
- // Use FastLED's fill_rainbow routine.
- }
- void loop() {
- readMotion();
- }
Advertisement
Add Comment
Please, Sign In to add comment