Created
July 15, 2014 05:01
-
-
Save tekk/51f437da33a5e429848d to your computer and use it in GitHub Desktop.
PWM_ColorMixing_RGB_LED_CommonAnode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <FlexiTimer2.h> | |
| #define GREEN 9 | |
| #define RED 6 | |
| #define BLUE 5 | |
| int redPin = RED; | |
| int greenPin = GREEN; | |
| int bluePin = BLUE; | |
| void setup() | |
| { | |
| pinMode(RED, OUTPUT); | |
| pinMode(GREEN, OUTPUT); | |
| pinMode(BLUE, OUTPUT); | |
| digitalWrite(RED, 1); | |
| digitalWrite(GREEN, 1); | |
| digitalWrite(BLUE, 1); | |
| pinMode(10, OUTPUT); | |
| FlexiTimer2::set(500, flash); // 500ms period | |
| FlexiTimer2::start(); | |
| } | |
| int redIntensity = 255; | |
| void loop() | |
| { | |
| // set all 3 pins to the desired intensity | |
| analogWrite(redPin, redIntensity); | |
| analogWrite(greenPin, redIntensity - 255); | |
| analogWrite(bluePin, 0); | |
| // remain at this color, but not for very long | |
| delay(10); | |
| // increase the red | |
| redIntensity--; | |
| // since 255 is the maximum, set it back to 0 | |
| // when it increments beyond 255 | |
| if (redIntensity <= 0) | |
| { | |
| redIntensity = 255; | |
| } | |
| } | |
| void flash() | |
| { | |
| static boolean output = HIGH; | |
| digitalWrite(10, output); | |
| output = !output; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment