Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Debug mode, comment one of these lines out using a syntax
- //for a single line comment ("//"):
- //#define DEBUG 0 //0 = LEDs only
- #define DEBUG 1 //1 = LEDs w/ serial output
- // Define our LED pins:
- #define redPin 5
- #define greenPin 6
- #define bluePin 9
- // Create integer variables for our LED color value:
- int redValue = 0;
- int greenValue = 0;
- int blueValue = 0;
- //Create brightness variable
- //Ranging from 0.0-1.0:
- // 0.0 is off
- // 0.5 is 50%
- // 1.0 is fully on
- float brightness_LED = 0.1;
- //Create variables for type of LED and if it is used with a transistor
- boolean common_anode = false;
- boolean common_cathode = true;//i.e.) When pin is HIGH, LED will also go HIGH without a transistor/PicoBuck
- // Note:
- // Common Anode is `common_anode`
- // Common Cathode LED is `common_cathode`
- // Common Anode RGB LED Strip with transistor is `!common_anode`
- // RGB High Power LED with PicoBuck is also `!common_anode`
- boolean RGB_type = !common_anode;
- int blinkRate = 1000; //in milliseconds
- void setup() {
- // Make all of our LED pins outputs:
- pinMode(redPin, OUTPUT);
- pinMode(greenPin, OUTPUT);
- pinMode(bluePin, OUTPUT);
- allOFF(); //initialize LEDs with it turned off
- show_RGB(); //make sure to show it happening
- #if DEBUG
- Serial.begin(9600); //initialize Serial Monitor
- //while (!Serial); // Comment out to wait for serial port to connect to Serial Monitor. Needed for native USB.
- Serial.println("Custom Color Mixing w/ an RGB LED.");
- Serial.println(" ");
- Serial.println("Note: Make sure to adjust the code for a common cathode or common anode.");
- Serial.println("Default is set to no color and off!");
- Serial.println(" ");
- #endif
- }//end setup()
- void loop()
- {
- //used to visually check when Arduino is initialized
- redON();
- show_RGB();
- delay(blinkRate);
- orangeON();
- show_RGB();
- delay(blinkRate);
- yellowON();
- show_RGB();
- delay(blinkRate);
- chartrueseON();
- show_RGB();
- delay(blinkRate);
- greenON();
- show_RGB();
- delay(blinkRate);
- springGreenON();
- show_RGB();
- delay(blinkRate);
- cyanON();
- show_RGB();
- delay(blinkRate);
- azureON();
- show_RGB();
- delay(blinkRate);
- blueON();
- show_RGB();
- delay(blinkRate);
- violetON();
- show_RGB();
- delay(blinkRate);
- magentaON();
- show_RGB();
- delay(blinkRate);
- roseON();
- show_RGB();
- delay(blinkRate);
- whiteON();
- show_RGB();
- delay(blinkRate);
- allOFF();
- show_RGB();
- delay(blinkRate);
- }//end loop
- // ==================== CUSTOM FUNCTIONS DEFINED BELOW ====================
- void allOFF() {
- // Black (all LEDs off)
- // RGB LEDs:
- redValue = 0;
- greenValue = 0;
- blueValue = 0;
- calculate_RGB();
- redValue = int(redValue * brightness_LED);
- greenValue = int(greenValue * brightness_LED);
- blueValue = int(blueValue * brightness_LED);
- }
- void redON() {
- // Red
- redValue = 255;
- greenValue = 0;
- blueValue = 0;
- calculate_RGB();
- redValue = int(redValue * brightness_LED);
- greenValue = int(greenValue * brightness_LED);
- blueValue = int(blueValue * brightness_LED);
- }
- void orangeON() {
- // Orange
- redValue = 255;
- greenValue = 128;
- blueValue = 0;
- calculate_RGB();
- redValue = int(redValue * brightness_LED);
- greenValue = int(greenValue * brightness_LED);
- blueValue = int(blueValue * brightness_LED);
- }
- void yellowON() {
- // Yellow
- redValue = 255;
- greenValue = 255;
- blueValue = 0;
- calculate_RGB();
- redValue = int(redValue * brightness_LED);
- greenValue = int(greenValue * brightness_LED);
- blueValue = int(blueValue * brightness_LED);
- }
- void chartrueseON() {
- // Chartruese
- redValue = 128;
- greenValue = 255;
- blueValue = 0;
- calculate_RGB();
- redValue = int(redValue * brightness_LED);
- greenValue = int(greenValue * brightness_LED);
- blueValue = int(blueValue * brightness_LED);
- }
- void greenON() {
- // Green
- redValue = 0;
- greenValue = 255;
- blueValue = 0;
- calculate_RGB();
- redValue = int(redValue * brightness_LED);
- greenValue = int(greenValue * brightness_LED);
- blueValue = int(blueValue * brightness_LED);
- }
- void springGreenON() {
- // Spring Green
- redValue = 0;
- greenValue = 255;
- blueValue = 128;
- calculate_RGB();
- redValue = int(redValue * brightness_LED);
- greenValue = int(greenValue * brightness_LED);
- blueValue = int(blueValue * brightness_LED);
- }
- void cyanON() {
- // Cyan
- redValue = 0;
- greenValue = 255;
- blueValue = 255;
- calculate_RGB();
- redValue = int(redValue * brightness_LED);
- greenValue = int(greenValue * brightness_LED);
- blueValue = int(blueValue * brightness_LED);
- }
- void azureON() {
- // Azure
- redValue = 0;
- greenValue = 128;
- blueValue = 255;
- calculate_RGB();
- redValue = int(redValue * brightness_LED);
- greenValue = int(greenValue * brightness_LED);
- blueValue = int(blueValue * brightness_LED);
- }
- void blueON() {
- // Blue
- redValue = 0;
- greenValue = 0;
- blueValue = 255;
- calculate_RGB();
- redValue = int(redValue * brightness_LED);
- greenValue = int(greenValue * brightness_LED);
- blueValue = int(blueValue * brightness_LED);
- }
- void violetON() {
- // Violet
- redValue = 128;
- greenValue = 0;
- blueValue = 255;
- calculate_RGB();
- redValue = int(redValue * brightness_LED);
- greenValue = int(greenValue * brightness_LED);
- blueValue = int(blueValue * brightness_LED);
- }
- void magentaON() {
- // Magenta
- redValue = 255;
- greenValue = 0;
- blueValue = 255;
- calculate_RGB();
- redValue = int(redValue * brightness_LED);
- greenValue = int(greenValue * brightness_LED);
- blueValue = int(blueValue * brightness_LED);
- }
- void roseON() {
- // Rose
- redValue = 255;
- greenValue = 0;
- blueValue = 128;
- calculate_RGB();
- redValue = int(redValue * brightness_LED);
- greenValue = int(greenValue * brightness_LED);
- blueValue = int(blueValue * brightness_LED);
- }
- void whiteON() {
- // White (all LEDs on)
- redValue = 255;
- greenValue = 255;
- blueValue = 255;
- calculate_RGB();
- redValue = int(redValue * brightness_LED);
- greenValue = int(greenValue * brightness_LED);
- blueValue = int(blueValue * brightness_LED);
- }
- void calculate_RGB() {
- //use this to correctly light up LED depending on the setup
- if (RGB_type == common_anode) {
- /* If using a common anode LED, a pin
- should turn ON the LED when the pin is LOW.*/
- redValue = 255 - redValue;
- greenValue = 255 - greenValue;
- blueValue = 255 - blueValue;
- }
- else {
- /* If using a common cathode LED, an analog pin
- should turn on the LED when the pin is HIGH. The
- logic is flipped when using a Common Anode RGB LED
- strip, NPN BJT/N-Channel MOSFET, and microcontroller
- Leave RGB values as is, we're good!*/
- }
- }
- void show_RGB() {
- //once value is calculated, show the LED color
- analogWrite(redPin, redValue);
- analogWrite(greenPin, greenValue);
- analogWrite(bluePin, blueValue);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement