Advertisement
DuboisP

Distanciation / launch_video

Mar 22nd, 2022
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.19 KB | None | 0 0
  1. /*
  2.   author:   Dubois, Patrick
  3.   date:     2020 december
  4.   purpose : green, orange and red lights based on distance
  5.             more than 1.6 m green
  6.             between 1.0 and 1.6 m blink orange
  7.             less than 1.0 m red
  8.             distance computed by ultrasonic sensor
  9.   colors table: https://www.rapidtables.com/web/color/index.html
  10. */
  11.  
  12. #define elego_lib
  13. // #define hcsr04_lib
  14.  
  15. #ifdef elego_lib
  16. #include <SR04.h>             // ultrasonic sensor library
  17. #endif
  18.  
  19. #ifdef hcsr04_lib
  20. #include <HCSR04.h>           // ultrasonic sensor library
  21. #endif
  22.  
  23. #include <Adafruit_NeoPixel.h>
  24. #ifdef __AVR__
  25. #include <avr/power.h> // Required for 16 MHz Adafruit Trinket
  26. #endif
  27.  
  28. #define PIXEL_COUNT   24      // Number of NeoPixels
  29.  
  30. #define PIXEL_PIN     11      // Digital IO pin connected to the NeoPixels.
  31. #define TRIG_PIN       7      // Trigger
  32. #define ECHO_PIN       9      // Echo receiver
  33.  
  34. #define RED_DISTANCE    100
  35. #define ORANGE_DISTANCE 160
  36.  
  37. // Declare our NeoPixel strip object:
  38. Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);
  39. // Argument 1 = Number of pixels in NeoPixel strip
  40. // Argument 2 = Arduino pin number (most are valid)
  41. // Argument 3 = Pixel type flags, add together as needed:
  42. //   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
  43. //   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
  44. //   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
  45. //   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
  46. //   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
  47.  
  48. int iNormalBrightness = 31;
  49. int iReductedBrightness = 15;
  50.  
  51. #ifdef elego_lib
  52. SR04 sr04 = SR04(ECHO_PIN, TRIG_PIN);
  53. long distance;
  54. #endif
  55. #ifdef hcsr04_lib
  56. double distance;
  57. #endif
  58.  
  59.  
  60. void setup() {
  61.  
  62.   strip.begin();                // Initialize NeoPixel strip object (REQUIRED)
  63.   strip.show();                 // Initialize all pixels to 'off'
  64.   Serial.begin(9600);
  65.  
  66. #ifdef hcsr04_lib
  67.   HCSR04.begin(TRIG_PIN, ECHO_PIN);
  68. #endif
  69. }
  70.  
  71.  
  72. void loop() {
  73.  
  74. #ifdef elego_lib
  75.   distance = sr04.Distance();
  76. #endif
  77. #ifdef hcsr04_lib
  78.   double* distances = HCSR04.measureDistanceCm();
  79.   distance = distances[0];
  80. #endif
  81.  
  82.   // Serial.println(String(distance) + " cm");
  83.   // Colors are RGB
  84.   if (distance == 0) {                            // Time-out
  85.     colorWipe(strip.Color(0, 0, 0), 50);          // Black/off
  86.   }
  87.   else if (distance < RED_DISTANCE) {
  88.     strip.setBrightness(iNormalBrightness);
  89.     colorWipe(strip.Color(255, 0, 0), 50);        // red light
  90.     Serial.println("LAUNCH_VIDEO");
  91.   }
  92.   else if (distance < ORANGE_DISTANCE) {          // blinking orange light
  93.     // theaterChase(strip.Color(255, 140, 0), 50);
  94.     strip.setBrightness(iReductedBrightness);
  95.     arrow(0, PIXEL_COUNT, strip.Color(255, 165, 0), 10, 10); // Orange
  96.   }
  97.   else if (distance < 400) {                      // measure limit
  98.     strip.setBrightness(iNormalBrightness);
  99.     colorWipe(strip.Color(0, 128, 0), 50);        // green light
  100.   }
  101.   delay(500);
  102. }
  103.  
  104.  
  105. void arrow(int l_first, int l_last, uint32_t color, int l_repeat, int wait) {
  106.  
  107.   for (int j = 0; j < l_repeat; j++) {
  108.     strip.clear();
  109.     for (int i = l_first; i < l_last; i++) { // For each pixel in strip...
  110.       strip.setPixelColor(i, color);         //  Set pixel's color (in RAM)
  111.       strip.show();                          //  Update strip to match
  112.       delay(wait);                           //  Pause for a moment
  113.     }
  114.   }
  115. }
  116.  
  117.  
  118. // Fill strip pixels one after another with a color. Strip is NOT cleared
  119. // first; anything there will be covered pixel by pixel. Pass in color
  120. // (as a single 'packed' 32-bit value, which you can get by calling
  121. // strip.Color(red, green, blue) as shown in the loop() function above),
  122. // and a delay time (in milliseconds) between pixels.
  123. void colorWipe(uint32_t color, int wait) {
  124.  
  125.   for (int i = 0; i < strip.numPixels(); i++) { // For each pixel in strip...
  126.     strip.setPixelColor(i, color);         //  Set pixel's color (in RAM)
  127.     strip.show();                          //  Update strip to match
  128.     delay(wait);                           //  Pause for a moment
  129.   }
  130. }
  131.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement