1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
#include <Adafruit_NeoPixel.h> #define LED_PIN 3 #define LED_COUNT 8 Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800); #define PIR 2 #define PIEZO A3 unsigned long cnt=0; int states=0; int pos=0; int colorR=0, colorG=0, colorB=0; int n=0; int notes[]={262, 294, 330, 349, 392, 440, 494, 523}; void setup() { Serial.begin(9600); strip.begin(); strip.show(); strip.setBrightness(50); pinMode(PIR, INPUT); } void loop() { int value=digitalRead(PIR); Serial.print("PIR : "); Serial.println(value); if (value==HIGH) { cnt=millis(); states=1; } if (cnt > 0 && millis() > cnt+5000) { cnt=0; states=0; } if (states==1) { randomSeed(analogRead(A5)); pos=random(0, 8); colorR=random(0, 256); colorG=random(0, 256); colorB=random(0, 256); n=random(0, 8); for (int i=0;i<strip.numPixels();i++) { if (i==pos) { strip.setPixelColor(i, strip.Color(colorR, colorG, colorB)); } else { strip.setPixelColor(i, strip.Color(0, 0, 0)); } } strip.show(); tone(PIEZO, notes[n]); delay(100); } else { colorWipe(strip.Color(0, 0, 0)); noTone(PIEZO); } } void colorWipe(uint32_t color) { for(int i=0;i < strip.numPixels();i++) { strip.setPixelColor(i, color); } strip.show(); } |