/*
LEDC Software Fade
This example shows how to software fade LED
using the ledcWrite function.
Code adapted from original Arduino Fade example:
https://www.arduino.cc/en/Tutorial/Fade
This example code is in the public domain.
*/// use first channel of 16 channels (started from zero)
#define LEDC_CHANNEL_0 0
// use 12 bit precission for LEDC timer
#define LEDC_TIMER_12_BIT 12
// use 5000 Hz as a LEDC base frequency
#define LEDC_BASE_FREQ 5000
// fade LED PIN (replace with LED_BUILTIN constant for built-in LED)
#define LED_PIN 5
intbrightness=0;// how bright the LED is
intfadeAmount=5;// how many points to fade the LED by
// Arduino like analogWrite
// value has to be between 0 and valueMax
voidledcAnalogWrite(uint8_tchannel,uint32_tvalue,uint32_tvalueMax=255){// calculate duty, 4095 from 2 ^ 13 - 1
uint32_tduty=(4095/valueMax)*min(value,valueMax);// write duty to LEDC
ledcWrite(channel,duty);}
voidsetup(){// Setup timer and attach timer to a led pin
ledcSetup(LEDC_CHANNEL_0,LEDC_BASE_FREQ,LEDC_TIMER_12_BIT);ledcAttachPin(LED_PIN,LEDC_CHANNEL_0);}
voidloop(){// set the brightness on LEDC channel 0
ledcAnalogWrite(LEDC_CHANNEL_0,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);}