Advertisement
Guest User

Garland

a guest
Feb 25th, 2020
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.67 KB | None | 0 0
  1. #include <FastLED.h>
  2. #include <EEPROM.h>
  3.  
  4. #define LED_PIN     5
  5. #define BRIGHTNESS  255
  6. #define LED_TYPE    WS2811
  7. #define COLOR_ORDER GRB
  8. #define WIDTH       5
  9. #define HEIGHT      10
  10. #define NUM_LEDS   WIDTH * HEIGHT
  11. CRGB leds[NUM_LEDS];
  12.  
  13. #define UPDATES_PER_SECOND 100
  14. #define GARLAND_COLORS 10
  15. #define HUE_STEP 256 / GARLAND_COLORS
  16. #define SWITCH_DELAY 60
  17. #define MODES 2
  18. #define RAINBOW_LENGTH 30
  19. #define RAINBOW_HUE_STEP 256 / RAINBOW_LENGTH
  20.  
  21. #define MODE_CELL 1
  22.  
  23. int currentLed = 0;
  24. byte mode = 2; //Гирлянда
  25. unsigned long lastChange = 0;
  26. unsigned long switchDelay = 0;
  27. byte rainbowShift = 0;
  28. unsigned long lastModeChange = 0;
  29. unsigned int modeChangeStep = 0;
  30. bool modeChanged = false;
  31. bool autoChangeMode = false;
  32.  
  33.  
  34. void setup() {
  35.   //put your setup code here, to run once:
  36.     delay(500); // power-up safety delay
  37.     FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);//.setCorrection( TypicalLEDStrip );
  38.     FastLED.setBrightness(  BRIGHTNESS );
  39.     randomSeed(analogRead(0));
  40.     lastChange = millis();
  41.     switchDelay = (unsigned long)SWITCH_DELAY * 1000;
  42.     byte initMode = EEPROM.read(MODE_CELL);
  43.     if (initMode > MODES) {
  44.       initMode = 0;
  45.     }
  46.  
  47.     autoChangeMode = initMode == 0;
  48.     if (initMode == 0) {
  49.       mode = 1;
  50.     } else {
  51.       mode = initMode;
  52.     }
  53.     initMode++;
  54.     EEPROM.write(MODE_CELL, initMode);
  55. }
  56.  
  57. void loop() {
  58.   delay(10);
  59.   if (autoChangeMode && (millis() - lastChange >= switchDelay)) {
  60.     changeModeTick();
  61.   }
  62.   switch (mode) {
  63.     case 1: // Гирлянда
  64.       garland();
  65.       break;
  66.     case 2: //радуга
  67.       rainbow();
  68.       break;
  69.   }
  70.  
  71.   FastLED.show();
  72. }
  73. void changeModeTick() {
  74.   if (millis() - lastModeChange >= 12) {
  75.     modeChangeStep++;
  76.     if (!modeChanged && modeChangeStep >= BRIGHTNESS) {
  77.       changeMode();
  78.       return;
  79.     }
  80.     if (modeChanged && modeChangeStep >= BRIGHTNESS * 2) {
  81.       lastChange = millis();
  82.       modeChangeStep = 0;
  83.       lastModeChange = millis();
  84.       modeChanged = false;
  85.       FastLED.setBrightness(BRIGHTNESS);  
  86.       return;
  87.     }
  88.     if (modeChangeStep < BRIGHTNESS) {
  89.       FastLED.setBrightness(BRIGHTNESS - modeChangeStep);  
  90.     } else {
  91.       FastLED.setBrightness(modeChangeStep - BRIGHTNESS);  
  92.     }
  93.     lastModeChange = millis();
  94.   }
  95. }
  96.  
  97. void changeMode() {
  98.     mode++;
  99.     if (mode > MODES) {
  100.       mode = 1;
  101.     }
  102.     modeChanged = true;
  103. }
  104.  
  105. void garland() {
  106.   if (random(10000) > 9000) {
  107.     lightRandomLed();
  108.   }
  109.   fadeAllLeds();  
  110. }
  111.  
  112. void fadeAllLeds() {
  113.   for (int i = 0; i < NUM_LEDS; i++) {
  114.     leds[i].fadeToBlackBy(5);
  115.     uint8_t luma = leds[i].getLuma();
  116.     if (luma < 20) {
  117.        leds[i] = getRandomColor();
  118.     }
  119.   }
  120. }
  121.  
  122. void lightRandomLed() {
  123.   int ledNumber = random(NUM_LEDS);
  124.   leds[ledNumber] = getRandomColor();
  125. }
  126.  
  127. void rainbow() {
  128.   for (int i = 0; i < NUM_LEDS; i++) {
  129.     int colorIdx = i % RAINBOW_LENGTH;
  130.     int hue = (HUE_STEP * colorIdx + rainbowShift) % 256;
  131.     leds[i] =  CHSV(hue, 255, 255);
  132.   }
  133.   //int colorIdx = currentLed % RAINBOW_LENGTH;
  134.   //int hue = HUE_STEP * colorIdx;
  135.   //leds[currentLed] =  CHSV(hue, 255, 255);
  136.   //currentLed++;
  137.   //if (currentLed >= NUM_LEDS) {
  138. //    currentLed = 0;
  139. //  }
  140.   rainbowShift++;  
  141. }
  142.  
  143. void testColor() {
  144.   //leds[currentLed] = CRGB::Black;
  145.   leds[currentLed] = getRandomColor();
  146.   if (currentLed >= NUM_LEDS) {
  147.     currentLed = 0;
  148.   }
  149.   currentLed++;
  150. }
  151.  
  152. CRGB getRandomColor() {
  153.   int colorIdx = random(GARLAND_COLORS);
  154.   int hue = HUE_STEP * colorIdx;
  155.   CRGB color = CHSV(hue, 255, 255);
  156.   return color;
  157.   CRGB color2 = CHSV( random(256), random(80) + 176, 255);
  158.   return color2;
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement