Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <FastLED.h>
- #include <EEPROM.h>
- #define LED_PIN 5
- #define BRIGHTNESS 255
- #define LED_TYPE WS2811
- #define COLOR_ORDER GRB
- #define WIDTH 5
- #define HEIGHT 10
- #define NUM_LEDS WIDTH * HEIGHT
- CRGB leds[NUM_LEDS];
- #define UPDATES_PER_SECOND 100
- #define GARLAND_COLORS 10
- #define HUE_STEP 256 / GARLAND_COLORS
- #define SWITCH_DELAY 60
- #define MODES 2
- #define RAINBOW_LENGTH 30
- #define RAINBOW_HUE_STEP 256 / RAINBOW_LENGTH
- #define MODE_CELL 1
- int currentLed = 0;
- byte mode = 2; //Гирлянда
- unsigned long lastChange = 0;
- unsigned long switchDelay = 0;
- byte rainbowShift = 0;
- unsigned long lastModeChange = 0;
- unsigned int modeChangeStep = 0;
- bool modeChanged = false;
- bool autoChangeMode = false;
- void setup() {
- //put your setup code here, to run once:
- delay(500); // power-up safety delay
- FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);//.setCorrection( TypicalLEDStrip );
- FastLED.setBrightness( BRIGHTNESS );
- randomSeed(analogRead(0));
- lastChange = millis();
- switchDelay = (unsigned long)SWITCH_DELAY * 1000;
- byte initMode = EEPROM.read(MODE_CELL);
- if (initMode > MODES) {
- initMode = 0;
- }
- autoChangeMode = initMode == 0;
- if (initMode == 0) {
- mode = 1;
- } else {
- mode = initMode;
- }
- initMode++;
- EEPROM.write(MODE_CELL, initMode);
- }
- void loop() {
- delay(10);
- if (autoChangeMode && (millis() - lastChange >= switchDelay)) {
- changeModeTick();
- }
- switch (mode) {
- case 1: // Гирлянда
- garland();
- break;
- case 2: //радуга
- rainbow();
- break;
- }
- FastLED.show();
- }
- void changeModeTick() {
- if (millis() - lastModeChange >= 12) {
- modeChangeStep++;
- if (!modeChanged && modeChangeStep >= BRIGHTNESS) {
- changeMode();
- return;
- }
- if (modeChanged && modeChangeStep >= BRIGHTNESS * 2) {
- lastChange = millis();
- modeChangeStep = 0;
- lastModeChange = millis();
- modeChanged = false;
- FastLED.setBrightness(BRIGHTNESS);
- return;
- }
- if (modeChangeStep < BRIGHTNESS) {
- FastLED.setBrightness(BRIGHTNESS - modeChangeStep);
- } else {
- FastLED.setBrightness(modeChangeStep - BRIGHTNESS);
- }
- lastModeChange = millis();
- }
- }
- void changeMode() {
- mode++;
- if (mode > MODES) {
- mode = 1;
- }
- modeChanged = true;
- }
- void garland() {
- if (random(10000) > 9000) {
- lightRandomLed();
- }
- fadeAllLeds();
- }
- void fadeAllLeds() {
- for (int i = 0; i < NUM_LEDS; i++) {
- leds[i].fadeToBlackBy(5);
- uint8_t luma = leds[i].getLuma();
- if (luma < 20) {
- leds[i] = getRandomColor();
- }
- }
- }
- void lightRandomLed() {
- int ledNumber = random(NUM_LEDS);
- leds[ledNumber] = getRandomColor();
- }
- void rainbow() {
- for (int i = 0; i < NUM_LEDS; i++) {
- int colorIdx = i % RAINBOW_LENGTH;
- int hue = (HUE_STEP * colorIdx + rainbowShift) % 256;
- leds[i] = CHSV(hue, 255, 255);
- }
- //int colorIdx = currentLed % RAINBOW_LENGTH;
- //int hue = HUE_STEP * colorIdx;
- //leds[currentLed] = CHSV(hue, 255, 255);
- //currentLed++;
- //if (currentLed >= NUM_LEDS) {
- // currentLed = 0;
- // }
- rainbowShift++;
- }
- void testColor() {
- //leds[currentLed] = CRGB::Black;
- leds[currentLed] = getRandomColor();
- if (currentLed >= NUM_LEDS) {
- currentLed = 0;
- }
- currentLed++;
- }
- CRGB getRandomColor() {
- int colorIdx = random(GARLAND_COLORS);
- int hue = HUE_STEP * colorIdx;
- CRGB color = CHSV(hue, 255, 255);
- return color;
- CRGB color2 = CHSV( random(256), random(80) + 176, 255);
- return color2;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement