Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.06 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 colorMode = 1; //color mode to control LED color
  35.  
  36. int prev_FadeVal = 0;
  37. int current_FadeVal = 0;
  38. boolean increasing = true;
  39. int fadeVal = 5; //value to step when increasing/decreasing, recommended to be 1 or 5, larger numbers will have problems lighting up
  40. int fadeMAX = 255; //maximum fade value
  41. int fadeMIN = 0; //minimum fade value
  42. int fadeDelay = 30;//delay between each step
  43.  
  44.  
  45.  
  46.  
  47. void setup() {
  48.  
  49. // Make all of our LED pins outputs:
  50. pinMode(redPin, OUTPUT);
  51. pinMode(greenPin, OUTPUT);
  52. pinMode(bluePin, OUTPUT);
  53.  
  54. allOFF(); //make sure to initialize LEDs with it turned off
  55. show_RGB(); //make sure to show it happening
  56.  
  57. #if DEBUG
  58. Serial.begin(9600); //initialize Serial Monitor
  59. //while (!Serial); // Comment out to wait for serial port to connect to Serial Monitor. Needed for native USB.
  60. Serial.println("Custom Color Mixing w/ an RGB LED.");
  61. Serial.println(" ");
  62. Serial.println("Note: Make sure to adjust the code for a common cathode or common anode.");
  63. Serial.println("Default is set to no color and off!");
  64. Serial.println(" ");
  65. #endif
  66.  
  67. }//end setup()
  68.  
  69. void loop()
  70. {
  71.  
  72.  
  73. switch (colorMode) {
  74. case 1://FADE RED
  75. redValue = current_FadeVal;
  76. greenValue = 0;
  77. blueValue = 0;
  78.  
  79. calculate_RGB();
  80.  
  81. redValue = int(redValue * brightness_LED);
  82. greenValue = int(greenValue * brightness_LED);
  83. blueValue = int(blueValue * brightness_LED);
  84. break;
  85. case 2://FADE ORANGE
  86. redValue = current_FadeVal;
  87. greenValue = current_FadeVal * 0.498; // 128/255 = ~0.498039
  88. blueValue = 0;
  89.  
  90. calculate_RGB();
  91.  
  92. redValue = int(redValue * brightness_LED);
  93. greenValue = int(greenValue * brightness_LED);
  94. blueValue = int(blueValue * brightness_LED);
  95.  
  96. if (redValue > 0 && greenValue == 0) {
  97. //tertiary component is 1/2, so when it calculates to decimal with fade value,
  98. //it will be basically be off, make sure to turn off other color so that
  99. //it does not just show the other color
  100. redValue = 0;
  101. }
  102.  
  103. // takes x amount of steps if you do not set it to zero for certain brightness (i.e. takes 8 more steps to turn off for 0.1)
  104. //Serial.print("Red Value =");
  105. //Serial.println( int((current_FadeVal) * brightness_LED));
  106.  
  107. //Serial.print("Green Value =");
  108. //Serial.println( int((current_FadeVal * 0.498) * brightness_LED));
  109. break;
  110. case 3://FADE YELLOW
  111. redValue = current_FadeVal;
  112. greenValue = current_FadeVal;
  113. blueValue = 0;
  114.  
  115. calculate_RGB();
  116.  
  117. redValue = int(redValue * brightness_LED);
  118. greenValue = int(greenValue * brightness_LED);
  119. blueValue = int(blueValue * brightness_LED);
  120. break;
  121. case 4://FADE CHARTRUESE
  122. redValue = current_FadeVal * 0.498; // 128/255 = ~0.498039
  123. greenValue = current_FadeVal;
  124. blueValue = 0;
  125.  
  126. calculate_RGB();
  127.  
  128. redValue = int(redValue * brightness_LED);
  129. greenValue = int(greenValue * brightness_LED);
  130. blueValue = int(blueValue * brightness_LED);
  131.  
  132. if (greenValue > 0 && redValue == 0) {
  133. //tertiary component is 1/2, so when it calculates to decimal with fade value,
  134. //it will be basically be off, make sure to turn off other color so that
  135. //it does not just show the other color
  136. greenValue = 0;
  137. }
  138. break;
  139. case 5://FADE GREEN
  140. redValue = 0;
  141. greenValue = current_FadeVal;
  142. blueValue = 0;
  143.  
  144. calculate_RGB();
  145.  
  146. redValue = int(redValue * brightness_LED);
  147. greenValue = int(greenValue * brightness_LED);
  148. blueValue = int(blueValue * brightness_LED);
  149. break;
  150. case 6://FADE SPRING GREEN
  151. redValue = 0;
  152. greenValue = current_FadeVal;
  153. blueValue = current_FadeVal * 0.498; // 128/255 = ~0.498039
  154.  
  155. calculate_RGB();
  156.  
  157. redValue = int(redValue * brightness_LED);
  158. greenValue = int(greenValue * brightness_LED);
  159. blueValue = int(blueValue * brightness_LED);
  160.  
  161. if (greenValue > 0 && blueValue == 0) {
  162. //tertiary component is 1/2, so when it calculates to decimal with fade value,
  163. //it will be basically be off, make sure to turn off other color so that
  164. //it does not just show the other color
  165. greenValue = 0;
  166. }
  167. break;
  168. case 7://FADE CYAN
  169. redValue = 0;
  170. greenValue = current_FadeVal;
  171. blueValue = current_FadeVal;
  172.  
  173. calculate_RGB();
  174.  
  175. redValue = int(redValue * brightness_LED);
  176. greenValue = int(greenValue * brightness_LED);
  177. blueValue = int(blueValue * brightness_LED);
  178. break;
  179. case 8://FADE AZURE
  180. redValue = 0;
  181. greenValue = current_FadeVal * 0.498; // 128/255 = ~0.498039
  182. blueValue = current_FadeVal;
  183.  
  184. calculate_RGB();
  185.  
  186. redValue = int(redValue * brightness_LED);
  187. greenValue = int(greenValue * brightness_LED);
  188. blueValue = int(blueValue * brightness_LED);
  189.  
  190. if (blueValue > 0 && greenValue == 0) {
  191. //tertiary component is 1/2, so when it calculates to decimal with fade value,
  192. //it will be basically be off, make sure to turn off other color so that
  193. //it does not just show the other color
  194. blueValue = 0;
  195. }
  196. break;
  197. case 9://FADE BLUE
  198. redValue = 0;
  199. greenValue = 0;
  200. blueValue = current_FadeVal;
  201.  
  202. calculate_RGB();
  203.  
  204. redValue = int(redValue * brightness_LED);
  205. greenValue = int(greenValue * brightness_LED);
  206. blueValue = int(blueValue * brightness_LED);
  207. break;
  208. case 10://FADE VIOLET
  209. redValue = current_FadeVal * 0.498;
  210. greenValue = 0;
  211. blueValue = current_FadeVal;
  212.  
  213. calculate_RGB();
  214.  
  215. redValue = int(redValue * brightness_LED);// 128/255 = ~0.498039
  216. greenValue = int(greenValue * brightness_LED);
  217. blueValue = int(blueValue * brightness_LED);
  218.  
  219. if (blueValue > 0 && redValue == 0) {
  220. //tertiary component is 1/2, so when it calculates to decimal with fade value,
  221. //it will be basically be off, make sure to turn off other color so that
  222. //it does not just show the other color
  223. blueValue = 0;
  224. }
  225. break;
  226. case 11://FADE MAGENTA
  227. redValue = current_FadeVal;
  228. greenValue = 0;
  229. blueValue = current_FadeVal;
  230.  
  231. calculate_RGB();
  232.  
  233. redValue = int(redValue * brightness_LED);
  234. greenValue = int(greenValue * brightness_LED);
  235. blueValue = int(blueValue * brightness_LED);
  236. break;
  237. case 12://FADE ROSE
  238. redValue = current_FadeVal;
  239. greenValue = 0;
  240. blueValue = current_FadeVal * 0.498;
  241.  
  242. calculate_RGB();
  243.  
  244. redValue = int(redValue * brightness_LED);
  245. greenValue = int(greenValue * brightness_LED);
  246. blueValue = int(blueValue * brightness_LED);// 128/255 = ~0.498039
  247.  
  248. if (redValue > 0 && blueValue == 0) {
  249. //tertiary component is 1/2, so when it calculates to decimal with fade value,
  250. //it will be basically be off, make sure to turn off other color so that
  251. //it does not just show the other color
  252. redValue = 0;
  253. }
  254. break;
  255. case 13://FADE WHITE
  256. redValue = current_FadeVal;
  257. greenValue = current_FadeVal;
  258. blueValue = current_FadeVal;
  259.  
  260. redValue = int(redValue * brightness_LED);
  261. greenValue = int(greenValue * brightness_LED);
  262. blueValue = int(blueValue * brightness_LED);
  263. break;
  264. default:
  265. allOFF();
  266. break;
  267. }
  268. show_RGB();
  269. delay(fadeDelay);
  270.  
  271.  
  272. if (increasing == true) {
  273. //increasing
  274. current_FadeVal += fadeVal;
  275. }
  276. else {
  277. //decreasing
  278. current_FadeVal -= fadeVal;
  279. }
  280.  
  281. if (current_FadeVal > fadeMAX) {
  282. increasing = false;
  283. prev_FadeVal -= fadeVal;//undo addition
  284.  
  285. current_FadeVal = prev_FadeVal;
  286. }
  287. else if (current_FadeVal < fadeMIN) {
  288. increasing = true;
  289. prev_FadeVal += fadeVal;//undo subtraction
  290.  
  291. current_FadeVal = prev_FadeVal;
  292.  
  293. colorMode += 1;//next color
  294. if (colorMode > 13) {
  295. colorMode = 0;
  296. }
  297. }
  298.  
  299. prev_FadeVal = current_FadeVal;
  300.  
  301. }//END LOOP
  302.  
  303.  
  304.  
  305.  
  306. // ==================== CUSTOM FUNCTIONS DEFINED BELOW ====================
  307. void allOFF() {
  308. // Black (all LEDs off)
  309. // RGB LEDs:
  310. redValue = 0;
  311. greenValue = 0;
  312. blueValue = 0;
  313.  
  314. calculate_RGB();
  315.  
  316. redValue = int(redValue * brightness_LED);
  317. greenValue = int(greenValue * brightness_LED);
  318. blueValue = int(blueValue * brightness_LED);
  319. }
  320.  
  321. void redON() {
  322. // Red
  323. redValue = 255;
  324. greenValue = 0;
  325. blueValue = 0;
  326.  
  327. calculate_RGB();
  328.  
  329. redValue = int(redValue * brightness_LED);
  330. greenValue = int(greenValue * brightness_LED);
  331. blueValue = int(blueValue * brightness_LED);
  332. }
  333.  
  334. void orangeON() {
  335. // Orange
  336. redValue = 255;
  337. greenValue = 128;
  338. blueValue = 0;
  339.  
  340. calculate_RGB();
  341.  
  342. redValue = int(redValue * brightness_LED);
  343. greenValue = int(greenValue * brightness_LED);
  344. blueValue = int(blueValue * brightness_LED);
  345. }
  346.  
  347. void yellowON() {
  348. // Yellow
  349. redValue = 255;
  350. greenValue = 255;
  351. blueValue = 0;
  352.  
  353. calculate_RGB();
  354.  
  355. redValue = int(redValue * brightness_LED);
  356. greenValue = int(greenValue * brightness_LED);
  357. blueValue = int(blueValue * brightness_LED);
  358. }
  359.  
  360. void chartrueseON() {
  361. // Chartruese
  362. redValue = 128;
  363. greenValue = 255;
  364. blueValue = 0;
  365.  
  366. calculate_RGB();
  367.  
  368. redValue = int(redValue * brightness_LED);
  369. greenValue = int(greenValue * brightness_LED);
  370. blueValue = int(blueValue * brightness_LED);
  371. }
  372.  
  373. void greenON() {
  374. // Green
  375. redValue = 0;
  376. greenValue = 255;
  377. blueValue = 0;
  378.  
  379. calculate_RGB();
  380.  
  381. redValue = int(redValue * brightness_LED);
  382. greenValue = int(greenValue * brightness_LED);
  383. blueValue = int(blueValue * brightness_LED);
  384. }
  385.  
  386. void springGreenON() {
  387. // Spring Green
  388. redValue = 0;
  389. greenValue = 255;
  390. blueValue = 128;
  391.  
  392. calculate_RGB();
  393.  
  394. redValue = int(redValue * brightness_LED);
  395. greenValue = int(greenValue * brightness_LED);
  396. blueValue = int(blueValue * brightness_LED);
  397. }
  398.  
  399. void cyanON() {
  400. // Cyan
  401. redValue = 0;
  402. greenValue = 255;
  403. blueValue = 255;
  404.  
  405. calculate_RGB();
  406.  
  407. redValue = int(redValue * brightness_LED);
  408. greenValue = int(greenValue * brightness_LED);
  409. blueValue = int(blueValue * brightness_LED);
  410. }
  411.  
  412. void azureON() {
  413. // Azure
  414. redValue = 0;
  415. greenValue = 128;
  416. blueValue = 255;
  417.  
  418. calculate_RGB();
  419.  
  420. redValue = int(redValue * brightness_LED);
  421. greenValue = int(greenValue * brightness_LED);
  422. blueValue = int(blueValue * brightness_LED);
  423. }
  424.  
  425. void blueON() {
  426. // Blue
  427. redValue = 0;
  428. greenValue = 0;
  429. blueValue = 255;
  430.  
  431. calculate_RGB();
  432.  
  433. redValue = int(redValue * brightness_LED);
  434. greenValue = int(greenValue * brightness_LED);
  435. blueValue = int(blueValue * brightness_LED);
  436. }
  437.  
  438. void violetON() {
  439. // Violet
  440. redValue = 128;
  441. greenValue = 0;
  442. blueValue = 255;
  443.  
  444. calculate_RGB();
  445.  
  446. redValue = int(redValue * brightness_LED);
  447. greenValue = int(greenValue * brightness_LED);
  448. blueValue = int(blueValue * brightness_LED);
  449. }
  450.  
  451. void magentaON() {
  452. // Magenta
  453. redValue = 255;
  454. greenValue = 0;
  455. blueValue = 255;
  456.  
  457. calculate_RGB();
  458.  
  459. redValue = int(redValue * brightness_LED);
  460. greenValue = int(greenValue * brightness_LED);
  461. blueValue = int(blueValue * brightness_LED);
  462. }
  463.  
  464. void roseON() {
  465. // Rose
  466. redValue = 255;
  467. greenValue = 0;
  468. blueValue = 128;
  469.  
  470. calculate_RGB();
  471.  
  472. redValue = int(redValue * brightness_LED);
  473. greenValue = int(greenValue * brightness_LED);
  474. blueValue = int(blueValue * brightness_LED);
  475. }
  476.  
  477. void whiteON() {
  478. // White (all LEDs on)
  479. redValue = 255;
  480. greenValue = 255;
  481. blueValue = 255;
  482.  
  483. calculate_RGB();
  484.  
  485. redValue = int(redValue * brightness_LED);
  486. greenValue = int(greenValue * brightness_LED);
  487. blueValue = int(blueValue * brightness_LED);
  488. }
  489.  
  490.  
  491.  
  492. void calculate_RGB() {
  493. //use this to correctly light up LED depending on the setup
  494. if (RGB_type == common_anode) {
  495. /* If using a common anode LED, a pin
  496. should turn ON the LED when the pin is LOW.*/
  497. redValue = 255 - redValue;
  498. greenValue = 255 - greenValue;
  499. blueValue = 255 - blueValue;
  500.  
  501. }
  502. else {
  503. /* If using a common cathode LED, an analog pin
  504. should turn on the LED when the pin is HIGH. The
  505. logic is flipped when using a Common Anode RGB LED
  506. strip, NPN BJT/N-Channel MOSFET, and microcontroller
  507.  
  508. Leave RGB values as is, we're good!*/
  509. }
  510. }
  511.  
  512. void show_RGB() {
  513. //once value is calculated, show the LED color
  514. analogWrite(redPin, redValue);
  515. analogWrite(greenPin, greenValue);
  516. analogWrite(bluePin, blueValue);
  517. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement