Advertisement
thebys

Arduino Nano PIR Interupt RGB LED Showcase

Jun 1st, 2016
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.48 KB | None | 0 0
  1. //PIR RGB LED Showcaser random color switcher
  2. #include <Adafruit_NeoPixel.h>
  3. struct RGB {
  4.   byte r, g, b;
  5. };
  6.  
  7. //neopixel
  8. const int PIXEL_COUNT = 1;
  9. const int DATA_PIN = 3;
  10. Adafruit_NeoPixel pixels;
  11. RGB currentRGB = (RGB){0, 0, 0};
  12.  
  13. //PIR
  14. const int PIR_PIN = 2;
  15. volatile int PIR_TICKER = 0;
  16.  
  17. //inicializace randomizeru, serioveho portu, neopixelu a PIR cidla.
  18. void setup() {
  19.   randomSeed(analogRead(0));
  20.  
  21.   Serial.begin(9600);
  22.  
  23.   pixels = Adafruit_NeoPixel(PIXEL_COUNT, DATA_PIN, NEO_RGB + NEO_KHZ400);
  24.   pixels.begin();
  25.  
  26.   pinMode(PIR_PIN, INPUT);
  27.   attachInterrupt(digitalPinToInterrupt(PIR_PIN), PIRTriggered, RISING);
  28. }
  29.  
  30. //rozhoduje, co se bude dit, momentalne budnahodne meni barvy nebo idli
  31. void loop() {
  32.   while (PIR_TICKER > 0) {
  33.     switchRandomColor(random(12, 24));
  34.     PIR_TICKER--;
  35.     updateColor();
  36.   }
  37.   if (PIR_TICKER == 0) {
  38.     idleLED();
  39.   }
  40. }
  41.  
  42. //nastavi a zobrazi aktualni barvu na neopixelu a odesle zpravu pres seriovy port
  43. void updateColor() {
  44.   pixels.setPixelColor(0, currentRGB.g, currentRGB.r, currentRGB.b);
  45.   pixels.show();
  46.   serialUpdateColor();
  47. }
  48.  
  49. //cykli cervenou a zadnou barvu na ledce kdyz neni na praci nic zajimavejsiho
  50. void idleLED() {
  51.   currentRGB = {16,0,0};
  52.   updateColor();
  53.   delay(24);
  54.   currentRGB = {0,0,0};
  55.   updateColor();
  56.   delay(240);
  57. };
  58.  
  59. //Prepne na nahodnou barvu vyhovujici podminkam
  60. void switchRandomColor(int steps)
  61. {
  62.   int intensityDiff = 0;
  63.   int diffRed, diffGreen, diffBlue;
  64.   int targetRed, targetGreen, targetBlue;
  65.  
  66.   while (intensityDiff < random(256, 512)) {
  67.     targetRed = random(0, 255);
  68.     targetGreen = random(0, 255);
  69.     targetBlue = random(0, 255);
  70.     diffRed = targetRed - currentRGB.r;
  71.     diffGreen = targetGreen - currentRGB.g;
  72.     diffBlue = targetBlue - currentRGB.b;
  73.     intensityDiff = abs(diffRed) + abs(diffGreen) + abs(diffBlue);
  74.   }
  75.  
  76.   for (int i = steps; i > 0; i--) {
  77.     currentRGB.r += diffRed / steps;
  78.     currentRGB.g += diffGreen / steps;
  79.     currentRGB.b += diffBlue / steps;
  80.  
  81.     updateColor();
  82.     delay(48);
  83.   }
  84. }
  85.  
  86. //Odesle zpravu o soucasne barve a vysce tick query pres seriovy port
  87. void serialUpdateColor() {
  88.   String color = "Zbyva barev: ";
  89.   color += PIR_TICKER;
  90.   color += " --- Aktualni R";
  91.   color += currentRGB.r;
  92.   color += "-G";
  93.   color += currentRGB.g;
  94.   color += "-B";
  95.   color += currentRGB.b;
  96.   Serial.println(color);
  97. }
  98.  
  99. //obsluhuje PIR cidlo, pridava ticky do globalni promenne
  100. void PIRTriggered() {
  101.   PIR_TICKER += random(8, 16);
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement