Fading Two or More LEDs Using an ESP32 and the Arduino IDE
This video shows how to fade or dim LED lights using an ESP32.274-Fading two or more LEDs using an ESP32
语言: C++
/*
* Original code taken from ESP32 provided driver code
* This is Arduino code to control two or more LEDs and fade them
* using PWM and the LEDC library of ESP32
*
* Watch the video for explanation of this video: https://youtu.be/VCM6KMMvBfE
* Written/updated by Ahmad Shamshiri for Robojax Video channel, www.Robojax.com
* Date: December 17, 2017, in Ajax, Ontario, Canada
* Disclaimer: this code is "AS IS" and for educational purposes only.
* This code has been downloaded from https://robojax.com
* Get this code and other Arduino codes from Robojax.com.
Learn Arduino step by step in a structured course with all material, wiring diagrams, and libraries
all in one place. Purchase my course on Udemy.com: http://robojax.com/L/?id=62
****************************
Get early access to my videos via Patreon and have your name mentioned at the end of every
video I publish on YouTube here: http://robojax.com/L/?id=63 (watch until the end of this video for a list of my Patrons)
****************************
or make a donation using PayPal: http://robojax.com/L/?id=64
* * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.*
* This code has been downloaded from Robojax.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Use the first channel of 16 channels (started from zero)
#define LEDC_CHANNEL_0 0
#define LEDC_CHANNEL_1 1
// Use 13-bit precision for the LEDC timer
#define LEDC_TIMER_13_BIT 13
// 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_PIN0 17
#define LED_PIN1 16
int brightness0 = 0; // How bright the LED is
int brightness1 = 0; // How bright the LED is
int fadeAmount0 = 5; // How many points to fade the LED by
int fadeAmount1 = 10; // How many points to fade the LED by
// Arduino-like analogWrite
// Value has to be between 0 and valueMax
void ledcAnalogWrite(uint8_t channel, uint32_t value, uint32_t valueMax = 255) {
// Calculate duty, 8191 from 2 ^ 13 - 1
uint32_t duty = (8191 / valueMax) * min(value, valueMax);
// Write duty to LEDC
ledcWrite(channel, duty);
}
void setup() {
// Robojax.com ESP32 Fading LEDs 20191217
// Setup timer and attach timer to a led pin
ledcSetup(LEDC_CHANNEL_0, LEDC_BASE_FREQ, LEDC_TIMER_13_BIT);
ledcAttachPin(LED_PIN0, LEDC_CHANNEL_0);
ledcSetup(LEDC_CHANNEL_1, LEDC_BASE_FREQ, LEDC_TIMER_13_BIT);
ledcAttachPin(LED_PIN1, LEDC_CHANNEL_1);
}
void loop() {
// Robojax.com ESP32 Fading LEDs 20191217
// Set the brightness on LEDC channel 0
ledcAnalogWrite(LEDC_CHANNEL_0, brightness0);
ledcAnalogWrite(LEDC_CHANNEL_1, brightness1);
// Change the brightness for next time through the loop:
brightness0 = brightness0 + fadeAmount0;
brightness1 = brightness1 + fadeAmount1;
// brightness1 +=100;
// Reverse the direction of the fading at the ends of the fade:
if (brightness0 <= 0 || brightness0 >= 255) {
fadeAmount0 = -fadeAmount0;
}
if (brightness1 <= 0 || brightness1 >= 255) {
fadeAmount1 = -fadeAmount1;
}
// Wait for 30 milliseconds to see the dimming effect
delay(30);
// Robojax.com ESP32 Fading LEDs 20191217
}
资源与参考
尚无可用资源。
文件📁
没有可用的文件。