Advertisement
elevenbytes

Arduino Ambient Mood Light

Oct 25th, 2011
14,054
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.37 KB | None | 0 0
  1. // Ambient RGB light
  2. // Author: Kristoffer Scott
  3. //
  4. // Controls RGB LEDs; smoothly transitions through color values.
  5.  
  6. // Delay time: sets the time in milliseconds between loop iterations.
  7. // Make this value large for slower transitions.
  8. #define DELAY_TIME 500
  9.  
  10. // Maximum Brightness: the maximum level the pins will reach.
  11. #define MAX_BRIGHT 255
  12.  
  13. // The pins which each color value is output to.
  14. #define PIN_RED 9
  15. #define PIN_GREEN 10
  16. #define PIN_BLUE 11
  17.  
  18. // The initial values of each color.
  19. int red = 0;
  20. int green = 170;
  21. int blue = 170;
  22.  
  23. // Indicates whether a color is incrementing (1) or decrementing (0).
  24. int incR = 1;
  25. int incG = 1;
  26. int incB = 0;
  27.  
  28. // Smoothly changes the color values
  29. void transition()
  30. {
  31.   if (red >= MAX_BRIGHT)
  32.     incR = 0;
  33.   else if (red <= 0)
  34.     incR = 1;
  35.   if (green >= MAX_BRIGHT)
  36.     incG = 0;
  37.   else if (green <= 0)
  38.     incG = 1;
  39.   if (blue >= MAX_BRIGHT)
  40.     incB = 0;
  41.   else if (blue <= 0)
  42.     incB = 1;
  43.  
  44.   if (incR)
  45.     red++;
  46.   else
  47.     red--;
  48.   if(incG)
  49.     green++;
  50.   else
  51.     green--;
  52.   if(incB)
  53.     blue++;
  54.   else
  55.     blue--;
  56. }
  57.  
  58. // Sets the output voltage on the LED pins.
  59. void setColor()
  60. {
  61.   analogWrite(PIN_RED, red);
  62.   analogWrite(PIN_GREEN, green);
  63.   analogWrite(PIN_BLUE, blue);
  64. }
  65.  
  66. void setup()
  67. {
  68.   // Do nothing.
  69. }
  70.  
  71. void loop()
  72. {
  73.   transition();
  74.   setColor();
  75.   delay(DELAY_TIME);
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement