/* Variable Brightness LEDs * Done with three of Arduino's Pulse Width Modulation (PWM) * pins (9,10,11) */ // 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; // Setup // Set PWM pins 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, reset it to 0 if(pwmVal1 >= 255) { pwmVal1 = 0; } if(pwmVal2 >= 255) { pwmVal2 = 0; } if(pwmVal3 >= 255) { pwmVal3 = 0; } // Set the first PWM pin to pwmVal1 analogWrite(pwmPin1,pwmVal1); // Increment pwmVal1 by 1 pwmVal1=pwmVal1+1; // Same as before... analogWrite(pwmPin2,pwmVal2); // ... but increment pwmVal2 by 5 instead of 1 pwmVal2=pwmVal2+5; // Same as before... analogWrite(pwmPin3,pwmVal3); // ... but increment pwmVal3 by 10 instead of 1 or 5 pwmVal3=pwmVal3+10; // Put in a short delay to see the effect delay(10); }