YourESP32 Sketch 2LEDS PWM Opposite

From ArduinoInfo
Jump to navigation Jump to search

/* YourESP32 Example: Sketch Template: YourESP32_Sketch_2LEDS_PWM_Opposite
 *  SEE: https://ESP35Info.Info
 - WHAT IT DOES:
   Uses ledc PWM library to dim 2 LEDS opposite to each other
 - SEE the comments after "//" on each line below
 - CONNECTIONS:
   - See below
   - 
 - V1.00 10/02/2018
   Questions: terry@yourduino.com */

/*-----( Import needed libraries )-----*/
/*-----( Declare Constants and Pin Numbers )-----*/
#define  ledPin   21  // Pins for two LEDS (with 220 ohm resistors) to GND
#define  ledPin2  23

// setting PWM properties
#define freq          5000
#define ledChannel_0  0
#define ledChannel_1  1
#define resolution    8
/*-----( Declare objects )-----*/
/*-----( Declare Variables )-----*/


void setup()   /****** SETUP: RUNS ONCE ******/
{

  // configure LED PWM library
  ledcSetup(ledChannel_0, freq, resolution);
  ledcSetup(ledChannel_1, freq, resolution);

  // attach the channels to the GPIOs to be controlled
  ledcAttachPin(ledPin,  ledChannel_0);
  ledcAttachPin(ledPin2, ledChannel_1);

}//--(end setup )---


void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
  // cycle the LED brightness
  for (int dutyCycle = 0; dutyCycle <= 255; dutyCycle++) {
    // changing the LED brightness with PWM
    ledcWrite(ledChannel_0, dutyCycle);
    ledcWrite(ledChannel_1, 255 - dutyCycle);  // Opposite value
    delay(15);
  }

  // reverse cycle the LED brightness
  for (int dutyCycle = 255; dutyCycle >= 0; dutyCycle--) {
    // changing the LED brightness with PWM
    ledcWrite(ledChannel_0, dutyCycle);
    ledcWrite(ledChannel_1, 255 - dutyCycle);
    delay(15);
  }

}//--(end main loop )---

/*-----( Declare User-written Functions )-----*/
//NONE

//*********( THE END )***********