1
+ /*
2
+ LEDC Software Fade on shared channel with multiple pins
3
+
4
+ This example shows how to software fade LED
5
+ using the ledcWriteChannel function on multiple pins.
6
+ This example is useful if you need to control synchronously
7
+ multiple LEDs on different pins.
8
+
9
+ Code adapted from original Arduino Fade example:
10
+ https://www.arduino.cc/en/Tutorial/Fade
11
+
12
+ This example code is in the public domain.
13
+ */
14
+
15
+ // use 8 bit precision for LEDC timer
16
+ #define LEDC_TIMER_8_BIT 8
17
+
18
+ // use 5000 Hz as a LEDC base frequency
19
+ #define LEDC_BASE_FREQ 5000
20
+
21
+ // LED pins
22
+ #define LED_PIN_1 4
23
+ #define LED_PIN_2 5
24
+
25
+ // LED channel that will be used instead of automatic selection.
26
+ #define LED_CHANNEL 0
27
+
28
+ int brightness = 0 ; // how bright the LED is
29
+ int fadeAmount = 5 ; // how many points to fade the LED by
30
+
31
+ void setup () {
32
+ // Use single LEDC channel 0 for both pins
33
+ ledcAttachChannel (LED_PIN_1, LEDC_BASE_FREQ, LEDC_TIMER_8_BIT, LED_CHANNEL);
34
+ ledcAttachChannel (LED_PIN_2, LEDC_BASE_FREQ, LEDC_TIMER_8_BIT, LED_CHANNEL);
35
+ }
36
+
37
+ void loop () {
38
+ // set the brightness on LEDC channel 0
39
+ ledcWriteChannel (LED_CHANNEL, brightness);
40
+
41
+ // change the brightness for next time through the loop:
42
+ brightness = brightness + fadeAmount;
43
+
44
+ // reverse the direction of the fading at the ends of the fade:
45
+ if (brightness <= 0 || brightness >= 255 ) {
46
+ fadeAmount = -fadeAmount;
47
+ }
48
+ // wait for 30 milliseconds to see the dimming effect
49
+ delay (30 );
50
+ }
0 commit comments