Last active
September 13, 2022 20:30
-
-
Save mgrybyk/3c44ab92251694a90361db124658cf37 to your computer and use it in GitHub Desktop.
arduino uno + WS2812B + IR
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Codes | |
#define IR_CH_MIN 0x45 | |
#define IR_CH 0x46 | |
#define IR_CH_PLUS 0x47 | |
#define IR_PREV 0x44 | |
#define IR_NEXT 0x40 | |
#define IR_PLAY 0x43 | |
#define IR_VOL_MIN 0x07 | |
#define IR_VOL_PLUS 0x15 | |
#define IR_0 0x16 | |
#define IR_100 0x19 | |
#define IR_200 0xD | |
#define IR_EQ 0x9 | |
#define IR_1 0xC | |
#define IR_2 0x18 | |
#define IR_3 0x5E | |
#define IR_4 0x8 | |
#define IR_5 0x1C | |
#define IR_6 0x5A | |
#define IR_7 0x42 | |
#define IR_8 0x52 | |
#define IR_9 0x4A | |
// IR button to command mapping | |
#define COMMAND_COLOR_NEXT IR_NEXT | |
#define COMMAND_COLOR_PREV IR_PREV | |
#define COMMAND_BR_DEC IR_VOL_MIN | |
#define COMMAND_BR_INC IR_VOL_PLUS | |
#define COMMAND_BR_MIN IR_0 | |
#define COMMAND_BR_MID IR_100 | |
#define COMMAND_BR_MAX IR_200 | |
#define COMMAND_ANIM_PREV IR_CH_MIN | |
#define COMMAND_ANIM_NEXT IR_CH_PLUS | |
#define COMMAND_ST_LIGHT IR_CH | |
#define COMMAND_WHITE IR_EQ | |
#define COMMAND_1 IR_1 | |
#define COMMAND_2 IR_2 | |
#define COMMAND_3 IR_3 | |
#define COMMAND_4 IR_4 | |
#define COMMAND_5 IR_5 | |
#define COMMAND_6 IR_6 | |
#define COMMAND_7 IR_7 | |
#define COMMAND_8 IR_8 | |
#define COMMAND_9 IR_9 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <FastLED.h> | |
#define DATA_PIN 6 | |
#define NUM_LEDS 120 // количество светодиодов | |
#define LED_TYPE WS2812B | |
#include "IRremote.h" | |
#include "IRCommandMapping.h" | |
#define MAXPULSE 5000 | |
#define FIRST_MAXPULSE 500 | |
#define RESOLUTION 20 | |
const int IR_PIN = A4; | |
byte colorShiftPrev = 84; | |
byte colorShift = 84; | |
byte brightnessPrev = 33; | |
byte brightness = 33; | |
const byte MID_BRIGHTNESS = 45; | |
const byte MAX_BRIGHTNESS = 90; | |
byte saturation = 255; | |
boolean shouldShow = true; | |
boolean ledOn = true; | |
byte stripMode = 0; // 0 - static, 1 - animation | |
byte animationType = 0; | |
// effects | |
uint16_t rainbowCycle_j = 0; | |
// effects end | |
CRGB leds[NUM_LEDS]; | |
IRrecv irrecv(IR_PIN); | |
void setup() { | |
// arduino uno builtin led | |
pinMode(13, OUTPUT); | |
digitalWrite(13, LOW); | |
FastLED.addLeds<LED_TYPE, DATA_PIN>(leds, NUM_LEDS); | |
irrecv.enableIRIn(); | |
// Serial.begin(9600); | |
} | |
void loop() { | |
onIRCommand(); | |
if (shouldShow && stripMode == 0) { | |
setAllSlow(colorShift, saturation, brightness); | |
// Serial.print(colorShift); Serial.print(", "); Serial.println(brightness); | |
stripShow(); | |
shouldShow = false; | |
} | |
if (stripMode == 1) { | |
Serial.println(animationType); | |
switch (animationType) { | |
case 0: rainbowCycle(20); break; | |
// case 1: FadeInOut(true); break; | |
case 1: TwinkleRandom(random(12, 18), 90); | |
} | |
} | |
} | |
void shiftColor(int d) { | |
saturation = 255; | |
colorShift = (colorShift + 3 * d) & 255; | |
shouldShow = true; | |
} | |
void setColor(byte c, byte sat) { | |
saturation = sat; | |
colorShift = c; | |
shouldShow = true; | |
} | |
void incBrightness() { | |
if (brightness < MAX_BRIGHTNESS) { | |
brightness += 3; | |
shouldShow = true; | |
} | |
} | |
void decBrightness() { | |
if (brightness > 0) { | |
brightness = max(0, brightness - 3); | |
shouldShow = true; | |
} | |
} | |
void setAll(byte hue, byte sat, byte lightness) { | |
for (int i = 0; i < NUM_LEDS; i++) { | |
leds[i] = CHSV(hue, sat, lightness); | |
} | |
colorShiftPrev = hue; | |
brightnessPrev = lightness; | |
} | |
void setAllSlow(byte hue, byte sat, byte lightness) { | |
int lightStep = brightnessPrev < lightness ? 1 : -1; | |
for (; lightness != brightnessPrev; brightnessPrev += lightStep) { | |
setAll(colorShiftPrev, sat, brightnessPrev); | |
stripShow(); | |
delay(10); | |
} | |
if (sat != 0 && colorShiftPrev != 100) { | |
int hueStep = ((colorShiftPrev - hue) & 255) > 127 ? 1 : -1; | |
for (; hue != colorShiftPrev; colorShiftPrev = ((colorShiftPrev + hueStep) & 255)) { | |
setAll(colorShiftPrev, sat, lightness); | |
stripShow(); | |
delay(10); | |
} | |
} | |
setAll(hue, sat, lightness); | |
} | |
void onIRCommand() { | |
while (!irrecv.isIdle()); | |
if (!irrecv.decode()) { | |
return; | |
} | |
if (irrecv.decodedIRData.protocol != NEC || irrecv.decodedIRData.address != 0) { | |
// Serial.println("ignored bad command"); | |
// irrecv.printIRResultRawFormatted(&Serial, true); | |
irrecv.resume(); | |
return; | |
} | |
// irrecv.printIRResultShort(&Serial); | |
// if (irrecv.decodedIRData.flags == 1) {} | |
if (irrecv.decodedIRData.command == COMMAND_BR_INC) { | |
incBrightness(); | |
} else if (irrecv.decodedIRData.command == COMMAND_BR_DEC) { | |
decBrightness(); | |
} else if (irrecv.decodedIRData.command == COMMAND_COLOR_NEXT) { | |
shiftColor(1); | |
} else if (irrecv.decodedIRData.command == COMMAND_COLOR_PREV) { | |
shiftColor(-1); | |
} else if (irrecv.decodedIRData.command == COMMAND_WHITE) { | |
setColor(100, 0); // white | |
} else if (irrecv.decodedIRData.command == COMMAND_1) { | |
setColor(99, 255); // red | |
} else if (irrecv.decodedIRData.command == COMMAND_2) { | |
setColor(84, 255); // orange | |
} else if (irrecv.decodedIRData.command == COMMAND_3) { | |
setColor(69, 255); // yellow | |
} else if (irrecv.decodedIRData.command == COMMAND_4) { | |
setColor(9, 255); // green | |
} else if (irrecv.decodedIRData.command == COMMAND_5) { | |
setColor(235, 255); // cyan | |
} else if (irrecv.decodedIRData.command == COMMAND_6) { | |
setColor(223, 255); // light blue | |
} else if (irrecv.decodedIRData.command == COMMAND_7) { | |
setColor(184, 255); // blue | |
} else if (irrecv.decodedIRData.command == COMMAND_8) { | |
setColor(147, 255); // purple | |
} else if (irrecv.decodedIRData.command == COMMAND_9) { | |
setColor(120, 255); // pink | |
} else if (irrecv.decodedIRData.command == COMMAND_ANIM_PREV) { | |
stripMode = 1; | |
animationType = 0; | |
} else if (irrecv.decodedIRData.command == COMMAND_ANIM_NEXT) { | |
stripMode = 1; | |
animationType = 1; | |
} else if (irrecv.decodedIRData.command == COMMAND_ST_LIGHT) { | |
stripMode = 0; | |
shouldShow = true; | |
} else if (irrecv.decodedIRData.command == COMMAND_BR_MIN) { | |
brightness = 0; | |
shouldShow = true; | |
} else if (irrecv.decodedIRData.command == COMMAND_BR_MID) { | |
brightness = MID_BRIGHTNESS; | |
shouldShow = true; | |
} else if (irrecv.decodedIRData.command == COMMAND_BR_MAX) { | |
brightness = MAX_BRIGHTNESS; | |
shouldShow = true; | |
} | |
irrecv.resume(); | |
} | |
void FadeInOut(boolean colored) { | |
// Fade IN | |
Serial.print(animationType); Serial.print(", "); Serial.println(brightness); | |
for (; brightness < MAX_BRIGHTNESS; brightness++) { | |
setAll(colorShift, saturation, brightness); | |
stripShow(); | |
delay(20); | |
} | |
delay(300); | |
Serial.print(stripMode); Serial.print(", "); Serial.println(brightness); | |
// Fade OUT | |
for (brightness = MAX_BRIGHTNESS; brightness > 24; brightness--) { | |
setAll(colorShift, saturation, brightness); | |
stripShow(); | |
delay(20); | |
} | |
if (colored) { | |
colorShift = (colorShift + 1) & 255; | |
} | |
} | |
void setPixel(int Pixel, byte red, byte green, byte blue) { | |
leds[Pixel] = blend(CRGB::Black, CRGB(red, green, blue), brightness); | |
} | |
void rainbowCycle(int SpeedDelay) { | |
byte *c; | |
uint16_t i; | |
for (i = 0; i < NUM_LEDS; i++) { | |
c = Wheel(((i * 256 / NUM_LEDS) + rainbowCycle_j) & 255); | |
setPixel(i, *c, *(c + 1), *(c + 2)); | |
} | |
stripShow(); | |
delay(SpeedDelay); | |
rainbowCycle_j++; | |
// 5 cycles of all colors on wheel | |
if (rainbowCycle_j >= 256 * 5) { | |
rainbowCycle_j = 0; | |
} | |
} | |
byte * Wheel(byte WheelPos) { | |
static byte c[3]; | |
if (WheelPos < 85) { | |
c[0] = WheelPos * 3; | |
c[1] = 255 - WheelPos * 3; | |
c[2] = 0; | |
} else if (WheelPos < 170) { | |
WheelPos -= 85; | |
c[0] = 255 - WheelPos * 3; | |
c[1] = 0; | |
c[2] = WheelPos * 3; | |
} else { | |
WheelPos -= 170; | |
c[0] = 0; | |
c[1] = WheelPos * 3; | |
c[2] = 255 - WheelPos * 3; | |
} | |
return c; | |
} | |
void offHalf(int i) { | |
leds[i] = CHSV(0, 0, 0); | |
i = i + 2; | |
for (; i < NUM_LEDS; i = i + 2) { | |
leds[i] = CHSV(0, 0, 0); | |
if (random(0, 2) == 1) { | |
leds[i - 1] = CHSV(0, 0, 0); | |
} | |
delay(20); | |
stripShow(); | |
} | |
} | |
void TwinkleRandom(int Count, int SpeedDelay) { | |
offHalf(random(0, 2)); | |
for (int i = 0; i < Count; i++) { | |
setPixel(random(NUM_LEDS), random(0, 255), random(0, 255), random(0, 255)); | |
stripShow(); | |
delay(SpeedDelay); | |
} | |
} | |
void stripShow() { | |
while (!irrecv.isIdle()); | |
FastLED.show(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://www.tweaking4all.com/hardware/arduino/adruino-led-strip-effects/