/* Variable Brightness LEDs 2 * Done with three of Arduino's Pulse Width Modulation (PWM) * pins (9,10,11) * This time we'll use a direction changing variable to * change whether the pwmVal's will go up or down in the Loop */ // Set three pin variables for the three PWM pins int pwmPin1 = 11; int pwmPin2 = 10; int pwmPin3 = 9; // Set a variable for the intial value for the PWM int pwmVal1 = 0; int pwmVal2 = 0; int pwmVal3 = 0; // Set a variable for the inital direction (increment or decrement) for the PWM // A value of 1 will increment pwmVal's and a value of -1 will decrement pwmVal's. int upDown1 = 1; int upDown2 = 1; int upDown3 = 1; // Setup // Set PWM's to OUTPUT void setup() { pinMode(pwmPin1, OUTPUT); pinMode(pwmPin2, OUTPUT); pinMode(pwmPin3, OUTPUT); } //Loop void loop() { // analogWrite's range runs from 0 to 255, so when any of // our values reaches maximum, instead of reseting it to 0 // we'll change the variable upDown to -1. This will cause // pwmVal's to get smaller instead of bigger. if(pwmVal1 >= 255) { upDown1 = -1; } // Conversely, when it reaches a minimum (0), set the upDown to 1. // This will cause pwmVal's to get bigger instead of smaller. if(pwmVal1 <= 0) { upDown1 = 1; } // Same process for all pwmVal's (pwmVal2, pwmVal3) if(pwmVal2 >= 255) { upDown2 = -1; } if(pwmVal2 <= 0) { upDown2 = 1; } if(pwmVal3 >= 255) { upDown3 = -1; } if(pwmVal3 <= 0) { upDown3 = 1; } // Now set the pwmPins... analogWrite(pwmPin1,pwmVal1); // ... and depending on whether upDown is 1 or -1, increment // or decrement pwmVal's pwmVal1 = pwmVal1 + (1 * upDown1); analogWrite(pwmPin2,pwmVal2); pwmVal2 = pwmVal2 + (5 * upDown2); analogWrite(pwmPin3,pwmVal3); pwmVal3 = pwmVal3 + (10 * upDown3); // Delay a bit before looping delay(10); }