Advanced LED programs added

This commit is contained in:
Mal 2024-10-19 00:24:35 +02:00
parent 0e8b4acee6
commit a6fac3451b
2 changed files with 61 additions and 0 deletions

View File

@ -58,3 +58,47 @@ void SimpLedRGB::translateToColor(const Color &color, int duration_millis)
setColor(color);
}
void SimpLedRGB::rainbowCycle(int cycleDurationMillis)
{
setColor(COLOR_RED);
delay(cycleDurationMillis);
translateToColor(COLOR_YELLOW, cycleDurationMillis);
delay(cycleDurationMillis);
translateToColor(COLOR_GREEN, cycleDurationMillis);
delay(cycleDurationMillis);
translateToColor(COLOR_CYAN, cycleDurationMillis);
delay(cycleDurationMillis);
translateToColor(COLOR_BLUE, cycleDurationMillis);
delay(cycleDurationMillis);
translateToColor(COLOR_VIOLET, cycleDurationMillis);
delay(cycleDurationMillis);
translateToColor(COLOR_RED, cycleDurationMillis);
}
void SimpLedRGB::candle(const Color &color)
{
float brightness = (float)random(300, 1000) / 1000.0;
int duration = random(100, 1000);
translateToColor(
{
color.red * brightness,
color.green * brightness,
color.blue * brightness
},
duration
);
}
void SimpLedRGB::pulse(const Color &color, float timesPerSecond = 1.0, float amplitudeMin = 0.0, float amplitudeMax = 1.0)
{
float amplitude = (1.0 + sin(PULSE_PER_SECOND * millis() * timesPerSecond)) / 2.0;
float brightness = amplitudeMin + (amplitudeMax - amplitudeMin) * amplitude;
setColor({
color.red * brightness,
color.green * brightness,
color.blue * brightness
});
}

View File

@ -9,12 +9,23 @@ typedef struct {
float blue;
} Color;
const Color COLOR_CANDLE = {1.0, 0.5, 0.0};
const Color COLOR_VIOLET = {1.0, 0.0, 1.0};
const Color COLOR_GREEN = {0.0, 1.0, 0.0};
const Color COLOR_CYAN = {0.0, 0.5, 1.0};
const Color COLOR_RED = {1.0, 0.0, 0.0};
const Color COLOR_YELLOW = {1.0, 1.0, 0.0};
const Color COLOR_ORANGE = {1.0, 0.5, 0.0};
const Color COLOR_BLUE = {0.0, 0.0, 1.0};
class SimpLedRGB
{
int pin_red = 0;
int pin_green = 0;
int pin_blue = 0;
const double PULSE_PER_SECOND = ((PI * 2) / 1000.0);
Color color = {0.0, 0.0, 0.0};
public:
@ -25,6 +36,12 @@ class SimpLedRGB
void setColor(const Color &color);
void translateToColor(const Color &color, int duration_millis);
void rainbowCycle(int cycleDurationMillis);
void candle(const Color &color);
void pulse(const Color &color, float timesPerSecond = 1.0, float amplitudeMin = 0.0, float amplitudeMax = 1.0);
};
#endif