Arduino LED+ Distance sensor / PIR sensor

distance sensor + LED

Advantage: 相较于其他感应器,距离传感器可以捕捉它前方的物品和它之间的距离数据。因此我利用它捕捉观众和装置之间的距离,再通过coding根据距离数据调节LED亮度,亮度可以逐渐变化,变化速度可控。可以将“观众的手靠近该星球则该星球文明消失”这种交互关系,更形象地表达出来。

Disadvantage:感应器的感应区域小。手必须在感应器上方很小的范围内活动,才会被捕捉到信号;任何一些偏离,都会导致无法触发交互效果。所以,如果使用该感应器,为了保证关观众的交互体验感顺畅,则需要较多数量的该感应器。这又会带来一些电路连接上的复杂度。

#include <HCSR04.h>
// Initialize sensor that uses digital pins 13 and 12.
UltraSonicDistanceSensor distanceSensor(13, 12);

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
 #include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif

// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1:
#define LED_PIN    6

// How many NeoPixels are attached to the Arduino?
#define LED_COUNT 20

// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);

void setup () {
    Serial.begin(9600);  //initialize serial connection so that we could print values from sensor.
    pinMode(13, OUTPUT);

    #if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
  clock_prescale_set(clock_div_1);
  #endif
  // END of Trinket-specific code.

  strip.begin();           // INITIALIZE NeoPixel strip object (REQUIRED)
  strip.show();            // Turn OFF all pixels ASAP
  strip.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255)

}

void loop () {

    float distance = distanceSensor.measureDistanceCm();
    Serial.println(distance);
if (distance > 0) {
    if (distance < 30 ){
       digitalWrite(13, HIGH);
        for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
        strip.setPixelColor(i, 125,0,125);         //  Set pixel's color (in RAM)
         strip.show();
        }
      }else{
         digitalWrite(13, LOW);
         strip.clear();           // INITIALIZE NeoPixel strip object (REQUIRED)
         strip.show();
        }

  }
}

PIR +LED

Leave a Reply

Your email address will not be published. Required fields are marked *