LEDPROJECT69420

Untitled

Aug 21st, 2020
900
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.67 KB | None | 0 0
  1. /////// Code to controll an RGB LED strip. This will be mounted in a door frame to welcome me when I walk in.
  2.  
  3. #include <FastLED.h>
  4.  
  5. #define echoPin 7
  6. #define trigPin 8
  7. #define NUM_LEDS 45
  8. #define ledPin 12
  9.  
  10. float brightness = 0;
  11. float hue = 0;
  12. float inc = 0;
  13. float hueInc = .3;
  14. float incSpeed;
  15. int goState = LOW; ///// YES MOTION
  16. int val = 0;
  17. long duration;
  18. long distance;
  19. long starttime;
  20. long endtime;
  21.  
  22. CRGB leds[NUM_LEDS];
  23.  
  24. void setup(){
  25.   Serial.begin(9600);
  26.   pinMode (echoPin, INPUT);
  27.   pinMode (trigPin, OUTPUT);
  28.   LEDS.addLeds<WS2812, ledPin, RGB>(leds, NUM_LEDS);
  29.   LEDS.setBrightness(255);
  30.   incSpeed = random(4, 6) / 100.0;
  31.   clearAll();
  32. }
  33.  
  34. void clearAll() {
  35.   for (int i = 0; i < NUM_LEDS; i++) {
  36.     leds[i] = CRGB( 0, 0, 0);
  37.     delay(5);
  38.     FastLED.show();
  39.   }
  40. }
  41.  
  42. // led code to run when motion is detected
  43. void animate() {
  44.   creepBootUp();
  45.   fancy(65, 15);
  46. }
  47.  
  48.  
  49.  
  50. void readMotion() { ///////////////// Detects with an UltraSonic sensor if someone is close by
  51.  digitalWrite(trigPin, LOW);
  52.  delayMicroseconds(2);
  53.  digitalWrite(trigPin, HIGH);
  54.  delayMicroseconds(10);
  55.  digitalWrite(trigPin,LOW);
  56.  duration = pulseIn (echoPin, HIGH);
  57.  //This gives us distance in cm
  58.  distance = duration/58.2;
  59.  Serial.println (distance);
  60.  //Milliseconds
  61.  delay(50);
  62.  if (distance < 10){
  63.   if(goState == LOW){
  64.     animate();
  65.     goState = HIGH;
  66.   }
  67.   }
  68.   else {
  69.     if (goState == HIGH) {
  70.       clearAll();
  71.       goState = LOW;
  72.     }
  73.   }
  74. }
  75.  
  76. // do that gradual white fill from the middle
  77. void creepBootUp() {
  78.   float start = NUM_LEDS / 2;
  79.   float bright = 0;
  80.   while (start < NUM_LEDS + 10) {
  81.     start += .5;
  82.     for (int i = NUM_LEDS / 2; i < NUM_LEDS; i++) {
  83.       if (i <= start) {
  84.         bright = (start - i) * 3;
  85.         if (bright > 255) {
  86.           bright = 255;
  87.         }
  88.         leds[i] = CRGB( bright, bright, bright);
  89.         leds[NUM_LEDS / 2 - (i - NUM_LEDS / 2)] = CRGB( bright, bright, bright);
  90.       }
  91.       else {
  92.         leds[i] = CRGB( 0, 0, 0);
  93.         leds[NUM_LEDS / 2 - (i - NUM_LEDS / 2)] = CRGB( 0, 0, 0);
  94.       }
  95.     }
  96.     FastLED.show();
  97.     delay(7);
  98.   }
  99. }
  100.  
  101. void fancy(uint8_t thisSpeed, uint8_t deltaHue) {    
  102. starttime = millis();
  103. endtime = starttime;
  104. while ((endtime - starttime) <=3000) // do this loop for up to 5000mS
  105. {
  106. uint8_t thisHue = beatsin8(thisSpeed,0,255);                // A simple rainbow wave.
  107.  //uint8_t thisHue = beat8(thisSpeed,255);                     // A simple rainbow march.
  108.  
  109.  fill_rainbow(leds, NUM_LEDS, thisHue, deltaHue);
  110.  FastLED.show();
  111. endtime = millis();
  112. }
  113.             // Use FastLED's fill_rainbow routine.
  114.  
  115. }
  116.  
  117. void loop() {
  118.   readMotion();
  119. }
  120.  
Advertisement
Add Comment
Please, Sign In to add comment