Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- author: Dubois, Patrick
- date: 2020 december
- purpose : green, orange and red lights based on distance
- more than 1.6 m green
- between 1.0 and 1.6 m blink orange
- less than 1.0 m red
- distance computed by ultrasonic sensor
- colors table: https://www.rapidtables.com/web/color/index.html
- */
- #define elego_lib
- // #define hcsr04_lib
- #ifdef elego_lib
- #include <SR04.h> // ultrasonic sensor library
- #endif
- #ifdef hcsr04_lib
- #include <HCSR04.h> // ultrasonic sensor library
- #endif
- #include <Adafruit_NeoPixel.h>
- #ifdef __AVR__
- #include <avr/power.h> // Required for 16 MHz Adafruit Trinket
- #endif
- #define PIXEL_COUNT 24 // Number of NeoPixels
- #define PIXEL_PIN 11 // Digital IO pin connected to the NeoPixels.
- #define TRIG_PIN 7 // Trigger
- #define ECHO_PIN 9 // Echo receiver
- #define RED_DISTANCE 100
- #define ORANGE_DISTANCE 160
- // Declare our NeoPixel strip object:
- Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);
- // Argument 1 = Number of pixels in NeoPixel strip
- // Argument 2 = Arduino pin number (most are valid)
- // Argument 3 = Pixel type flags, add together as needed:
- // NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
- // NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
- // NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
- // NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
- // NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
- int iNormalBrightness = 31;
- int iReductedBrightness = 15;
- #ifdef elego_lib
- SR04 sr04 = SR04(ECHO_PIN, TRIG_PIN);
- long distance;
- #endif
- #ifdef hcsr04_lib
- double distance;
- #endif
- void setup() {
- strip.begin(); // Initialize NeoPixel strip object (REQUIRED)
- strip.show(); // Initialize all pixels to 'off'
- Serial.begin(9600);
- #ifdef hcsr04_lib
- HCSR04.begin(TRIG_PIN, ECHO_PIN);
- #endif
- }
- void loop() {
- #ifdef elego_lib
- distance = sr04.Distance();
- #endif
- #ifdef hcsr04_lib
- double* distances = HCSR04.measureDistanceCm();
- distance = distances[0];
- #endif
- // Serial.println(String(distance) + " cm");
- // Colors are RGB
- if (distance == 0) { // Time-out
- colorWipe(strip.Color(0, 0, 0), 50); // Black/off
- }
- else if (distance < RED_DISTANCE) {
- strip.setBrightness(iNormalBrightness);
- colorWipe(strip.Color(255, 0, 0), 50); // red light
- Serial.println("LAUNCH_VIDEO");
- }
- else if (distance < ORANGE_DISTANCE) { // blinking orange light
- // theaterChase(strip.Color(255, 140, 0), 50);
- strip.setBrightness(iReductedBrightness);
- arrow(0, PIXEL_COUNT, strip.Color(255, 165, 0), 10, 10); // Orange
- }
- else if (distance < 400) { // measure limit
- strip.setBrightness(iNormalBrightness);
- colorWipe(strip.Color(0, 128, 0), 50); // green light
- }
- delay(500);
- }
- void arrow(int l_first, int l_last, uint32_t color, int l_repeat, int wait) {
- for (int j = 0; j < l_repeat; j++) {
- strip.clear();
- for (int i = l_first; i < l_last; i++) { // For each pixel in strip...
- strip.setPixelColor(i, color); // Set pixel's color (in RAM)
- strip.show(); // Update strip to match
- delay(wait); // Pause for a moment
- }
- }
- }
- // Fill strip pixels one after another with a color. Strip is NOT cleared
- // first; anything there will be covered pixel by pixel. Pass in color
- // (as a single 'packed' 32-bit value, which you can get by calling
- // strip.Color(red, green, blue) as shown in the loop() function above),
- // and a delay time (in milliseconds) between pixels.
- void colorWipe(uint32_t color, int wait) {
- for (int i = 0; i < strip.numPixels(); i++) { // For each pixel in strip...
- strip.setPixelColor(i, color); // Set pixel's color (in RAM)
- strip.show(); // Update strip to match
- delay(wait); // Pause for a moment
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement