Zero To One

현관문 프로젝트) LED Strip + Door sensor + Arduino 2편 본문

토이프로젝트

현관문 프로젝트) LED Strip + Door sensor + Arduino 2편

Zero_To_One 2022. 9. 16. 17:50

1. Door sensor를 이용한 회로 구성

 

2. 코드

fastLED라이브러리 설치

#include "FastLED.h"
#define NUM_LEDS 100 
CRGB leds[NUM_LEDS];
#define PIN 5 

#define DOOR_SENSOR_PIN 2
int doorState;

void setup()
{
  FastLED.addLeds<WS2812, PIN, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  pinMode(2,INPUT_PULLUP);  // internal pull-up resistor
}

void loop() {
  doorState = digitalRead(DOOR_SENSOR_PIN);
  if (doorState == HIGH) {
    NewKITT(100, 255, 100, 25, 10, 50); // red green blue
    delay(10000);
  }
  else{
    setAll(0,0,0);
  }
}

void NewKITT(byte red, byte green, byte blue, int EyeSize, int SpeedDelay, int ReturnDelay){
  OutsideToCenter(red, green, blue, EyeSize, SpeedDelay, ReturnDelay);
}

void OutsideToCenter(byte red, byte green, byte blue, int EyeSize, int SpeedDelay, int ReturnDelay) {
  for(int i = 0; i<=((NUM_LEDS-EyeSize)/2); i++) {
    setAll(0,0,0);
   
    setPixel(i, red/10, green/10, blue/10);
    for(int j = 1; j <= EyeSize; j++) {
      setPixel(i+j, red, green, blue);
    }
    setPixel(i+EyeSize+1, red/10, green/10, blue/10);
   
    setPixel(NUM_LEDS-i, red/10, green/10, blue/10);
    for(int j = 1; j <= EyeSize; j++) {
      setPixel(NUM_LEDS-i-j, red, green, blue);
    }
    setPixel(NUM_LEDS-i-EyeSize-1, red/10, green/10, blue/10);
   
    showStrip();
    delay(SpeedDelay);
  }
  delay(ReturnDelay);
}

void showStrip() {
 #ifdef ADAFRUIT_NEOPIXEL_H 
   // NeoPixel
   strip.show();
 #endif
 #ifndef ADAFRUIT_NEOPIXEL_H
   // FastLED
   FastLED.show();
 #endif
}

void setPixel(int Pixel, byte red, byte green, byte blue) {
 #ifdef ADAFRUIT_NEOPIXEL_H 
   // NeoPixel
   strip.setPixelColor(Pixel, strip.Color(red, green, blue));
 #endif
 #ifndef ADAFRUIT_NEOPIXEL_H 
   // FastLED
   leds[Pixel].r = red;
   leds[Pixel].g = green;
   leds[Pixel].b = blue;
 #endif
}

void setAll(byte red, byte green, byte blue) {
  for(int i = 0; i < NUM_LEDS; i++ ) {
    setPixel(i, red, green, blue); 
  }
  showStrip();
}

참조 : https://forum.arduino.cc/t/programming-led-light-strip-using-fastled/557227/4

 

Programming LED Light strip using fastLED

Let me try this again this is the first time i have ever posted to this so for that i am sorry… here are the two codes that i am trying to combine into 1 sketch and upload and get to play together… i guess i just don’t understand where i need to plac

forum.arduino.cc

https://www.tweaking4all.com/hardware/arduino/arduino-ws2812-led/

 

Arduino - Controlling a WS2812 LED strand with NeoPixel or FastLED

Getting started with Arduino and a WS2811 or WS2812 LED strand using NEOPixel by AdaFruit or FastSPI_LED (FastLED).

www.tweaking4all.com

 

3. 동작

가운데로 모여드는 동작

https://youtu.be/EwgBiirj09k

 

 

 

4. 기타동작

유성우 

#include "FastLED.h"
#define NUM_LEDS 100 
CRGB leds[NUM_LEDS];
#define PIN 5 

#define DOOR_SENSOR_PIN 2
int doorState;

void setup()
{
  FastLED.addLeds<WS2812, PIN, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  pinMode(2,INPUT_PULLUP);  // internal pull-up resistor
}

void loop() {
  doorState = digitalRead(DOOR_SENSOR_PIN);
  if (doorState == HIGH) {
    meteorRain(205,215,100,10, 64, true, 30);
  }
  else{
    setAll(0,0,0);
  }
}

void meteorRain(byte red, byte green, byte blue, byte meteorSize, byte meteorTrailDecay, boolean meteorRandomDecay, int SpeedDelay) {  
  setAll(0,0,0);
 
  for(int i = 0; i < NUM_LEDS+NUM_LEDS; i++) {
   
   
    // fade brightness all LEDs one step
    for(int j=0; j<NUM_LEDS; j++) {
      if( (!meteorRandomDecay) || (random(10)>5) ) {
        fadeToBlack(j, meteorTrailDecay );        
      }
    }
   
    // draw meteor
    for(int j = 0; j < meteorSize; j++) {
      if( ( i-j <NUM_LEDS) && (i-j>=0) ) {
        setPixel(i-j, red, green, blue);
      }
    }
   
    showStrip();
    delay(SpeedDelay);
  }
}

void fadeToBlack(int ledNo, byte fadeValue) {
 #ifdef ADAFRUIT_NEOPIXEL_H
    // NeoPixel
    uint32_t oldColor;
    uint8_t r, g, b;
    int value;
   
    oldColor = strip.getPixelColor(ledNo);
    r = (oldColor & 0x00ff0000UL) >> 16;
    g = (oldColor & 0x0000ff00UL) >> 8;
    b = (oldColor & 0x000000ffUL);

    r=(r<=10)? 0 : (int) r-(r*fadeValue/256);
    g=(g<=10)? 0 : (int) g-(g*fadeValue/256);
    b=(b<=10)? 0 : (int) b-(b*fadeValue/256);
   
    strip.setPixelColor(ledNo, r,g,b);
 #endif
 #ifndef ADAFRUIT_NEOPIXEL_H
   // FastLED
   leds[ledNo].fadeToBlackBy( fadeValue );
 #endif  
}

void showStrip() {
 #ifdef ADAFRUIT_NEOPIXEL_H 
   // NeoPixel
   strip.show();
 #endif
 #ifndef ADAFRUIT_NEOPIXEL_H
   // FastLED
   FastLED.show();
 #endif
}

void setPixel(int Pixel, byte red, byte green, byte blue) {
 #ifdef ADAFRUIT_NEOPIXEL_H 
   // NeoPixel
   strip.setPixelColor(Pixel, strip.Color(red, green, blue));
 #endif
 #ifndef ADAFRUIT_NEOPIXEL_H 
   // FastLED
   leds[Pixel].r = red;
   leds[Pixel].g = green;
   leds[Pixel].b = blue;
 #endif
}

void setAll(byte red, byte green, byte blue) {
  for(int i = 0; i < NUM_LEDS; i++ ) {
    setPixel(i, red, green, blue); 
  }
  showStrip();
}

 

https://youtu.be/g0TGRMajVyI