Replies: 2 comments
-
I just modified the code I've been running in my lab to control an RGB LED strip to use the fade: #include <Arduino.h>
#ifdef ESP32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif
#include "fauxmoESP.h"
#include "driver/ledc.h"
// Rename the credentials.sample.h file to credentials.h and
// edit it according to your router configuration
#include "credentials.h"
// or alternately set SSID and passphrase below
//#define WIFI_SSID "----"
//#define WIFI_PASS "----"
fauxmoESP fauxmo;
#define SERIAL_BAUDRATE 115200
#define DEVICE_NAME "Aardvark"
// Set RGB pins
#define REDPIN 21
#define GREENPIN 22
#define BLUEPIN 23
// LED channels
#define REDC 1
#define GREENC 2
#define BLUEC 3
// -----------------------------------------------------------------------------
// Wifi
// -----------------------------------------------------------------------------
void wifiSetup() {
// Set WIFI module to STA mode
WiFi.mode(WIFI_STA);
// Connect
Serial.printf("[WIFI] Connecting to %s ", WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASS);
// Wait
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(100);
}
Serial.println();
// Connected!
Serial.printf("[WIFI] STATION Mode, SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());
}
// HSV-RGB stuff
uint8_t redLed;
uint8_t greenLed;
uint8_t blueLed;
void setup()
{
Serial.begin(SERIAL_BAUDRATE);
// Wifi
wifiSetup();
fauxmo.setPort(80); // This is required for gen3 devices
fauxmo.enable(true);
fauxmo.addDevice(DEVICE_NAME);
// LED strip testing
ledcAttachPin(REDPIN, REDC);
ledcAttachPin(GREENPIN, GREENC);
ledcAttachPin(BLUEPIN, BLUEC);
ledcSetup(REDC, 12000, 8); // 12kHz 8 bit
ledcSetup(GREENC, 12000, 8);
ledcSetup(BLUEC, 12000, 8);
// Initialize fade service.
ledc_fade_func_install(0);
//fauxmo.setState((unsigned char) 0, (bool) 1, (unsigned char) 254);
fauxmo.onSetState([](unsigned char device_id, const char * device_name, bool state, unsigned char value, unsigned int hue, unsigned int saturation, unsigned int ct)
{
Serial.printf("[MAIN] Device #%d (%s) state: %s value: %d hue: %u saturation: %u ct: %u\n", device_id, device_name, state ? "ON" : "OFF", value, hue, saturation, ct);
char colormode[3];
fauxmo.getColormode(device_id, colormode, 3);
Serial.printf("Colormode: %s\n", colormode);
redLed = fauxmo.getRed(device_id);
greenLed = fauxmo.getGreen(device_id);
blueLed = fauxmo.getBlue(device_id);
Serial.printf("HSV: %d %d %d RGB: %d %d %d\n", hue, saturation, value, redLed, greenLed, blueLed);
int fadeTime = 1000;
if (state)
{
/* //Note: if you want to be able to adjust the duty cycle more rapidly than the fade time it's better to use:
ledcWrite(REDC, redLed);
ledcWrite(GREENC, greenLed);
ledcWrite(BLUEC, blueLed);
*/
ledc_set_fade_with_time( LEDC_HIGH_SPEED_MODE, (ledc_channel_t) REDC, redLed, fadeTime);
ledc_set_fade_with_time( LEDC_HIGH_SPEED_MODE, (ledc_channel_t) GREENC, greenLed, fadeTime);
ledc_set_fade_with_time( LEDC_HIGH_SPEED_MODE, (ledc_channel_t) BLUEC, blueLed, fadeTime);
ledc_fade_start( LEDC_HIGH_SPEED_MODE, (ledc_channel_t) REDC, LEDC_FADE_NO_WAIT);
ledc_fade_start( LEDC_HIGH_SPEED_MODE, (ledc_channel_t) GREENC, LEDC_FADE_NO_WAIT);
ledc_fade_start( LEDC_HIGH_SPEED_MODE, (ledc_channel_t) BLUEC, LEDC_FADE_NO_WAIT);
}
else
{
ledc_set_fade_with_time( LEDC_HIGH_SPEED_MODE, (ledc_channel_t) REDC, 0, fadeTime);
ledc_set_fade_with_time( LEDC_HIGH_SPEED_MODE, (ledc_channel_t) GREENC, 0, fadeTime);
ledc_set_fade_with_time( LEDC_HIGH_SPEED_MODE, (ledc_channel_t) BLUEC, 0, fadeTime);
ledc_fade_start( LEDC_HIGH_SPEED_MODE, (ledc_channel_t) REDC, LEDC_FADE_NO_WAIT);
ledc_fade_start( LEDC_HIGH_SPEED_MODE, (ledc_channel_t) GREENC, LEDC_FADE_NO_WAIT);
ledc_fade_start( LEDC_HIGH_SPEED_MODE, (ledc_channel_t) BLUEC, LEDC_FADE_NO_WAIT);
}
});
}
void loop() {
fauxmo.handle();
} The excerpt from code you saw before was from a big (and ugly!) ESP-IDF program, so I simplified it here. Note that if you rapidly and repeatedly change the level using the slider in the Alexa app it can lock it up. I haven't looked into that yet - I simply don't do that. ;) I think I'll add this (or something like this) to the examples, but I'll wait and see your experience. HTH! |
Beta Was this translation helpful? Give feedback.
-
Hello Paul, |
Beta Was this translation helpful? Give feedback.
-
Hello Paul,
in #135 you showed a code example with ledc_set_fade_with_time() and ledc_fade_start().
I use my own interrupt driven fade-functions for RGB and white LED strips. But the fading stutters sometimes on low brightnesses because of changing the duty cycle into an running PWM-cycle.
I test the ESP32-functions you use for fading with no success:
Please can you post as an example your declaration and init sections for the used consts and variables for the arguments of both ledc functions?
Thanks and regards
Bernd
Beta Was this translation helpful? Give feedback.
All reactions