Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Fade RGB LEDs
- This example shows how to fade an LED on pin 3,5,6
- using the analogWrite() function.
- This example code is in the public domain.
- */
- int brightnessBlue = 0; // how bright the BLUE LED is
- int fadeAmountBlue = 1; // how many points to fade the BLUE LED by
- long incrementTimeBlue = 10;
- long desiredTimeBlue = incrementTimeBlue;
- int brightnessGreen = 85; // how bright the GREEN LED is
- int fadeAmountGreen = 2; // how many points to fade the BLUE LED by
- long incrementTimeGreen = 10;
- long desiredTimeGreen = incrementTimeGreen;
- int brightnessRed = 170; // how bright the RED LED is
- int fadeAmountRed = 3; // how many points to fade the BLUE LED by
- long incrementTimeRed = 10;
- long desiredTimeRed = incrementTimeRed;
- void setup() {
- // declare pin 9 to be an output:
- pinMode(3, OUTPUT);
- pinMode(5, OUTPUT);
- pinMode(6, OUTPUT);
- Serial.begin(9600);
- }
- void loop() {
- // set the brightness of pin 3, 5, and 6:
- analogWrite(3, brightnessBlue);
- analogWrite(5, brightnessGreen);
- analogWrite(6, brightnessRed);
- // BLUE LED LOGIC BEGIN
- // change the brightness for next time through the loop:
- if( millis() >= desiredTimeBlue){
- brightnessBlue += fadeAmountBlue;
- desiredTimeBlue += incrementTimeBlue;
- }
- // reverse the direction of the fading at the ends of the fade:
- if (brightnessBlue <= 0 || brightnessBlue >= 255) {
- fadeAmountBlue *= -1 ;
- brightnessBlue += fadeAmountBlue;
- }
- // BLUE LED LOGIC END
- // GREEN LED LOGIC BEGIN
- // change the brightness for next time through the loop:
- if( millis() >= desiredTimeGreen){
- brightnessGreen += fadeAmountGreen;
- desiredTimeGreen += incrementTimeGreen;
- }
- // reverse the direction of the fading at the ends of the fade:
- if (brightnessGreen <= 0 || brightnessGreen >= 255) {
- fadeAmountGreen *= -1 ;
- brightnessGreen += fadeAmountGreen;
- }
- // GREEN LED LOGIC END
- // RED LED LOGIC BEGIN
- // change the brightness for next time through the loop:
- if( millis() >= desiredTimeRed){
- brightnessRed += fadeAmountRed;
- desiredTimeRed += incrementTimeRed;
- }
- // reverse the direction of the fading at the ends of the fade:
- if (brightnessRed <= 0 || brightnessRed >= 255) {
- fadeAmountRed *= -1 ;
- brightnessRed += fadeAmountRed;
- }
- // RED LED LOGIC END
- // wait for 30 milliseconds to see the dimming effect
- //delay(30);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement