Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //PIR RGB LED Showcaser random color switcher
- #include <Adafruit_NeoPixel.h>
- struct RGB {
- byte r, g, b;
- };
- //neopixel
- const int PIXEL_COUNT = 1;
- const int DATA_PIN = 3;
- Adafruit_NeoPixel pixels;
- RGB currentRGB = (RGB){0, 0, 0};
- //PIR
- const int PIR_PIN = 2;
- volatile int PIR_TICKER = 0;
- //inicializace randomizeru, serioveho portu, neopixelu a PIR cidla.
- void setup() {
- randomSeed(analogRead(0));
- Serial.begin(9600);
- pixels = Adafruit_NeoPixel(PIXEL_COUNT, DATA_PIN, NEO_RGB + NEO_KHZ400);
- pixels.begin();
- pinMode(PIR_PIN, INPUT);
- attachInterrupt(digitalPinToInterrupt(PIR_PIN), PIRTriggered, RISING);
- }
- //rozhoduje, co se bude dit, momentalne budnahodne meni barvy nebo idli
- void loop() {
- while (PIR_TICKER > 0) {
- switchRandomColor(random(12, 24));
- PIR_TICKER--;
- updateColor();
- }
- if (PIR_TICKER == 0) {
- idleLED();
- }
- }
- //nastavi a zobrazi aktualni barvu na neopixelu a odesle zpravu pres seriovy port
- void updateColor() {
- pixels.setPixelColor(0, currentRGB.g, currentRGB.r, currentRGB.b);
- pixels.show();
- serialUpdateColor();
- }
- //cykli cervenou a zadnou barvu na ledce kdyz neni na praci nic zajimavejsiho
- void idleLED() {
- currentRGB = {16,0,0};
- updateColor();
- delay(24);
- currentRGB = {0,0,0};
- updateColor();
- delay(240);
- };
- //Prepne na nahodnou barvu vyhovujici podminkam
- void switchRandomColor(int steps)
- {
- int intensityDiff = 0;
- int diffRed, diffGreen, diffBlue;
- int targetRed, targetGreen, targetBlue;
- while (intensityDiff < random(256, 512)) {
- targetRed = random(0, 255);
- targetGreen = random(0, 255);
- targetBlue = random(0, 255);
- diffRed = targetRed - currentRGB.r;
- diffGreen = targetGreen - currentRGB.g;
- diffBlue = targetBlue - currentRGB.b;
- intensityDiff = abs(diffRed) + abs(diffGreen) + abs(diffBlue);
- }
- for (int i = steps; i > 0; i--) {
- currentRGB.r += diffRed / steps;
- currentRGB.g += diffGreen / steps;
- currentRGB.b += diffBlue / steps;
- updateColor();
- delay(48);
- }
- }
- //Odesle zpravu o soucasne barve a vysce tick query pres seriovy port
- void serialUpdateColor() {
- String color = "Zbyva barev: ";
- color += PIR_TICKER;
- color += " --- Aktualni R";
- color += currentRGB.r;
- color += "-G";
- color += currentRGB.g;
- color += "-B";
- color += currentRGB.b;
- Serial.println(color);
- }
- //obsluhuje PIR cidlo, pridava ticky do globalni promenne
- void PIRTriggered() {
- PIR_TICKER += random(8, 16);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement