-
Notifications
You must be signed in to change notification settings - Fork 7.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ledc): Allow attaching multiple pins to 1 channel (#10032)
* feat(ledc): Allow attaching multiple pins to 1 channel * feat(ledc): Add ledcWriteChannel function * feat(ledc): Print info about already set channel * docs(ledc): Add ledcWriteChannel function and attach update * feat(ledc): Add example and fixes * feat(ledc): Remove commented code * feat(ledc): Fix missing new line at end of file * ci(pre-commit): Apply automatic fixes --------- Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
- Loading branch information
1 parent
e850afb
commit 3c1e5a9
Showing
4 changed files
with
151 additions
and
17 deletions.
There are no files selected for viewing
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
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
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
50 changes: 50 additions & 0 deletions
50
libraries/ESP32/examples/AnalogOut/LEDCSingleChannel/LEDCSingleChannel.ino
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
LEDC Software Fade on shared channel with multiple pins | ||
This example shows how to software fade LED | ||
using the ledcWriteChannel function on multiple pins. | ||
This example is useful if you need to control synchronously | ||
multiple LEDs on different pins. | ||
Code adapted from original Arduino Fade example: | ||
https://www.arduino.cc/en/Tutorial/Fade | ||
This example code is in the public domain. | ||
*/ | ||
|
||
// use 8 bit precision for LEDC timer | ||
#define LEDC_TIMER_8_BIT 8 | ||
|
||
// use 5000 Hz as a LEDC base frequency | ||
#define LEDC_BASE_FREQ 5000 | ||
|
||
// LED pins | ||
#define LED_PIN_1 4 | ||
#define LED_PIN_2 5 | ||
|
||
// LED channel that will be used instead of automatic selection. | ||
#define LEDC_CHANNEL 0 | ||
|
||
int brightness = 0; // how bright the LED is | ||
int fadeAmount = 5; // how many points to fade the LED by | ||
|
||
void setup() { | ||
// Use single LEDC channel 0 for both pins | ||
ledcAttachChannel(LED_PIN_1, LEDC_BASE_FREQ, LEDC_TIMER_8_BIT, LEDC_CHANNEL); | ||
ledcAttachChannel(LED_PIN_2, LEDC_BASE_FREQ, LEDC_TIMER_8_BIT, LEDC_CHANNEL); | ||
} | ||
|
||
void loop() { | ||
// set the brightness on LEDC channel 0 | ||
ledcWriteChannel(LEDC_CHANNEL, brightness); | ||
|
||
// change the brightness for next time through the loop: | ||
brightness = brightness + fadeAmount; | ||
|
||
// reverse the direction of the fading at the ends of the fade: | ||
if (brightness <= 0 || brightness >= 255) { | ||
fadeAmount = -fadeAmount; | ||
} | ||
// wait for 30 milliseconds to see the dimming effect | ||
delay(30); | ||
} |