Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pulse mode #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions mqtt_esp8266_rgb/config-sample.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
// Number of seconds for one transition in colorfade mode
#define CONFIG_COLORFADE_TIME_SLOW 10
#define CONFIG_COLORFADE_TIME_FAST 3
// Number of seconds for one full pulse cycle
#define CONFIG_PULSE_TIME 5

// Reverse the LED logic
// false: 0 (off) - 255 (bright)
Expand Down
61 changes: 61 additions & 0 deletions mqtt_esp8266_rgb/mqtt_esp8266_rgb.ino
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ const byte colors[][3] = {
};
const int numColors = 7;

// Globals for pulse
int pulse = false;
int pulseDir = 1;
byte pulseRed = red;
byte pulseGreen = green;
byte pulseBlue = blue;
byte pulseBrightness = brightness;
byte pulseCurrentBrightness = 0;
int pulseTickEveryXms;

WiFiClient espClient;
PubSubClient client(espClient);

Expand Down Expand Up @@ -236,11 +246,13 @@ bool processJson(char* message) {

flash = true;
startFlash = true;
pulse = false;
}
else if (root.containsKey("effect") &&
(strcmp(root["effect"], "colorfade_slow") == 0 || strcmp(root["effect"], "colorfade_fast") == 0)) {
flash = false;
colorfade = true;
pulse = false;
currentColor = 0;
if (strcmp(root["effect"], "colorfade_slow") == 0) {
transitionTime = CONFIG_COLORFADE_TIME_SLOW;
Expand All @@ -249,6 +261,33 @@ bool processJson(char* message) {
transitionTime = CONFIG_COLORFADE_TIME_FAST;
}
}
else if (root.containsKey("effect") && strcmp(root["effect"], "pulse") == 0) {
if (root.containsKey("brightness")) {
pulseBrightness = root["brightness"];
}
else {
pulseBrightness = brightness;
}

pulseCurrentBrightness = pulseBrightness;

if (root.containsKey("color")) { // get the color to pulse
pulseRed = root["color"]["r"];
pulseGreen = root["color"]["g"];
pulseBlue = root["color"]["b"];
}
else { // or take the current color
pulseRed = red;
pulseGreen = green;
pulseBlue = blue;
}

pulseTickEveryXms = (CONFIG_PULSE_TIME*1000) / (pulseBrightness*2);

Serial.println("ACTIVATED PULSING!");
pulse = true;
flash = colorfade = startFade = inFade = false;
}
else if (colorfade && !root.containsKey("color") && root.containsKey("brightness")) {
// Adjust brightness during colorfade
// (will be applied when fading to the next color)
Expand All @@ -257,6 +296,7 @@ bool processJson(char* message) {
else { // No effect
flash = false;
colorfade = false;
pulse = false;

if (root.containsKey("color")) {
red = root["color"]["r"];
Expand Down Expand Up @@ -428,6 +468,27 @@ void loop() {
}
}
}

if (pulse) {
if ( millis() % pulseTickEveryXms == 0) {
pulseCurrentBrightness += pulseDir;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I'm misunderstanding something here, but wouldn't this only trigger if we manage to hit this if in a matching millisecond? I don't think we can guarantee that the loop will happen regularly enough to trust that.

I'll give this a more thorough review in the next few days.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point.. my tests (on a WeMos D1 mini with 80MHz) showed that without the delay(1) the pulsing was way too fast, and with it, one cycle was around 5 seconds (like specified in the config) and the animation looked nice and smooth.
Will look into a possibility to refactor this.

}
delay(1); // Don't hit above if-clause twice in the same millisecond
Serial.print("pulseCurrentBrightness:");
Serial.println(pulseCurrentBrightness);
if (pulseDir < 0 && pulseCurrentBrightness <= 0) {
pulseDir = 1;
Serial.println("pulseDir set to 1");
} else if (pulseDir > 0 && pulseCurrentBrightness >= pulseBrightness) {
pulseDir = -1;
Serial.println("pulseDir set to -1");
}
int tickRed = map(pulseRed, 0, 255, 0, pulseCurrentBrightness);
int tickGreen = map(pulseGreen, 0, 255, 0, pulseCurrentBrightness);
int tickBlue = map(pulseBlue, 0, 255, 0, pulseCurrentBrightness);
setColor(tickRed, tickGreen, tickBlue);
}

}

// From https://www.arduino.cc/en/Tutorial/ColorCrossfader
Expand Down