Advertisement
Guest User

RGB

a guest
Feb 8th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. /*
  2. * RGB.ino
  3. * Program to make the color of an RGB LED attached to an Arduino change based on the reading of a potentiometer.
  4. * Shows how to use RGB LEDs, potentiometers, and switch statements
  5. * 123D Circuits demo: https://123d.circuits.io/circuits/1543748-the-unnamed-circuit/
  6. * Codebender: https://codebender.cc/sketch:230656
  7. * Diagram of color changes: http://i.imgur.com/jDrOQUy.png
  8. * Schematic: http://i.imgur.com/hTR5oGr.png
  9. */
  10.  
  11. int redpin = 11; // setting pin #s for the colors
  12. int greenpin = 10;
  13. int bluepin = 9;
  14. int r, g, b = 0; // these will be used for color values; init to 0 for now
  15. int value;
  16.  
  17. void setup() { // run once, when the sketch starts
  18. pinMode(redpin, OUTPUT); // set pinmodes
  19. pinMode(greenpin, OUTPUT);
  20. pinMode(bluepin, OUTPUT);
  21. Serial.begin(9600); // set up Serial library at 9600 bps
  22. }
  23.  
  24. void loop() { // run over and over again
  25. value = analogRead(A0); //find potentiometer value
  26. Serial.println(value);
  27. switch ((value-value%170)/170){ //figures out how many times 170 goes into the value to the nearest whole number, then switches on this number
  28. //* this part of the code is for a common anode LED; remove the first slash to switch to common cathode
  29. case 0: // increases red
  30. r = 255-(value)*3/2; // because 0<value<170, 0<value for color<255 (it is a common anode so the values are reversed)
  31. g = 255;
  32. b = 255; // green and blue are not lit up
  33. break;
  34. case 1: // increases green
  35. r = 0;
  36. g = 255-(value-170)*3/2;
  37. b = 255;
  38. break;
  39. case 2: // increases blue
  40. r = 0;
  41. g = 0;
  42. b = 255-(value-340)*3/2;
  43. break;
  44. case 3: // decreases value for red
  45. r = (value-510)*3/2;
  46. g = 0;
  47. b = 0;
  48. break;
  49. case 4: // decreases value for green
  50. r = 255;
  51. g = (value-680)*3/2;
  52. b = 0;
  53. break;
  54. case 5: // decreases value for blue
  55. r = 255;
  56. g = 255;
  57. b = (value-850)*3/2;
  58. break;
  59. default: // since 170 * 6 is 1020, this will usually happen from 1020 to 1024, and it also works fine as a default if something goes wrong
  60. r = 255; // all colors are turned off
  61. g = 255;
  62. b = 255;
  63. break;
  64. /*/ this part of the code is for a common cathode LED
  65. case 0: // increases red
  66. r = (value)*3/2;
  67. g = 0;
  68. b = 0;
  69. break;
  70. case 1: // increases green
  71. r = 255;
  72. g = (value-170)*3/2;
  73. b = 0;
  74. break;
  75. case 2: // increases blue
  76. r = 255;
  77. g = 255;
  78. b = (value-340)*3/2;
  79. break;
  80. case 3: // decreases red
  81. r = 255-(value-510)*3/2;
  82. g = 255;
  83. b = 255;
  84. break;
  85. case 4: // decreases greeen
  86. r = 0;
  87. g = 255-(value-680)*3/2;
  88. b = 255;
  89. break;
  90. case 5: //decreases blue
  91. r = 0;
  92. g = 0;
  93. b = 255-(value-850)*3/2;
  94. break;
  95. default: //all colors are off
  96. r = 0;
  97. g = 0;
  98. b = 0;
  99. break;
  100. //*/
  101. }
  102. analogWrite(redpin, r); // setting colors to the led
  103. analogWrite(greenpin, g);
  104. analogWrite(bluepin, b);
  105. delay(10); //brief delay
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement