Guest User

Untitled

a guest
Jan 21st, 2018
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. /*
  2. * Code for cross-fading 3 LEDs, red, green and blue, or one tri-color LED, using PWM
  3. * The program cross-fades slowly from red to green, green to blue, and blue to red
  4. * Clay Shirky <clay.shirky@nyu.edu>
  5. */
  6.  
  7.  
  8. int redPin = 9; // Red LED, connected to digital pin 9
  9. int greenPin = 10; // Green LED, connected to digital pin 10
  10. int bluePin = 11; // Blue LED, connected to digital pin 11
  11.  
  12. // Program variables
  13. int redVal = 255; // Variables to store the values to send to the pins
  14. int greenVal = 1; // Initial values are Red full, Green and Blue off
  15. int blueVal = 1;
  16.  
  17. int i = 0; // Loop counter
  18. int wait = 50; // 50ms (.05 second) delay; shorten for faster fades
  19.  
  20. void setup()
  21. {
  22. pinMode(redPin, OUTPUT); // sets the pins as output
  23. pinMode(greenPin, OUTPUT);
  24. pinMode(bluePin, OUTPUT);
  25. }
  26.  
  27. // Main program
  28. void loop()
  29. {
  30. i += 1; // Increment counter
  31. if (i < 255) // First phase of fades
  32. {
  33. redVal -= 1; // Red down
  34. greenVal += 1; // Green up
  35. blueVal = 1; // Blue low
  36. }
  37. else if (i < 509) // Second phase of fades
  38. {
  39. redVal = 1; // Red low
  40. greenVal -= 1; // Green down
  41. blueVal += 1; // Blue up
  42. }
  43. else if (i < 763) // Third phase of fades
  44. {
  45. redVal += 1; // Red up
  46. greenVal = 1; // Green low
  47. blueVal -= 1; // Blue down
  48. }
  49. else // Re-set the counter, and start the fades again
  50. {
  51. i = 1;
  52. }
  53.  
  54. analogWrite(redPin, redVal); // Write current values to LED pins
  55. analogWrite(greenPin, greenVal);
  56. analogWrite(bluePin, blueVal);
  57. }
Add Comment
Please, Sign In to add comment