Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.69 KB | None | 0 0
  1. //Debug mode, comment one of these lines out using a syntax
  2. //for a single line comment ("//"):
  3. //#define DEBUG 0 //0 = LEDs only
  4. #define DEBUG 1 //1 = LEDs w/ serial output
  5.  
  6. // Define our LED pins:
  7. #define redPin 5
  8. #define greenPin 6
  9. #define bluePin 9
  10.  
  11. // Create integer variables for our LED color value:
  12. int redValue = 0;
  13. int greenValue = 0;
  14. int blueValue = 0;
  15.  
  16. //Create brightness variable
  17. //Ranging from 0.0-1.0:
  18. // 0.0 is off
  19. // 0.5 is 50%
  20. // 1.0 is fully on
  21. float brightness_LED = 0.1;
  22.  
  23. //Create variables for type of LED and if it is used with a transistor
  24. boolean common_anode = false;
  25. boolean common_cathode = true;//i.e.) When pin is HIGH, LED will also go HIGH without a transistor/PicoBuck
  26.  
  27. // Note:
  28. // Common Anode is `common_anode`
  29. // Common Cathode LED is `common_cathode`
  30. // Common Anode RGB LED Strip with transistor is `!common_anode`
  31. // RGB High Power LED with PicoBuck is also `!common_anode`
  32. boolean RGB_type = !common_anode;
  33.  
  34. int blinkRate = 1000; //in milliseconds
  35.  
  36.  
  37.  
  38. void setup() {
  39.  
  40. // Make all of our LED pins outputs:
  41. pinMode(redPin, OUTPUT);
  42. pinMode(greenPin, OUTPUT);
  43. pinMode(bluePin, OUTPUT);
  44.  
  45. allOFF(); //initialize LEDs with it turned off
  46. show_RGB(); //make sure to show it happening
  47.  
  48. #if DEBUG
  49. Serial.begin(9600); //initialize Serial Monitor
  50. //while (!Serial); // Comment out to wait for serial port to connect to Serial Monitor. Needed for native USB.
  51. Serial.println("Custom Color Mixing w/ an RGB LED.");
  52. Serial.println(" ");
  53. Serial.println("Note: Make sure to adjust the code for a common cathode or common anode.");
  54. Serial.println("Default is set to no color and off!");
  55. Serial.println(" ");
  56. #endif
  57.  
  58. }//end setup()
  59.  
  60.  
  61.  
  62.  
  63. void loop()
  64. {
  65. //used to visually check when Arduino is initialized
  66. redON();
  67. show_RGB();
  68. delay(blinkRate);
  69.  
  70. orangeON();
  71. show_RGB();
  72. delay(blinkRate);
  73.  
  74. yellowON();
  75. show_RGB();
  76. delay(blinkRate);
  77.  
  78. chartrueseON();
  79. show_RGB();
  80. delay(blinkRate);
  81.  
  82. greenON();
  83. show_RGB();
  84. delay(blinkRate);
  85.  
  86. springGreenON();
  87. show_RGB();
  88. delay(blinkRate);
  89.  
  90. cyanON();
  91. show_RGB();
  92. delay(blinkRate);
  93.  
  94. azureON();
  95. show_RGB();
  96. delay(blinkRate);
  97.  
  98. blueON();
  99. show_RGB();
  100. delay(blinkRate);
  101.  
  102. violetON();
  103. show_RGB();
  104. delay(blinkRate);
  105.  
  106. magentaON();
  107. show_RGB();
  108. delay(blinkRate);
  109.  
  110. roseON();
  111. show_RGB();
  112. delay(blinkRate);
  113.  
  114. whiteON();
  115. show_RGB();
  116. delay(blinkRate);
  117.  
  118. allOFF();
  119. show_RGB();
  120. delay(blinkRate);
  121. }//end loop
  122.  
  123.  
  124.  
  125. // ==================== CUSTOM FUNCTIONS DEFINED BELOW ====================
  126. void allOFF() {
  127. // Black (all LEDs off)
  128. // RGB LEDs:
  129. redValue = 0;
  130. greenValue = 0;
  131. blueValue = 0;
  132.  
  133. calculate_RGB();
  134.  
  135. redValue = int(redValue * brightness_LED);
  136. greenValue = int(greenValue * brightness_LED);
  137. blueValue = int(blueValue * brightness_LED);
  138. }
  139.  
  140. void redON() {
  141. // Red
  142. redValue = 255;
  143. greenValue = 0;
  144. blueValue = 0;
  145.  
  146. calculate_RGB();
  147.  
  148. redValue = int(redValue * brightness_LED);
  149. greenValue = int(greenValue * brightness_LED);
  150. blueValue = int(blueValue * brightness_LED);
  151. }
  152.  
  153. void orangeON() {
  154. // Orange
  155. redValue = 255;
  156. greenValue = 128;
  157. blueValue = 0;
  158.  
  159. calculate_RGB();
  160.  
  161. redValue = int(redValue * brightness_LED);
  162. greenValue = int(greenValue * brightness_LED);
  163. blueValue = int(blueValue * brightness_LED);
  164. }
  165.  
  166. void yellowON() {
  167. // Yellow
  168. redValue = 255;
  169. greenValue = 255;
  170. blueValue = 0;
  171.  
  172. calculate_RGB();
  173.  
  174. redValue = int(redValue * brightness_LED);
  175. greenValue = int(greenValue * brightness_LED);
  176. blueValue = int(blueValue * brightness_LED);
  177. }
  178.  
  179. void chartrueseON() {
  180. // Chartruese
  181. redValue = 128;
  182. greenValue = 255;
  183. blueValue = 0;
  184.  
  185. calculate_RGB();
  186.  
  187. redValue = int(redValue * brightness_LED);
  188. greenValue = int(greenValue * brightness_LED);
  189. blueValue = int(blueValue * brightness_LED);
  190. }
  191.  
  192. void greenON() {
  193. // Green
  194. redValue = 0;
  195. greenValue = 255;
  196. blueValue = 0;
  197.  
  198. calculate_RGB();
  199.  
  200. redValue = int(redValue * brightness_LED);
  201. greenValue = int(greenValue * brightness_LED);
  202. blueValue = int(blueValue * brightness_LED);
  203. }
  204.  
  205. void springGreenON() {
  206. // Spring Green
  207. redValue = 0;
  208. greenValue = 255;
  209. blueValue = 128;
  210.  
  211. calculate_RGB();
  212.  
  213. redValue = int(redValue * brightness_LED);
  214. greenValue = int(greenValue * brightness_LED);
  215. blueValue = int(blueValue * brightness_LED);
  216. }
  217.  
  218. void cyanON() {
  219. // Cyan
  220. redValue = 0;
  221. greenValue = 255;
  222. blueValue = 255;
  223.  
  224. calculate_RGB();
  225.  
  226. redValue = int(redValue * brightness_LED);
  227. greenValue = int(greenValue * brightness_LED);
  228. blueValue = int(blueValue * brightness_LED);
  229. }
  230.  
  231. void azureON() {
  232. // Azure
  233. redValue = 0;
  234. greenValue = 128;
  235. blueValue = 255;
  236.  
  237. calculate_RGB();
  238.  
  239. redValue = int(redValue * brightness_LED);
  240. greenValue = int(greenValue * brightness_LED);
  241. blueValue = int(blueValue * brightness_LED);
  242. }
  243.  
  244. void blueON() {
  245. // Blue
  246. redValue = 0;
  247. greenValue = 0;
  248. blueValue = 255;
  249.  
  250. calculate_RGB();
  251.  
  252. redValue = int(redValue * brightness_LED);
  253. greenValue = int(greenValue * brightness_LED);
  254. blueValue = int(blueValue * brightness_LED);
  255. }
  256.  
  257. void violetON() {
  258. // Violet
  259. redValue = 128;
  260. greenValue = 0;
  261. blueValue = 255;
  262.  
  263. calculate_RGB();
  264.  
  265. redValue = int(redValue * brightness_LED);
  266. greenValue = int(greenValue * brightness_LED);
  267. blueValue = int(blueValue * brightness_LED);
  268. }
  269.  
  270. void magentaON() {
  271. // Magenta
  272. redValue = 255;
  273. greenValue = 0;
  274. blueValue = 255;
  275.  
  276. calculate_RGB();
  277.  
  278. redValue = int(redValue * brightness_LED);
  279. greenValue = int(greenValue * brightness_LED);
  280. blueValue = int(blueValue * brightness_LED);
  281. }
  282.  
  283. void roseON() {
  284. // Rose
  285. redValue = 255;
  286. greenValue = 0;
  287. blueValue = 128;
  288.  
  289. calculate_RGB();
  290.  
  291. redValue = int(redValue * brightness_LED);
  292. greenValue = int(greenValue * brightness_LED);
  293. blueValue = int(blueValue * brightness_LED);
  294. }
  295.  
  296. void whiteON() {
  297. // White (all LEDs on)
  298. redValue = 255;
  299. greenValue = 255;
  300. blueValue = 255;
  301.  
  302. calculate_RGB();
  303.  
  304. redValue = int(redValue * brightness_LED);
  305. greenValue = int(greenValue * brightness_LED);
  306. blueValue = int(blueValue * brightness_LED);
  307. }
  308.  
  309.  
  310.  
  311. void calculate_RGB() {
  312. //use this to correctly light up LED depending on the setup
  313. if (RGB_type == common_anode) {
  314. /* If using a common anode LED, a pin
  315. should turn ON the LED when the pin is LOW.*/
  316. redValue = 255 - redValue;
  317. greenValue = 255 - greenValue;
  318. blueValue = 255 - blueValue;
  319.  
  320. }
  321. else {
  322. /* If using a common cathode LED, an analog pin
  323. should turn on the LED when the pin is HIGH. The
  324. logic is flipped when using a Common Anode RGB LED
  325. strip, NPN BJT/N-Channel MOSFET, and microcontroller
  326.  
  327. Leave RGB values as is, we're good!*/
  328. }
  329. }
  330.  
  331. void show_RGB() {
  332. //once value is calculated, show the LED color
  333. analogWrite(redPin, redValue);
  334. analogWrite(greenPin, greenValue);
  335. analogWrite(bluePin, blueValue);
  336. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement