Advertisement
skizziks_53

pretty_vandal_plusplus

May 25th, 2017
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 28.89 KB | None | 0 0
  1. /*
  2. Pretty Vandal PlusPlus
  3. Notes:
  4.  
  5. 1. This code is intended for a 5-volt Arduino Nano or Nano clone, or an Uno or Uno clone. It uses 12% of program storage space and 13% of dynamic memory.
  6. 2. This code is written to operate a single 3-color RGB LED and the single color power-indicator and hard-drive-activity-indicator LEDs by PWM. It uses hardware PWM pins, not software PWM.
  7. 3. For the RGB LED, at the minimums--the red LED should have a 150 ohm resistor, green needs a 100 ohm and blue needs a 100 ohm. In my version I used 220 ohms for the red and 150 ohms each for the green and blue.
  8. 4. For the single-color power and hard-drive-activity LEDs, the minimum resistor value will vary depending on the LED colors but it is safe to use a 220-ohm resistor for any color.
  9.  
  10. This program allows having two separate color RGB values: one for the power/sleep indication, and the other for the hard-drive activity indication.
  11. Additionally, each of the above two conditions has two transitional colors for the rising-value and the decaying-value.
  12. The colors are made to transition smoothly into each other.
  13.  
  14. The single-color power and hard-drive LEDs can be operated also.
  15. Both have a maximum brightness setting, and have separate settings for how fast the light levels of each LED rise and decay.
  16. */
  17.  
  18. /*
  19. The three settings below control if each feature is enabled or not.
  20. Technically it doesn't matter if they are left on and just left unconnected, but anyway.
  21. If you wanted to use those pins for anything else you added, you would need to disable the LED functions here from altering them.
  22. */
  23. boolean use_RGB_output = true; // This controls the single RGB LED or button.
  24. boolean use_case_power_LED = true; // This controls the PC case normal power LED.
  25. boolean use_case_hardDrive_LED = true;// This controls the PC case normal hard-drive-activity LED.
  26.  
  27.  
  28. /*
  29. The RGB LED color levels are set here, in the first 18 variables stated.
  30. The first nine are for the power indicator colors, and the second nine are for the hard-drive activity indicator colors.
  31. Each RGB LED has a value of 0 to 255 for each of the primary LED colors.
  32. The first three variables below state the maximum value for that primary color that will be used.
  33. The second set controls how fast that each of the three primary colors will rise, if the indicator state is positive.
  34. The third set is the amount that each of the three primary colors decrease, if the indicator state is negative.
  35.  
  36. Since you mostly see the power in a constant state of being [on] with the hard drive signal flickering on and off, it may be more useful to set the decay values relative to each other's maximum values rather than zero.
  37. */
  38.  
  39. // ###############################################################################################################################################
  40.  
  41. // Power indicator maximum values, can range from 0 to 255. These values may be zero, but if all three are set to zero, then the LED of that primary color will not light up at all in that color.
  42. int power_indicator_red_maximum_value = 0;
  43. int power_indicator_green_maximum_value = 100;
  44. int power_indicator_blue_maximum_value = 30;
  45.  
  46. // Power indicator increment values, maximum = 255. These values should not be zero unless the corresponding primary color maximum value (above) is also set to zero.
  47. // A larger number will cause that color to rise faster.
  48. int power_indicator_red_rise_value = 0;
  49. int power_indicator_green_rise_value = 10;
  50. int power_indicator_blue_rise_value = 15;
  51.  
  52. // Power indicator decay values are below. These can be values from 1 to 255, but should not be zero unless the maximum value and the rising value for that primary color are also zero.
  53. // A larger number will cause that color to decay faster.
  54. int power_indicator_red_decay_value = 10;
  55. int power_indicator_green_decay_value = 40;
  56. int power_indicator_blue_decay_value = 5;
  57.  
  58. // ###############################################################################################################################################
  59.  
  60. // Hard drive indicator maximum values, can range from 0 to 255. These values may be zero, but if all three are set to zero, then the LED of that primary color will not light up at all.
  61. int hardDrive_indicator_red_maximum_value = 60;
  62. int hardDrive_indicator_green_maximum_value = 0;
  63. int hardDrive_indicator_blue_maximum_value = 120;
  64.  
  65. // Hard drive indicator increment values, maximum = 255. These values should not be zero unless the corresponding primary color maximum value (above) is also set to zero.
  66. // A larger number will cause that color to rise faster.
  67. int hardDrive_indicator_red_rise_value = 30;
  68. int hardDrive_indicator_green_rise_value = 0;
  69. int hardDrive_indicator_blue_rise_value = 10;
  70.  
  71. // Hard drive indicator decay values are below. These can be values from 1 to 255, but should not be zero unless the maximum value and the rising value for that primary color are also zero.
  72. // A larger number will cause that color to decay faster.
  73. int hardDrive_indicator_red_decay_value = 25;
  74. int hardDrive_indicator_green_decay_value = 60;
  75. int hardDrive_indicator_blue_decay_value = 10;
  76.  
  77. // ###############################################################################################################################################
  78.  
  79. /*
  80. The section below is the variables to control the single-color case LEDs for the power and hard-drive-activity indicators.
  81. There is no color setting because these are single-color LEDs.
  82. There is only the maximum level setting, the rising rate and decay rate for each.
  83. */
  84.  
  85. int case_LED_power_indicator_maximum_brightness = 100;
  86. int case_LED_power_indicator_rising_rate = 50;
  87. int case_LED_power_indicator_decay_rate = 25;
  88.  
  89. int case_LED_hardDrive_indicator_maximum_brightness = 255;
  90. int case_LED_hardDrive_indicator_rising_rate = 100;
  91. int case_LED_hardDrive_indicator_decay_rate = 50;
  92.  
  93. // ###############################################################################################################################################
  94.  
  95. /*
  96. Input/Output Pins
  97. ------------------------------------------------
  98. Different boards use different I/O pin assignments.
  99. The set for the board you are using must be un-commented, and the other two should be commented out.
  100. */
  101.  
  102.  
  103. // Arduino Nano pins --------------------------------------------------------
  104. int power_indicator_pin_positive = A7; // this connects to the (+) power LED line of the PC
  105. int power_indicator_pin_negative = A6; // this connects to the (-) power LED line of the PC
  106. int hard_drive_indicator_pin_positive = A5; // this connects to the (+) hard drive LED line of the PC
  107. int hard_drive_indicator_pin_negative = A4; // this connects to the (-) hard drive LED line of the PC
  108. int LED_red_pin = 5; // This is pin D5: it connects to the red pin of the RGB LED
  109. int LED_green_pin = 6;// This is pin D6: it connects to the green pin of the RGB LED
  110. int LED_blue_pin = 9;// This is pin D9: it connects to the blue pin of the RGB LED
  111. int case_power_LED_pin = 10; // This is pin D10: this is where the case power indicator LED (+) wire is connected to.
  112. int case_hardDrive_LED_pin = 11;// This is pin D11: this is where the case hard-drive-activity LED (+) wire is connected to.
  113. // ---------------------------------------------------------------------------
  114.  
  115.  
  116. /*
  117. // Arduino Uno pins ----------------------------------------------------------
  118. int power_indicator_pin_positive = A1; // this connects to the (+) power LED line of the PC
  119. int power_indicator_pin_negative = A2; // this connects to the (-) power LED line of the PC
  120. int hard_drive_indicator_pin_positive = A3; // this connects to the (+) hard drive LED line of the PC
  121. int hard_drive_indicator_pin_negative = A4; // this connects to the (-) hard drive LED line of the PC
  122. int LED_red_pin = 11; // This is pin ~11: it connects to the red pin of the RGB LED
  123. int LED_green_pin = 10;// This is pin ~10: it connects to the green pin of the RGB LED
  124. int LED_blue_pin = 9;// This is pin ~9: it connects to the blue pin of the RGB LED
  125. int case_power_LED_pin = 5; // This is pin ~5: this is where the case power indicator LED (+) wire is connected to.
  126. int case_hardDrive_LED_pin = 6;// This is pin ~6: this is where the case hard-drive-activity LED (+) wire is connected to.
  127. // ---------------------------------------------------------------------------
  128. */
  129.  
  130. // ###############################################################################################################################################
  131.  
  132.  
  133. // PWM levels (don't change these values)
  134. int current_LED_red_PWM_level = 0;
  135. int current_LED_green_PWM_level = 0;
  136. int current_LED_blue_PWM_level = 0;
  137. int previous_LED_red_PWM_level = 0;
  138. int previous_LED_green_PWM_level = 0;
  139. int previous_LED_blue_PWM_level = 0;
  140.  
  141. int current_case_LED_power_level = 0;
  142. int previous_case_LED_power_level = 0;
  143.  
  144. int current_case_LED_hardDrive_level = 0;
  145. int previous_case_LED_hardDrive_level = 0;
  146.  
  147. int temp_difference = 0; // A temporary-use variable for setting the LED levels properly.
  148.  
  149. // power monitoring variables
  150. int array_powerChecks[20];
  151. /*
  152. The array above is 20 elements long, and the power check interval is set to 1/10th of a second (100 milliseconds).
  153. This is used to keep the hard-drive LED from triggering unless the power has been "on" for at least 2 seconds.
  154. Since we only have one display here and two inputs (power ahd hard drive activity) one of them must be given priotity.
  155. In this sketch, the hard-drive activity gets ignored while the computer is sleeping (while the power status input line is blinking on and off).
  156. */
  157. int power_check_total = 0;
  158. int power_pin_value = 0;
  159. //float power_pin_measured_volts = 0;
  160. float power_pin_positive_volts = 0;
  161. float power_pin_negative_volts = 0;
  162. float power_pin_minimum_volts = 1;
  163. int power_check_interval_milliseconds = 100; // This is the time interval that the power activity pin is checked. 100 milliseconds = ten times per second.
  164. unsigned long previous_power_check_time = 0;
  165. unsigned long current_power_check_time = 0;
  166.  
  167. // hard drive monitoring variables
  168. int hardDriveCheck_value = 0;
  169. int hardDrive_pin_value = 0;
  170. //float hardDrive_pin_measured_volts = 0;
  171. float hardDrive_pin_positive_volts = 0;
  172. float hardDrive_pin_negative_volts = 0;
  173. float hardDrive_pin_minimum_volts = 1;
  174. int hardDrive_check_interval_milliseconds = 20; // This is the time interval that the power activity pin is checked. 20 milliseconds = fifty times per second.
  175. int hardDrive_transition_interval_milliseconds = 100; // This is the time interval that the hard drive level stays "high" once it is found to be high.
  176. boolean hardDrive_delay_active = false;
  177. /*
  178. This program uses two different time intervals for the hard drive check, since it tends to blink on and off very rapidly.
  179. Normally the hard drive is checked at a rate of 50 times per second (20 milliseconds).
  180. If the hard drive activity signal is found to be high, then a second time delay of 100 milliseconds is performed (one tenth of a second).
  181. */
  182. unsigned long previous_hardDrive_check_time = 0;
  183. unsigned long current_hardDrive_check_time = 0;
  184.  
  185.  
  186. unsigned long LED_update_interval_previousTime = 0;
  187. unsigned long LED_update_interval_currentTime = 0;
  188. int LED_update_interval_milliseconds = 100;
  189.  
  190.  
  191.  
  192. // these are function declarations, for functions written further below
  193. void check_power_pin_state();
  194. void advance_power_array_values();
  195. void get_power_pin_input();
  196. void get_power_array_total();
  197.  
  198. void check_hardDrive_pin_state();
  199. void advance_hardDrive_array_values();
  200. void get_hardDrive_pin_input();
  201.  
  202. void set_LED_button_colors();
  203. void transition_LED_power_down();
  204. void transition_to_power_LED_colors();
  205. void transition_to_hardDrive_LED_colors();
  206.  
  207. void case_power_LED_turnOn();
  208. void case_power_LED_turnOff();
  209. void check_case_power_LED_value();
  210. void write_to_case_power_LED_pin();
  211.  
  212. void case_hardDrive_LED_turnOn();
  213. void case_hardDrive_LED_turnOff();
  214. void check_case_hardDrive_LED_value();
  215. void write_to_case_hardDrive_LED_pin();
  216.  
  217. void check_LED_values();
  218. void write_to_LED_pins();
  219.  
  220. void setup() {
  221.  
  222. Serial.begin(115200); // This is needed for serial debug messaging. Also you cannot use pins 30 or 31 (on the Nano) or else the serial debugging messages will not work.
  223.  
  224. /*
  225. Normally you would declare the input/output pins with their desired settings, but in this case we don't need to.
  226. This sketch uses analogread() for the two inputs, and the analogread() function doesn't require a delaration to use.
  227. This sketch also uses analogwrite() for the three PWM outputs to run the LED, and that doesn't need to be declared in advance either.
  228. */
  229. //pinMode(power_indicator_pin, INPUT);
  230. //pinMode(hard_drive_indicator_pin, INPUT);
  231. //pinMode(LED_red_pin, OUTPUT);
  232. //pinMode(LED_green_pin, OUTPUT);
  233. //pinMode(LED_blue_pin, OUTPUT);
  234.  
  235.  
  236.  
  237. // The lines below write the LED pins to zero value to start, else they may begin at random values.
  238. current_LED_red_PWM_level = 0;
  239. analogWrite(LED_red_pin, current_LED_red_PWM_level);
  240. current_LED_green_PWM_level = 0;
  241. analogWrite(LED_green_pin, current_LED_green_PWM_level);
  242. current_LED_blue_PWM_level = 0;
  243. analogWrite(LED_blue_pin, current_LED_blue_PWM_level);
  244.  
  245. // below initializes the power-check array to all-zero-values to begin.
  246. for (int i = 0; i <= 19; i++) {
  247. array_powerChecks[i] = 0;
  248. }
  249.  
  250. hardDriveCheck_value = 0;
  251.  
  252. } // --------- end of setup() function
  253.  
  254.  
  255.  
  256.  
  257. void loop() {
  258.  
  259. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  260. // This section checks the power pin state, at the specified time interval.
  261. current_power_check_time = millis();
  262. if (current_power_check_time > previous_power_check_time) {
  263. if (current_power_check_time >= (previous_power_check_time + power_check_interval_milliseconds)) {
  264. previous_power_check_time = millis();
  265. check_power_pin_state();
  266. }
  267. }
  268. else {
  269. previous_power_check_time = millis(); // rollover condition: probably not necessary but good practice
  270. }
  271.  
  272. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  273. // This section checks the hard drive pin state, at the specified time interval.
  274.  
  275. if (hardDrive_delay_active == false) {
  276. current_hardDrive_check_time = millis();
  277. if (current_hardDrive_check_time > previous_hardDrive_check_time) {
  278. if (current_hardDrive_check_time >= (previous_hardDrive_check_time + hardDrive_check_interval_milliseconds)) {
  279. hardDrive_delay_active = true;
  280. previous_hardDrive_check_time = millis();
  281. check_hardDrive_pin_state();
  282. }
  283. }
  284. else {
  285. previous_hardDrive_check_time = millis(); // rollover condition: probably not necessary but good practice
  286. }
  287. }
  288.  
  289.  
  290. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  291. // This sets the LED RGB values
  292.  
  293. //set_LED_button_colors();
  294.  
  295. LED_update_interval_currentTime = millis();
  296. if (LED_update_interval_currentTime > LED_update_interval_previousTime) {
  297. if (LED_update_interval_currentTime >= (LED_update_interval_previousTime + LED_update_interval_milliseconds)) {
  298. set_LED_button_colors();
  299. hardDrive_delay_active = false;
  300. LED_update_interval_previousTime = millis();
  301. }
  302. }
  303. else {
  304. LED_update_interval_previousTime = millis(); // rollover condition: probably not necessary but good practice
  305. }
  306.  
  307. } //----------- end of (main) loop
  308.  
  309.  
  310.  
  311.  
  312.  
  313. void check_power_pin_state() {
  314. advance_power_array_values();
  315. get_power_pin_input();
  316. }
  317.  
  318. void advance_power_array_values() {
  319. for (int i = 19; i >= 1; i--) {
  320. array_powerChecks[i] = array_powerChecks[i - 1];
  321. }
  322. }
  323.  
  324. void get_power_pin_input() {
  325. power_pin_value = analogRead(power_indicator_pin_positive);
  326. power_pin_positive_volts = (5.0 / 1024) * power_pin_value;
  327. power_pin_value = analogRead(power_indicator_pin_negative);
  328. power_pin_negative_volts = (5.0 / 1024) * power_pin_value;
  329. if ((power_pin_positive_volts - power_pin_negative_volts) >= power_pin_minimum_volts) {
  330. array_powerChecks[0] = 1;
  331. }
  332. else {
  333. array_powerChecks[0] = 0;
  334. }
  335. }
  336.  
  337. void get_power_array_total() {
  338. power_check_total = 0;
  339. for (int i = 0; i <= 19; i++) {
  340. power_check_total = power_check_total + array_powerChecks[i];
  341. }
  342. }
  343.  
  344.  
  345. void check_hardDrive_pin_state() {
  346. get_hardDrive_pin_input();
  347. }
  348.  
  349. void get_hardDrive_pin_input() {
  350. hardDrive_pin_value = analogRead(hard_drive_indicator_pin_positive);
  351. hardDrive_pin_positive_volts = (5.0 / 1024) * hardDrive_pin_value;
  352. hardDrive_pin_value = analogRead(hard_drive_indicator_pin_negative);
  353. hardDrive_pin_negative_volts = (5.0 / 1024) * hardDrive_pin_value;
  354. if ((hardDrive_pin_positive_volts - hardDrive_pin_negative_volts) >= hardDrive_pin_minimum_volts) {
  355. hardDriveCheck_value = 1;
  356. hardDrive_delay_active = true;
  357. }
  358. else {
  359. hardDriveCheck_value = 0;
  360. }
  361. }
  362.  
  363.  
  364.  
  365. void set_LED_button_colors() {
  366. get_power_array_total();
  367. if (hardDriveCheck_value == 1) { // This checks if the hard drive is active.
  368. if (power_check_total == 20) { // This checks if the power has been on constantly at least ~2 seconds.
  369. if (use_RGB_output == true) {
  370. transition_to_hardDrive_LED_colors();
  371. }
  372. if (use_case_hardDrive_LED == true) {
  373. case_hardDrive_LED_turnOn();
  374. }
  375. }
  376. }
  377. else {
  378. // If the first power check is not zero, but all 20 power checks are not 1, then call the power-color-transition function.
  379. if (array_powerChecks[0] == 0) {
  380. // If the first power check is zero, then the power-color_transition function is called, because the computer is sleeping and the LED signal is 'off'.
  381. if (use_RGB_output == true) {
  382. transition_LED_power_down();
  383. }
  384. if (use_case_power_LED == true) {
  385. case_power_LED_turnOff();
  386. }
  387. }
  388. else {
  389. // If (array_powerChecks[0] = 1 then turn the LED power colors on.
  390. if (use_RGB_output == true) {
  391. transition_to_power_LED_colors();
  392. }
  393. if (use_case_power_LED == true) {
  394. case_power_LED_turnOn();
  395. }
  396. }
  397. if (use_case_hardDrive_LED == true) {
  398. case_hardDrive_LED_turnOff();
  399. }
  400. }
  401. }
  402.  
  403.  
  404. void transition_LED_power_down() {
  405. current_LED_red_PWM_level = current_LED_red_PWM_level - power_indicator_red_decay_value;
  406. current_LED_green_PWM_level = current_LED_green_PWM_level - power_indicator_green_decay_value;
  407. current_LED_blue_PWM_level = current_LED_blue_PWM_level - power_indicator_blue_decay_value;
  408. // The LED's PWM values are checked to make sure they are valid (must be from zero to 255).
  409. check_LED_values();
  410. // The PWM values are written to the three pins controlling the three primary-color LEDs.
  411. write_to_LED_pins();
  412. }
  413.  
  414.  
  415. void transition_to_power_LED_colors() {
  416. // The LED's PWM values are incremented with the power-color-rise values.
  417. // In this function, it doesn't matter if each current color value is above or below the power_indicator_maximum values; it is moved towards that value either way by the rising value or the decay value.
  418. // The variable temp_difference is used to make certain that the incremental value does not overshoot the desired maximum value.
  419. if (current_LED_red_PWM_level < power_indicator_red_maximum_value) {
  420. temp_difference = power_indicator_red_maximum_value - current_LED_red_PWM_level;
  421. if (temp_difference > power_indicator_red_rise_value) {
  422. current_LED_red_PWM_level = current_LED_red_PWM_level + power_indicator_red_rise_value;
  423. }
  424. else {
  425. current_LED_red_PWM_level = power_indicator_red_maximum_value;
  426. }
  427. }
  428. else if (current_LED_red_PWM_level > power_indicator_red_maximum_value) {
  429. temp_difference = current_LED_red_PWM_level - power_indicator_red_maximum_value;
  430. if (temp_difference > power_indicator_red_decay_value) {
  431. current_LED_red_PWM_level = current_LED_red_PWM_level - power_indicator_red_decay_value;
  432. }
  433. else {
  434. current_LED_red_PWM_level = power_indicator_red_maximum_value;
  435. }
  436. }
  437.  
  438. if (current_LED_green_PWM_level < power_indicator_green_maximum_value) {
  439. temp_difference = power_indicator_green_maximum_value - current_LED_green_PWM_level;
  440. if (temp_difference > power_indicator_green_rise_value) {
  441. current_LED_green_PWM_level = current_LED_green_PWM_level + power_indicator_green_rise_value;
  442. }
  443. else {
  444. current_LED_green_PWM_level = power_indicator_green_maximum_value;
  445. }
  446. }
  447. else if (current_LED_green_PWM_level > power_indicator_green_maximum_value) {
  448. temp_difference = current_LED_green_PWM_level - power_indicator_green_maximum_value;
  449. if (temp_difference > power_indicator_green_decay_value) {
  450. current_LED_green_PWM_level = current_LED_green_PWM_level - power_indicator_green_decay_value;
  451. }
  452. else {
  453. current_LED_green_PWM_level = power_indicator_green_maximum_value;
  454. }
  455. }
  456.  
  457. if (current_LED_blue_PWM_level < power_indicator_blue_maximum_value) {
  458. temp_difference = power_indicator_blue_maximum_value - current_LED_blue_PWM_level;
  459. if (temp_difference > power_indicator_blue_rise_value) {
  460. current_LED_blue_PWM_level = current_LED_blue_PWM_level + power_indicator_blue_rise_value;
  461. }
  462. else {
  463. current_LED_blue_PWM_level = power_indicator_blue_maximum_value;
  464. }
  465. }
  466. else if (current_LED_blue_PWM_level > power_indicator_blue_maximum_value) {
  467. temp_difference = current_LED_blue_PWM_level - power_indicator_blue_maximum_value;
  468. if (temp_difference > power_indicator_blue_decay_value) {
  469. current_LED_blue_PWM_level = current_LED_blue_PWM_level - power_indicator_blue_decay_value;
  470. }
  471. else {
  472. current_LED_blue_PWM_level = power_indicator_blue_maximum_value;
  473. }
  474. }
  475.  
  476. // The LED's PWM values are checked to make sure they are valid (must be from zero to 255).
  477. check_LED_values();
  478. // The PWM values are written to the three pins controlling the three primary-color LEDs.
  479. write_to_LED_pins();
  480. }
  481.  
  482.  
  483. void transition_to_hardDrive_LED_colors() {
  484. // This function does the same thing as transition_to_power_LED_colors(), but for the hardDrive indicator values.
  485. if (current_LED_red_PWM_level < hardDrive_indicator_red_maximum_value) {
  486. temp_difference = hardDrive_indicator_red_maximum_value - current_LED_red_PWM_level;
  487. if (temp_difference > hardDrive_indicator_red_rise_value) {
  488. current_LED_red_PWM_level = current_LED_red_PWM_level + hardDrive_indicator_red_rise_value;
  489. }
  490. else {
  491. current_LED_red_PWM_level = hardDrive_indicator_red_maximum_value;
  492. }
  493. }
  494. else if (current_LED_red_PWM_level > hardDrive_indicator_red_maximum_value) {
  495. temp_difference = current_LED_red_PWM_level - hardDrive_indicator_red_maximum_value;
  496. if (temp_difference > hardDrive_indicator_red_decay_value) {
  497. current_LED_red_PWM_level = current_LED_red_PWM_level - hardDrive_indicator_red_decay_value;
  498. }
  499. else {
  500. current_LED_red_PWM_level = hardDrive_indicator_red_maximum_value;
  501. }
  502. }
  503.  
  504. if (current_LED_green_PWM_level < hardDrive_indicator_green_maximum_value) {
  505. temp_difference = hardDrive_indicator_green_maximum_value - current_LED_green_PWM_level;
  506. if (temp_difference > hardDrive_indicator_green_rise_value) {
  507. current_LED_green_PWM_level = current_LED_green_PWM_level + hardDrive_indicator_green_rise_value;
  508. }
  509. else {
  510. current_LED_green_PWM_level = hardDrive_indicator_green_maximum_value;
  511. }
  512. }
  513. else if (current_LED_green_PWM_level > hardDrive_indicator_green_maximum_value) {
  514. temp_difference = current_LED_green_PWM_level - hardDrive_indicator_green_maximum_value;
  515. if (temp_difference > hardDrive_indicator_green_decay_value) {
  516. current_LED_green_PWM_level = current_LED_green_PWM_level - hardDrive_indicator_green_decay_value;
  517. }
  518. else {
  519. current_LED_green_PWM_level = hardDrive_indicator_green_maximum_value;
  520. }
  521. }
  522.  
  523. if (current_LED_blue_PWM_level < hardDrive_indicator_blue_maximum_value) {
  524. temp_difference = hardDrive_indicator_blue_maximum_value - current_LED_blue_PWM_level;
  525. if (temp_difference > hardDrive_indicator_blue_rise_value) {
  526. current_LED_blue_PWM_level = current_LED_blue_PWM_level + hardDrive_indicator_blue_rise_value;
  527. }
  528. else {
  529. current_LED_blue_PWM_level = hardDrive_indicator_blue_maximum_value;
  530. }
  531. }
  532. else if (current_LED_blue_PWM_level > hardDrive_indicator_blue_maximum_value) {
  533. temp_difference = current_LED_blue_PWM_level - hardDrive_indicator_blue_maximum_value;
  534. if (temp_difference > hardDrive_indicator_blue_decay_value) {
  535. current_LED_blue_PWM_level = current_LED_blue_PWM_level - hardDrive_indicator_blue_decay_value;
  536. }
  537. else {
  538. current_LED_blue_PWM_level = hardDrive_indicator_blue_maximum_value;
  539. }
  540. }
  541.  
  542. //hardDrive_delay_active = false;
  543.  
  544. // The LED's PWM values are checked to make sure they are valid (must be from zero to 255).
  545. check_LED_values();
  546. // The PWM values are written to the three pins controlling the three primary-color LEDs.
  547. write_to_LED_pins();
  548. }
  549.  
  550. void case_power_LED_turnOn() {
  551. /*
  552. int current_case_LED_power_level = 0;
  553. int previous_case_LED_power_level = 0;
  554. int case_LED_power_indicator_rising_rate = 20;
  555. int case_LED_power_indicator_decay_rate = 5;
  556. */
  557. current_case_LED_power_level = current_case_LED_power_level + case_LED_power_indicator_rising_rate;
  558. check_case_power_LED_value();
  559. write_to_case_power_LED_pin();
  560. }
  561.  
  562. void case_power_LED_turnOff() {
  563. current_case_LED_power_level = current_case_LED_power_level - case_LED_power_indicator_decay_rate;
  564. check_case_power_LED_value();
  565. write_to_case_power_LED_pin();
  566. }
  567.  
  568. void check_case_power_LED_value() {
  569. if (current_case_LED_power_level > 255) {
  570. current_case_LED_power_level = 255;
  571. }
  572. if (current_case_LED_power_level < 0) {
  573. current_case_LED_power_level = 0;
  574. }
  575. }
  576.  
  577. void write_to_case_power_LED_pin() {
  578. if (current_case_LED_power_level != previous_case_LED_power_level) {
  579. previous_case_LED_power_level = current_case_LED_power_level;
  580. analogWrite(case_power_LED_pin, current_case_LED_power_level);
  581. }
  582. }
  583.  
  584.  
  585. void case_hardDrive_LED_turnOn() {
  586. /*
  587. int current_case_LED_hardDrive_level = 0;
  588. int previous_case_LED_hardDrive_level = 0;
  589. int case_LED_hardDrive_indicator_maximum_brightness = 100;
  590. int case_LED_hardDrive_indicator_rising_rate = 100;
  591. int case_LED_hardDrive_indicator_decay_rate = 10;
  592. */
  593. current_case_LED_hardDrive_level = current_case_LED_hardDrive_level + case_LED_hardDrive_indicator_rising_rate;
  594. check_case_hardDrive_LED_value();
  595. write_to_case_hardDrive_LED_pin();
  596. }
  597.  
  598. void case_hardDrive_LED_turnOff() {
  599. current_case_LED_hardDrive_level = current_case_LED_hardDrive_level - case_LED_hardDrive_indicator_decay_rate;
  600. check_case_hardDrive_LED_value();
  601. write_to_case_hardDrive_LED_pin();
  602. }
  603.  
  604. void check_case_hardDrive_LED_value() {
  605. if (current_case_LED_hardDrive_level > 255) {
  606. current_case_LED_hardDrive_level = 255;
  607. }
  608. if (current_case_LED_hardDrive_level < 0) {
  609. current_case_LED_hardDrive_level = 0;
  610. }
  611. }
  612.  
  613. void write_to_case_hardDrive_LED_pin() {
  614. if (current_case_LED_hardDrive_level != previous_case_LED_hardDrive_level) {
  615. previous_case_LED_hardDrive_level = current_case_LED_hardDrive_level;
  616. analogWrite(case_hardDrive_LED_pin, current_case_LED_hardDrive_level);
  617. }
  618. }
  619.  
  620.  
  621. void check_LED_values() {
  622. /*
  623. The LED PWM primary color values must be a number from zero to 255.
  624. If they fall outside of that range, they must be corrected first, before calling the PWM command.
  625. This is done for all three primary color values.
  626. */
  627. if (current_LED_red_PWM_level > 255) {
  628. current_LED_red_PWM_level = 255;
  629. }
  630. if (current_LED_red_PWM_level < 0) {
  631. current_LED_red_PWM_level = 0;
  632. }
  633. if (current_LED_green_PWM_level > 255) {
  634. current_LED_green_PWM_level = 255;
  635. }
  636. if (current_LED_green_PWM_level < 0) {
  637. current_LED_green_PWM_level = 0;
  638. }
  639. if (current_LED_blue_PWM_level > 255) {
  640. current_LED_blue_PWM_level = 255;
  641. }
  642. if (current_LED_blue_PWM_level < 0) {
  643. current_LED_blue_PWM_level = 0;
  644. }
  645. }
  646.  
  647. void write_to_LED_pins() {
  648. /*
  649. This function contains some code so that the PWM values are only set if they are different than the previous values.
  650. Calling them every time might cause the LED to blink undesireably, so this only calls the change when it is necessary for each primary color.
  651. */
  652. if (current_LED_red_PWM_level != previous_LED_red_PWM_level) {
  653. previous_LED_red_PWM_level = current_LED_red_PWM_level;
  654. analogWrite(LED_red_pin, current_LED_red_PWM_level);
  655. }
  656. if (current_LED_green_PWM_level != previous_LED_green_PWM_level) {
  657. previous_LED_green_PWM_level = current_LED_green_PWM_level;
  658. analogWrite(LED_green_pin, current_LED_green_PWM_level);
  659. }
  660. if (current_LED_blue_PWM_level != previous_LED_blue_PWM_level) {
  661. previous_LED_blue_PWM_level = current_LED_blue_PWM_level;
  662. analogWrite(LED_blue_pin, current_LED_blue_PWM_level);
  663. }
  664. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement