Advertisement
Guest User

Untitled

a guest
Feb 13th, 2011
2,082
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.48 KB | None | 0 0
  1. //---bof---RGBL-Analog Preamble
  2. //RGB LED pins
  3. int ledAnalogOne[] = {3, 5, 6}; //the three pins of the first analog LED 3 = redPin, 5 = greenPin, 6 = bluePin
  4. //These pins must be PWM
  5. int ledAnalogTwo[] = {9, 10, 11}; //the three pins of the second analog LED 9 = redPin, 10 = greenPin, 11 = bluePin
  6. //These pins must be PWM
  7.  
  8. //Defined Colors (different RGB (red, green, blue) values for colors
  9. //(to add your own ie. fuscia experiment and then add to the list)
  10. const byte RED[] = {255, 0, 0};
  11. const byte ORANGE[] = {83, 4, 0};
  12. const byte YELLOW[] = {255, 255, 0};
  13. const byte GREEN[] = {0, 255, 0};
  14. const byte BLUE[] = {0, 0, 255};
  15. const byte INDIGO[] = {4, 0, 19};
  16. const byte VIOLET[] =
  17. {23, 0, 22};
  18. const byte CYAN[] = {0, 255, 255};
  19. const byte MAGENTA[] = {255, 0, 255};
  20. const byte WHITE[] = {255, 255, 255};
  21. const byte BLACK[] = {0, 0, 0};
  22. const byte PINK[] = {158, 4, 79};
  23.  
  24. //---eof---RGBL-Analog Preamble
  25.  
  26. void setup(){
  27.   for(int i = 0; i < 3; i++){
  28.     pinMode(ledAnalogOne[i], OUTPUT);
  29.     //Set the three LED pins as outputs
  30.     pinMode(ledAnalogTwo[i], OUTPUT);
  31.     //Set the three LED pins as outputs
  32.   }
  33.   setColor(ledAnalogOne, BLACK);
  34.   //Turn off led 1
  35.   setColor(ledAnalogTwo, BLACK);
  36.   //Turn off led 2
  37. }
  38.  
  39. void loop(){
  40.   /* Example 1 - Defined Colors
  41.   Set to a known color (you can use any of the above defined colors)*/
  42.   setColor(ledAnalogOne, MAGENTA);
  43.   /* Example 2 - Any Color
  44.   Set the LED to any color you like*/
  45.   //byte tempColor[] = {12,34,12};
  46.   //the RGB (red, green, blue) value for a color to display
  47.   //setColor(ledAnalogOne, tempColor);
  48.   /*Example 3 - Fading
  49.   Fade the LED between two colors (this will go from red to green to blue then back to red)*/
  50.   //fadeToColor(ledAnalogOne, RED, GREEN, 10);
  51.   //fadeToColor takes 4 parameters
  52.   //ledAnalogOne - an array with 3 values defining the red, green and blue pins of the LED
  53.   //RED - This is the start color
  54.   //GREEN - This is the end color
  55.   //10 - the delay (in milliseconds between updates) (determines the fade speed)
  56.   //fadeToColor(ledAnalogOne, GREEN, BLUE, 10);
  57.   //Fades from Green to Blue
  58.   //fadeToColor(ledAnalogOne, BLUE, RED, 10);
  59.   //Fades from Blue to Red
  60. }
  61.  
  62. /* Sets the color of the LED to any RGB Value
  63. led - (int array of three values defining the LEDs pins (led[0] = redPin, led[1] = greenPin, led[2] = bluePin))
  64. color - (byte array of three values defing an RGB color to display (color[0] = new Red value, color[1] = new Green value, color[2] = new Red value*/
  65. void setColor(int* led, byte* color){
  66.   for(int i = 0; i < 3; i++){
  67.     //iterate through each of the three pins (red green blue)
  68.     analogWrite(led[i], 255 - color[i]);
  69.   //set the analog output value of each pin to the input value (ie led[0] (red pin) to 255- color[0] (red input color)
  70.   //we use 255 - the value because our RGB LED is common anode, this means a color is full on when we output analogWrite(pin, 0)
  71.   //and off when we output analogWrite(pin, 255).
  72.   }
  73. }
  74.  
  75. /* A version of setColor that takes a predefined color
  76. (neccesary to allow const int pre-defined colors */
  77. void setColor(int* led, const byte* color){
  78.   byte tempByte[] = {color[0], color[1], color[2]};
  79.   setColor(led, tempByte);
  80. }
  81.  
  82. /* Fades the LED from a start color to an end color at fadeSpeed
  83. led - (int array of three values defining the LEDs pins (led[0] = redPin, led[1] = greenPin, led[2] = bluePin))
  84. startColor - (byte array of three values defing the start RGB color (startColor[0] = start Red value, startColor[1] = start Green value, startColor[2] = start Red value
  85. endColor - (byte array of three values defing the finished RGB color (endColor[0] = end Red value, endColor[1] = end Green value, endColor[2] = end Red value
  86. fadeSpeed - this is the delay in milliseconds between steps, defines the speed of the fade*/
  87. void fadeToColor(int* led, byte* startColor, byte* endColor, int fadeSpeed){
  88.   int changeRed = endColor[0] - startColor[0];
  89.   //the difference in the two colors for the red channel
  90.   int changeGreen = endColor[1] - startColor[1];
  91.   //the difference in the two colors for the green channel
  92.   int changeBlue = endColor[2] - startColor[2];
  93.   //the difference in the two colors for the blue channel
  94.   int steps = max(abs(changeRed),max(abs(changeGreen), abs(changeBlue)));
  95.   //make the number of change steps the maximum channel change
  96.   for(int i = 0 ; i < steps; i++){
  97.     //iterate for the channel with the maximum change
  98.     byte newRed = startColor[0] + (i * changeRed / steps);
  99.     //the newRed intensity dependant on the start intensity and the change determined above
  100.     byte newGreen = startColor[1] + (i * changeGreen / steps);
  101.     //the newGreen intensity
  102.     byte newBlue = startColor[2] + (i * changeBlue / steps);
  103.     //the newBlue intensity
  104.     byte newColor[] = {newRed, newGreen, newBlue};
  105.     //Define an RGB color array for the new color
  106.     setColor(led, newColor);
  107.     //Set the LED to the calculated value
  108.     delay(fadeSpeed);
  109.     //Delay fadeSpeed milliseconds before going on to the next color
  110.   }
  111.   setColor(led, endColor);
  112.   //The LED should be at the endColor but set to endColor to avoid rounding errors}
  113.  
  114.  
  115. /* A version of fadeToColor that takes predefined colors (necessary to allow const int pre-defined colors */
  116. void fadeToColor(int* led, const byte* startColor, const byte* endColor, int fadeSpeed){
  117.   byte tempByte1[] = {startColor[0], startColor[1], startColor[2]};
  118.   byte tempByte2[] = {endColor[0], endColor[1], endColor[2]};
  119.   fadeToColor(led, tempByte1, tempByte2, fadeSpeed);
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement