Advertisement
Oliver_F

Untitled

Sep 30th, 2017
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1.  
  2. int RED = 11; // RED pin of the LED to PWM pin 9
  3. int GREEN = 10; // GREEN pin of the LED to PWM pin 10
  4. int BLUE = 9; // BLUE pin of the LED to PWM pin 11
  5.  
  6. int DELAY_TIME = 10; //changes speed of fading
  7. int MAX_BRIGHT = 255; //sets maximum brightness, 255 max brightness
  8. int COLOR_MIX = 0; //variable to change the colors that are mixed in switch statement
  9.  
  10. void fade_in(int x) //loop that gradually turns the LED on using PWM
  11. {
  12. int counter;
  13.  
  14. for(counter = 0; counter < x; counter++)
  15. {
  16. led_mixer(COLOR_MIX, counter);
  17. delay(DELAY_TIME);
  18. }
  19. }
  20.  
  21. void fade_out(int x) //loop that gradually turns the LED off using PWM
  22. {
  23. int counter;
  24.  
  25. for(counter = x; counter > 0; counter--)
  26. {
  27. led_mixer(COLOR_MIX, counter);
  28. delay(DELAY_TIME);
  29. }
  30. }
  31.  
  32. void led_mixer(int color, int x) //uses switch statement to mix color combinations
  33. {
  34. switch(color)
  35. {
  36. case 0:
  37. analogWrite(RED, x);
  38. analogWrite(GREEN, x);
  39. break;
  40. case 1:
  41. analogWrite(GREEN, x);
  42. analogWrite(GREEN, x);
  43. analogWrite(RED, x);
  44. break;
  45. case 2:
  46. analogWrite(RED, x);
  47. break;
  48. }
  49. }
  50.  
  51. void setup()
  52. {
  53. // nothing for setup
  54. }
  55.  
  56. void loop() //loop forever
  57. {
  58. fade_in(MAX_BRIGHT); //gradually turn the LED on to max brightness
  59. fade_out(MAX_BRIGHT); //gradually turn off the LED
  60. COLOR_MIX++; //increment to the next color combination
  61.  
  62. if(COLOR_MIX == 7) //if all color combinations have been displayed, reset the cycle
  63. {
  64. COLOR_MIX = 0;
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement