update ESP32 LEDC usage

This commit is contained in:
caternuson 2023-10-11 15:11:33 -07:00
parent 89e3823e94
commit 7e064d5be9
4 changed files with 127 additions and 71 deletions

View file

@ -62,6 +62,10 @@
uint8_t i=0; uint8_t i=0;
uint8_t red_out = RED_LED;
uint8_t green_out = GREEN_LED;
uint8_t blue_out = BLUE_LED;
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
Serial.println("\nProp-Maker Wing: LED Example"); Serial.println("\nProp-Maker Wing: LED Example");
@ -73,21 +77,32 @@ void setup() {
// Set up the LED Pins // Set up the LED Pins
#if defined(ESP32) // and ESP32-S2! #if defined(ESP32) // and ESP32-S2!
ledcSetup(RED_LED, 5000, 8); #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 1)
ledcAttachPin(RED_PIN, RED_LED); // newer LEDC API, use pins instead of channel
ledcSetup(GREEN_LED, 5000, 8); red_out = RED_PIN;
ledcAttachPin(GREEN_PIN, GREEN_LED); green_out = GREEN_PIN;
ledcSetup(BLUE_LED, 5000, 8); blue_out = BLUE_PIN;
ledcAttachPin(BLUE_PIN, BLUE_LED); ledcAttach(RED_PIN, 5000, 8);
ledcAttach(GREEN_PIN, 5000, 8);
ledcAttach(BLUE_PIN, 5000, 8);
#else
// older LEDC API, use channel, attach pin to channel
ledcSetup(RED_LED, 5000, 8);
ledcAttachPin(RED_PIN, RED_LED);
ledcSetup(GREEN_LED, 5000, 8);
ledcAttachPin(GREEN_PIN, GREEN_LED);
ledcSetup(BLUE_LED, 5000, 8);
ledcAttachPin(BLUE_PIN, BLUE_LED);
#endif
#else #else
pinMode(RED_LED, OUTPUT); pinMode(red_out, OUTPUT);
pinMode(GREEN_LED, OUTPUT); pinMode(green_out, OUTPUT);
pinMode(BLUE_LED, OUTPUT); pinMode(blue_out, OUTPUT);
#endif #endif
analogWrite(RED_LED, 0); analogWrite(red_out, 0);
analogWrite(GREEN_LED, 0); analogWrite(green_out, 0);
analogWrite(BLUE_LED, 0); analogWrite(blue_out, 0);
} }
uint32_t Color(uint8_t r, uint8_t g, uint8_t b) { uint32_t Color(uint8_t r, uint8_t g, uint8_t b) {
@ -119,8 +134,8 @@ void loop()
digitalWrite(POWER_PIN, HIGH); digitalWrite(POWER_PIN, HIGH);
// write colors to the 3W LED // write colors to the 3W LED
analogWrite(RED_LED, red); analogWrite(red_out, red);
analogWrite(GREEN_LED, green); analogWrite(green_out, green);
analogWrite(BLUE_LED, blue); analogWrite(blue_out, blue);
delay(2); delay(2);
} }

View file

@ -17,8 +17,12 @@ void setup() {
pinMode(LED_BUILTIN, OUTPUT); pinMode(LED_BUILTIN, OUTPUT);
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 1)
ledcAttach(LED_BUILTIN, 5000, 8);
#else
ledcSetup(0, 5000, 8); ledcSetup(0, 5000, 8);
ledcAttachPin(LED_BUILTIN, 0); ledcAttachPin(LED_BUILTIN, 0);
#endif
pixels.begin(); // Initialize pins for output pixels.begin(); // Initialize pins for output
pixels.show(); // Turn all LEDs off ASAP pixels.show(); // Turn all LEDs off ASAP
@ -26,12 +30,15 @@ void setup() {
} }
void loop() { void loop() {
Serial.println("Hello!"); Serial.println("Hello!");
// pulse red LED // pulse red LED
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 1)
ledcWrite(LED_BUILTIN, LED_dutycycle++);
#else
ledcWrite(0, LED_dutycycle++); ledcWrite(0, LED_dutycycle++);
#endif
// rainbow dotstars // rainbow dotstars
for (int i=0; i<pixels.numPixels(); i++) { // For each pixel in strip... for (int i=0; i<pixels.numPixels(); i++) { // For each pixel in strip...

View file

@ -79,12 +79,18 @@ void setup() {
pinMode(LED_BUILTIN, OUTPUT); pinMode(LED_BUILTIN, OUTPUT);
pinMode(SPEAKER, OUTPUT); pinMode(SPEAKER, OUTPUT);
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 1)
ledcAttach(LED_BUILTIN, 2000, 8);
ledcAttach(SPEAKER, 2000, 8);
ledcWrite(SPEAKER, 0);
#else
ledcSetup(0, 2000, 8); ledcSetup(0, 2000, 8);
ledcAttachPin(LED_BUILTIN, 0); ledcAttachPin(LED_BUILTIN, 0);
ledcSetup(1, 2000, 8); ledcSetup(1, 2000, 8);
ledcAttachPin(SPEAKER, 1); ledcAttachPin(SPEAKER, 1);
ledcWrite(1, 0); ledcWrite(1, 0);
#endif
} }
@ -257,7 +263,11 @@ void loop() {
/************************** LEDs */ /************************** LEDs */
// pulse red LED // pulse red LED
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 1)
ledcWrite(LED_BUILTIN, LED_dutycycle);
#else
ledcWrite(0, LED_dutycycle); ledcWrite(0, LED_dutycycle);
#endif
LED_dutycycle += 32; LED_dutycycle += 32;
// rainbow dotstars // rainbow dotstars
@ -271,9 +281,16 @@ void loop() {
void fhtone(uint8_t pin, float frequency, float duration) { void fhtone(uint8_t pin, float frequency, float duration) {
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 1)
ledcAttach(SPEAKER, frequency, 8);
ledcWrite(SPEAKER, 128);
delay(duration);
ledcWrite(SPEAKER, 0);
#else
ledcSetup(1, frequency, 8); ledcSetup(1, frequency, 8);
ledcAttachPin(pin, 1); ledcAttachPin(pin, 1);
ledcWrite(1, 128); ledcWrite(1, 128);
delay(duration); delay(duration);
ledcWrite(1, 0); ledcWrite(1, 0);
#endif
} }

View file

@ -78,12 +78,18 @@ void setup() {
pinMode(LED_BUILTIN, OUTPUT); pinMode(LED_BUILTIN, OUTPUT);
pinMode(SPEAKER, OUTPUT); pinMode(SPEAKER, OUTPUT);
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 1)
ledcAttach(LED_BUILTIN, 2000, 8);
ledcAttach(SPEAKER, 2000, 8);
ledcWrite(SPEAKER, 0);
#else
ledcSetup(0, 2000 * 80, 8); ledcSetup(0, 2000 * 80, 8);
ledcAttachPin(LED_BUILTIN, 0); ledcAttachPin(LED_BUILTIN, 0);
ledcSetup(1, 2000 * 80, 8); ledcSetup(1, 2000 * 80, 8);
ledcAttachPin(SPEAKER, 1); ledcAttachPin(SPEAKER, 1);
ledcWrite(1, 0); ledcWrite(1, 0);
#endif
} }
@ -256,7 +262,11 @@ void loop() {
/************************** LEDs */ /************************** LEDs */
// pulse red LED // pulse red LED
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 1)
ledcWrite(LED_BUILTIN, LED_dutycycle);
#else
ledcWrite(0, LED_dutycycle); ledcWrite(0, LED_dutycycle);
#endif
LED_dutycycle += 32; LED_dutycycle += 32;
// rainbow dotstars // rainbow dotstars
@ -270,9 +280,16 @@ void loop() {
void fhtone(uint8_t pin, float frequecy, float duration) { void fhtone(uint8_t pin, float frequecy, float duration) {
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 1)
ledcAttach(SPEAKER, frequecy, 8);
ledcWrite(SPEAKER, 128);
delay(duration);
ledcWrite(SPEAKER, 0);
#else
ledcSetup(1, frequecy * 80, 8); ledcSetup(1, frequecy * 80, 8);
ledcAttachPin(pin, 1); ledcAttachPin(pin, 1);
ledcWrite(1, 128); ledcWrite(1, 128);
delay(duration); delay(duration);
ledcWrite(1, 0); ledcWrite(1, 0);
#endif
} }