cathy Li
Published

Intelligent lighting

Make your life smarter and easier

BeginnerFull instructions provided5 hours165
Intelligent lighting

Things used in this project

Story

Read more

Code

how to meet it

C/C++
#include "Ultrasonic.h"
#include "Adafruit_NeoPixel.h"
#ifdef __AVR__
  #include <avr/power.h>
#endif
 
// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1
#define PIN            5
 
// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS      60
 
// When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals.
// Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest
// example for more information on possible values.
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
 
Ultrasonic ultrasonic(7);
void setup()
{
    Serial.begin(9600);
    pixels.setBrightness(0);
    pixels.begin(); // This initializes the NeoPixel library.
    
}
void loop()
{
    long RangeInCentimeters; 
    RangeInCentimeters = ultrasonic.MeasureInCentimeters(); // two measurements should keep an interval
    Serial.print(RangeInCentimeters);//0~400cm
    Serial.println(" cm");
    if (RangeInCentimeters > 100) 
    {
      // 如果距离大于100cm
      pixels.setBrightness(0); // turn off led
      for(int i=0;i<NUMPIXELS;i++) {
        // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
        pixels.setPixelColor(i, pixels.Color(255,255,255)); // Moderately bright green color.
        pixels.show(); // This sends the updated pixel color to the hardware.    
      }
      Serial.println("turn off led");
    }
    else
    {
      // 如果距离小于100cm
      pixels.setBrightness(255); // turn on led
      for(int i=0;i<NUMPIXELS;i++) {
        // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
        pixels.setPixelColor(i, pixels.Color(255,255,255)); // Moderately bright green color.
        pixels.show(); // This sends the updated pixel color to the hardware.    
      }
      Serial.println("turn on led");
      delay(10 * 1000); // 10 seconds
    }

    delay(250); // next detection, 250 ms
}

Credits

cathy Li
1 project • 2 followers

Comments