daily pastebin goal
51%
SHARE
TWEET

pretty_vandal_RGB

skizziks_53 May 25th, 2017 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.    Pretty Vandal RGB
  3.    Notes:
  4.  
  5.    1. This code is intended for a 5-volt Arduino Nano or Nano 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 from three of the Nano output pins by PWM. You also still need current-limiting resistors for each primary-color LED.
  7.    3. At the minimums--the red LED should have a 180 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. The two input wires have pulldown resistors of 10K ohms each.
  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.  
  15.  
  16.  
  17.  
  18. /*
  19.   LED color levels are set here, in the first 18 variables stated.
  20.   The first nine are for the power indicator, and the second nine are for the hard-drive activity indicator.
  21.   Each RGB LED has a value of 0 to 255 for each of the primary LED colors.
  22.   The first three variables below state the maximum value for that primary color that will be used.
  23.   The second set controls how fast that each of the three primary colors will rise, if the indicator state is positive.
  24.   The third set is the amount that each of the three primary colors decrease, if the indicator state is negative.
  25.  
  26.   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.
  27. */
  28.  
  29. // ###############################################################################################################################################
  30.  
  31. // 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.
  32. int power_indicator_red_maximum_value = 0;
  33. int power_indicator_green_maximum_value = 100;
  34. int power_indicator_blue_maximum_value = 30;
  35.  
  36. // 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.
  37. // A larger number will cause that color to rise faster.
  38. int power_indicator_red_rise_value = 0;
  39. int power_indicator_green_rise_value = 10;
  40. int power_indicator_blue_rise_value = 15;
  41.  
  42. // 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.
  43. // A larger number will cause that color to decay faster.
  44. int power_indicator_red_decay_value = 10;
  45. int power_indicator_green_decay_value = 40;
  46. int power_indicator_blue_decay_value = 5;
  47.  
  48. // ###############################################################################################################################################
  49.  
  50. // 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.
  51. int hardDrive_indicator_red_maximum_value = 60;
  52. int hardDrive_indicator_green_maximum_value = 0;
  53. int hardDrive_indicator_blue_maximum_value = 120;
  54.  
  55. // 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.
  56. // A larger number will cause that color to rise faster.
  57. int hardDrive_indicator_red_rise_value = 30;
  58. int hardDrive_indicator_green_rise_value = 0;
  59. int hardDrive_indicator_blue_rise_value = 10;
  60.  
  61. // 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.
  62. // A larger number will cause that color to decay faster.
  63. int hardDrive_indicator_red_decay_value = 25;
  64. int hardDrive_indicator_green_decay_value = 60;
  65. int hardDrive_indicator_blue_decay_value = 10;
  66.  
  67. // ###############################################################################################################################################
  68.  
  69. /*
  70.     Input/Output Pins
  71.     ------------------------------------------------
  72.     Different boards use different I/O pin assignments.
  73.     The set for the board you are using must be un-commented, and the other two should be commented out.
  74. */
  75.  
  76.  
  77. // Arduino Nano pins --------------------------------------------------------
  78. int power_indicator_pin_positive = A7; // this connects to the (+) power LED line of the PC
  79. int power_indicator_pin_negative = A6; // this connects to the (-) power LED line of the PC
  80. int hard_drive_indicator_pin_positive = A5; // this connects to the (+) hard drive LED line of the PC
  81. int hard_drive_indicator_pin_negative = A4; // this connects to the (-) hard drive LED line of the PC
  82. int LED_red_pin = 5; // This is pin D5: it connects to the red pin of the RGB LED
  83. int LED_green_pin = 6;// This is pin D6: connects to the green pin of the RGB LED
  84. int LED_blue_pin = 9;// This is pin D9: it connects to the blue pin of the RGB LED
  85. // ---------------------------------------------------------------------------
  86.  
  87.  
  88. /*
  89.   // Arduino Uno pins ----------------------------------------------------------
  90.   int power_indicator_pin_positive = A1; // this connects to the (+) power LED line of the PC
  91.   int power_indicator_pin_negative = A2; // this connects to the (-) power LED line of the PC
  92.   int hard_drive_indicator_pin_positive = A3; // this connects to the (+) hard drive LED line of the PC
  93.   int hard_drive_indicator_pin_negative = A4; // this connects to the (-) hard drive LED line of the PC
  94.   int LED_red_pin = 11; // This is pin ~11: it connects to the red pin of the RGB LED
  95.   int LED_green_pin = 10;// This is pin ~10: it connects to the green pin of the RGB LED
  96.   int LED_blue_pin = 9;// This is pin ~9: it connects to the blue pin of the RGB LED
  97.   // ---------------------------------------------------------------------------
  98. */
  99.  
  100. // ###############################################################################################################################################
  101.  
  102.  
  103. // PWM levels (don't change these values)
  104. int current_LED_red_PWM_level = 0;
  105. int current_LED_green_PWM_level = 0;
  106. int current_LED_blue_PWM_level = 0;
  107. int previous_LED_red_PWM_level = 0;
  108. int previous_LED_green_PWM_level = 0;
  109. int previous_LED_blue_PWM_level = 0;
  110. int temp_difference = 0; // A temporary-use variable for setting the LED levels properly.
  111.  
  112. // power monitoring variables
  113. int array_powerChecks[20];
  114. /*
  115.    The array above is 20 elements long, and the power check interval is set to 1/10th of a second (100 milliseconds).
  116.    This is used to keep the hard-drive LED from triggering unless the power has been "on" for at least 2 seconds.
  117.    Since we only have one display here and two inputs (power ahd hard drive activity) one of them must be given priotity.
  118.    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).
  119. */
  120. int power_check_total = 0;
  121. int power_pin_value = 0;
  122. //float power_pin_measured_volts = 0;
  123. float power_pin_positive_volts = 0;
  124. float power_pin_negative_volts = 0;
  125. float power_pin_minimum_volts = 1;
  126. int power_check_interval_milliseconds = 100; // This is the time interval that the power activity pin is checked. 100 milliseconds = ten times per second.
  127. unsigned long previous_power_check_time = 0;
  128. unsigned long current_power_check_time = 0;
  129.  
  130. // hard drive monitoring variables
  131. int hardDriveCheck_value = 0;
  132. int hardDrive_pin_value = 0;
  133. //float hardDrive_pin_measured_volts = 0;
  134. float hardDrive_pin_positive_volts = 0;
  135. float hardDrive_pin_negative_volts = 0;
  136. float hardDrive_pin_minimum_volts = 1;
  137. int hardDrive_check_interval_milliseconds = 20; // This is the time interval that the power activity pin is checked. 20 milliseconds = fifty times per second.
  138. 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.
  139. boolean hardDrive_delay_active = false;
  140. /*
  141.    This program uses two different time intervals for the hard drive check, since it tends to blink on and off very rapidly.
  142.    Normally the hard drive is checked at a rate of 50 times per second (20 milliseconds).
  143.    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).
  144. */
  145. unsigned long previous_hardDrive_check_time = 0;
  146. unsigned long current_hardDrive_check_time = 0;
  147.  
  148.  
  149. unsigned long LED_update_interval_previousTime = 0;
  150. unsigned long LED_update_interval_currentTime = 0;
  151. int LED_update_interval_milliseconds = 100;
  152.  
  153.  
  154.  
  155. // these are function declarations, for functions written further below
  156. void check_power_pin_state();
  157. void advance_power_array_values();
  158. void get_power_pin_input();
  159. void get_power_array_total();
  160.  
  161. void check_hardDrive_pin_state();
  162. void advance_hardDrive_array_values();
  163. void get_hardDrive_pin_input();
  164.  
  165. void set_LED_button_colors();
  166. void transition_LED_power_down();
  167. void transition_to_power_LED_colors();
  168. void transition_to_hardDrive_LED_colors();
  169. void check_LED_values();
  170. void write_to_LED_pins();
  171.  
  172. void setup() {
  173.  
  174.   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.
  175.  
  176.   /*
  177.      Normally you would declare the input/output pins with their desired settings, but in this case we don't need to.
  178.      This sketch uses analogread() for the two inputs, and the analogread() function doesn't require a delaration to use.
  179.      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.
  180.   */
  181.   //pinMode(power_indicator_pin, INPUT);
  182.   //pinMode(hard_drive_indicator_pin, INPUT);
  183.   //pinMode(LED_red_pin, OUTPUT);
  184.   //pinMode(LED_green_pin, OUTPUT);
  185.   //pinMode(LED_blue_pin, OUTPUT);
  186.  
  187.  
  188.  
  189.   // The lines below write the LED pins to zero value to start, else they may begin at random values.
  190.   current_LED_red_PWM_level = 0;
  191.   analogWrite(LED_red_pin, current_LED_red_PWM_level);
  192.   current_LED_green_PWM_level = 0;
  193.   analogWrite(LED_green_pin, current_LED_green_PWM_level);
  194.   current_LED_blue_PWM_level = 0;
  195.   analogWrite(LED_blue_pin, current_LED_blue_PWM_level);
  196.  
  197.   // below initializes the power-check array to all-zero-values to begin.
  198.   for (int i = 0; i <= 19; i++) {
  199.     array_powerChecks[i] = 0;
  200.   }
  201.  
  202.   hardDriveCheck_value = 0;
  203.  
  204. } // --------- end of setup() function
  205.  
  206.  
  207.  
  208.  
  209. void loop() {
  210.  
  211.   // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  212.   // This section checks the power pin state, at the specified time interval.
  213.   current_power_check_time = millis();
  214.   if (current_power_check_time > previous_power_check_time) {
  215.     if (current_power_check_time >= (previous_power_check_time + power_check_interval_milliseconds)) {
  216.       previous_power_check_time = millis();
  217.       check_power_pin_state();
  218.     }
  219.   }
  220.   else {
  221.     previous_power_check_time = millis(); // rollover condition: probably not necessary but good practice
  222.   }
  223.  
  224.   // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  225.   // This section checks the hard drive pin state, at the specified time interval.
  226.  
  227.   if (hardDrive_delay_active == false) {
  228.     current_hardDrive_check_time = millis();
  229.     if (current_hardDrive_check_time > previous_hardDrive_check_time) {
  230.       if (current_hardDrive_check_time >= (previous_hardDrive_check_time + hardDrive_check_interval_milliseconds)) {
  231.         hardDrive_delay_active = true;
  232.         previous_hardDrive_check_time = millis();
  233.         check_hardDrive_pin_state();
  234.       }
  235.     }
  236.     else {
  237.       previous_hardDrive_check_time = millis(); // rollover condition: probably not necessary but good practice
  238.     }
  239.   }
  240.  
  241.  
  242.   // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  243.   // This sets the LED RGB values
  244.  
  245.   //set_LED_button_colors();
  246.  
  247.   LED_update_interval_currentTime = millis();
  248.   if (LED_update_interval_currentTime > LED_update_interval_previousTime) {
  249.     if (LED_update_interval_currentTime >= (LED_update_interval_previousTime + LED_update_interval_milliseconds)) {
  250.       set_LED_button_colors();
  251.       hardDrive_delay_active = false;
  252.       LED_update_interval_previousTime = millis();
  253.     }
  254.   }
  255.   else {
  256.     LED_update_interval_previousTime = millis(); // rollover condition: probably not necessary but good practice
  257.   }
  258.  
  259. } //----------- end of (main) loop
  260.  
  261.  
  262.  
  263.  
  264.  
  265. void check_power_pin_state() {
  266.   advance_power_array_values();
  267.   get_power_pin_input();
  268. }
  269.  
  270. void advance_power_array_values() {
  271.   for (int i = 19; i >= 1; i--) {
  272.     array_powerChecks[i] = array_powerChecks[i - 1];
  273.   }
  274. }
  275.  
  276. void get_power_pin_input() {
  277.   power_pin_value = analogRead(power_indicator_pin_positive);
  278.   power_pin_positive_volts = (5.0 / 1024) * power_pin_value;
  279.   power_pin_value = analogRead(power_indicator_pin_negative);
  280.   power_pin_negative_volts = (5.0 / 1024) * power_pin_value;
  281.   if ((power_pin_positive_volts - power_pin_negative_volts) >= power_pin_minimum_volts) {
  282.     array_powerChecks[0] = 1;
  283.   }
  284.   else {
  285.     array_powerChecks[0] = 0;
  286.   }
  287. }
  288.  
  289. void get_power_array_total() {
  290.   power_check_total = 0;
  291.   for (int i = 0; i <= 19; i++) {
  292.     power_check_total = power_check_total + array_powerChecks[i];
  293.   }
  294. }
  295.  
  296.  
  297. void check_hardDrive_pin_state() {
  298.   get_hardDrive_pin_input();
  299. }
  300.  
  301. void get_hardDrive_pin_input() {
  302.   hardDrive_pin_value = analogRead(hard_drive_indicator_pin_positive);
  303.   hardDrive_pin_positive_volts = (5.0 / 1024) * hardDrive_pin_value;
  304.   hardDrive_pin_value = analogRead(hard_drive_indicator_pin_negative);
  305.   hardDrive_pin_negative_volts = (5.0 / 1024) * hardDrive_pin_value;
  306.   if ((hardDrive_pin_positive_volts - hardDrive_pin_negative_volts) >= hardDrive_pin_minimum_volts) {
  307.     hardDriveCheck_value = 1;
  308.     hardDrive_delay_active = true;
  309.     //set_LED_button_colors();
  310.   }
  311.   else {
  312.     hardDriveCheck_value = 0;
  313.   }
  314. }
  315.  
  316.  
  317.  
  318. void set_LED_button_colors() {
  319.   get_power_array_total();
  320.   if (hardDriveCheck_value == 1) { // This checks if the hard drive is active.
  321.     if (power_check_total == 20) { // This checks if the power has been on constantly at least ~2 seconds.
  322.       transition_to_hardDrive_LED_colors();
  323.     }
  324.   }
  325.   else {
  326.     // If the first power check is not zero, but all 20 power checks are not 1, then call the power-color-transition function.
  327.     if (array_powerChecks[0] == 0) {
  328.       // 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'.
  329.       transition_LED_power_down();
  330.     }
  331.     else {
  332.       // If (array_powerChecks[0] = 1 then turn the LED power colors on.
  333.       transition_to_power_LED_colors();
  334.     }
  335.   }
  336. }
  337.  
  338.  
  339. void transition_LED_power_down() {
  340.   current_LED_red_PWM_level = current_LED_red_PWM_level - power_indicator_red_decay_value;
  341.   current_LED_green_PWM_level = current_LED_green_PWM_level - power_indicator_green_decay_value;
  342.   current_LED_blue_PWM_level = current_LED_blue_PWM_level - power_indicator_blue_decay_value;
  343.   // The LED's PWM values are checked to make sure they are valid (must be from zero to 255).
  344.   check_LED_values();
  345.   // The PWM values are written to the three pins controlling the three primary-color LEDs.
  346.   write_to_LED_pins();
  347. }
  348.  
  349.  
  350. void transition_to_power_LED_colors() {
  351.   // The LED's PWM values are incremented with the power-color-rise values.
  352.   // 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.
  353.   // The variable temp_difference is used to make certain that the incremental value does not overshoot the desired maximum value.
  354.   if (current_LED_red_PWM_level < power_indicator_red_maximum_value) {
  355.     temp_difference = power_indicator_red_maximum_value - current_LED_red_PWM_level;
  356.     if (temp_difference > power_indicator_red_rise_value) {
  357.       current_LED_red_PWM_level = current_LED_red_PWM_level + power_indicator_red_rise_value;
  358.     }
  359.     else {
  360.       current_LED_red_PWM_level = power_indicator_red_maximum_value;
  361.     }
  362.   }
  363.   else if (current_LED_red_PWM_level > power_indicator_red_maximum_value) {
  364.     temp_difference = current_LED_red_PWM_level - power_indicator_red_maximum_value;
  365.     if (temp_difference > power_indicator_red_decay_value) {
  366.       current_LED_red_PWM_level = current_LED_red_PWM_level - power_indicator_red_decay_value;
  367.     }
  368.     else {
  369.       current_LED_red_PWM_level = power_indicator_red_maximum_value;
  370.     }
  371.   }
  372.  
  373.   if (current_LED_green_PWM_level < power_indicator_green_maximum_value) {
  374.     temp_difference = power_indicator_green_maximum_value - current_LED_green_PWM_level;
  375.     if (temp_difference > power_indicator_green_rise_value) {
  376.       current_LED_green_PWM_level = current_LED_green_PWM_level + power_indicator_green_rise_value;
  377.     }
  378.     else {
  379.       current_LED_green_PWM_level = power_indicator_green_maximum_value;
  380.     }
  381.   }
  382.   else if (current_LED_green_PWM_level > power_indicator_green_maximum_value) {
  383.     temp_difference = current_LED_green_PWM_level - power_indicator_green_maximum_value;
  384.     if (temp_difference > power_indicator_green_decay_value) {
  385.       current_LED_green_PWM_level = current_LED_green_PWM_level - power_indicator_green_decay_value;
  386.     }
  387.     else {
  388.       current_LED_green_PWM_level = power_indicator_green_maximum_value;
  389.     }
  390.   }
  391.  
  392.   if (current_LED_blue_PWM_level < power_indicator_blue_maximum_value) {
  393.     temp_difference = power_indicator_blue_maximum_value - current_LED_blue_PWM_level;
  394.     if (temp_difference > power_indicator_blue_rise_value) {
  395.       current_LED_blue_PWM_level = current_LED_blue_PWM_level + power_indicator_blue_rise_value;
  396.     }
  397.     else {
  398.       current_LED_blue_PWM_level = power_indicator_blue_maximum_value;
  399.     }
  400.   }
  401.   else if (current_LED_blue_PWM_level > power_indicator_blue_maximum_value) {
  402.     temp_difference = current_LED_blue_PWM_level - power_indicator_blue_maximum_value;
  403.     if (temp_difference > power_indicator_blue_decay_value) {
  404.       current_LED_blue_PWM_level = current_LED_blue_PWM_level - power_indicator_blue_decay_value;
  405.     }
  406.     else {
  407.       current_LED_blue_PWM_level = power_indicator_blue_maximum_value;
  408.     }
  409.   }
  410.  
  411.   // The LED's PWM values are checked to make sure they are valid (must be from zero to 255).
  412.   check_LED_values();
  413.   // The PWM values are written to the three pins controlling the three primary-color LEDs.
  414.   write_to_LED_pins();
  415. }
  416.  
  417.  
  418. void transition_to_hardDrive_LED_colors() {
  419.   // This function does the same thing as transition_to_power_LED_colors(), but for the hardDrive indicator values.
  420.   if (current_LED_red_PWM_level < hardDrive_indicator_red_maximum_value) {
  421.     temp_difference = hardDrive_indicator_red_maximum_value - current_LED_red_PWM_level;
  422.     if (temp_difference > hardDrive_indicator_red_rise_value) {
  423.       current_LED_red_PWM_level = current_LED_red_PWM_level + hardDrive_indicator_red_rise_value;
  424.     }
  425.     else {
  426.       current_LED_red_PWM_level = hardDrive_indicator_red_maximum_value;
  427.     }
  428.   }
  429.   else if (current_LED_red_PWM_level > hardDrive_indicator_red_maximum_value) {
  430.     temp_difference = current_LED_red_PWM_level - hardDrive_indicator_red_maximum_value;
  431.     if (temp_difference > hardDrive_indicator_red_decay_value) {
  432.       current_LED_red_PWM_level = current_LED_red_PWM_level - hardDrive_indicator_red_decay_value;
  433.     }
  434.     else {
  435.       current_LED_red_PWM_level = hardDrive_indicator_red_maximum_value;
  436.     }
  437.   }
  438.  
  439.   if (current_LED_green_PWM_level < hardDrive_indicator_green_maximum_value) {
  440.     temp_difference = hardDrive_indicator_green_maximum_value - current_LED_green_PWM_level;
  441.     if (temp_difference > hardDrive_indicator_green_rise_value) {
  442.       current_LED_green_PWM_level = current_LED_green_PWM_level + hardDrive_indicator_green_rise_value;
  443.     }
  444.     else {
  445.       current_LED_green_PWM_level = hardDrive_indicator_green_maximum_value;
  446.     }
  447.   }
  448.   else if (current_LED_green_PWM_level > hardDrive_indicator_green_maximum_value) {
  449.     temp_difference = current_LED_green_PWM_level - hardDrive_indicator_green_maximum_value;
  450.     if (temp_difference > hardDrive_indicator_green_decay_value) {
  451.       current_LED_green_PWM_level = current_LED_green_PWM_level - hardDrive_indicator_green_decay_value;
  452.     }
  453.     else {
  454.       current_LED_green_PWM_level = hardDrive_indicator_green_maximum_value;
  455.     }
  456.   }
  457.  
  458.   if (current_LED_blue_PWM_level < hardDrive_indicator_blue_maximum_value) {
  459.     temp_difference = hardDrive_indicator_blue_maximum_value - current_LED_blue_PWM_level;
  460.     if (temp_difference > hardDrive_indicator_blue_rise_value) {
  461.       current_LED_blue_PWM_level = current_LED_blue_PWM_level + hardDrive_indicator_blue_rise_value;
  462.     }
  463.     else {
  464.       current_LED_blue_PWM_level = hardDrive_indicator_blue_maximum_value;
  465.     }
  466.   }
  467.   else if (current_LED_blue_PWM_level > hardDrive_indicator_blue_maximum_value) {
  468.     temp_difference = current_LED_blue_PWM_level - hardDrive_indicator_blue_maximum_value;
  469.     if (temp_difference > hardDrive_indicator_blue_decay_value) {
  470.       current_LED_blue_PWM_level = current_LED_blue_PWM_level - hardDrive_indicator_blue_decay_value;
  471.     }
  472.     else {
  473.       current_LED_blue_PWM_level = hardDrive_indicator_blue_maximum_value;
  474.     }
  475.   }
  476.  
  477.   //hardDrive_delay_active = false;
  478.  
  479.   // The LED's PWM values are checked to make sure they are valid (must be from zero to 255).
  480.   check_LED_values();
  481.   // The PWM values are written to the three pins controlling the three primary-color LEDs.
  482.   write_to_LED_pins();
  483. }
  484.  
  485.  
  486.  
  487. void check_LED_values() {
  488.   /*
  489.      The LED PWM primary color values must be a number from zero to 255.
  490.      If they fall outside of that range, they must be corrected first, before calling the PWM command.
  491.      This is done for all three primary color values.
  492.   */
  493.   if (current_LED_red_PWM_level > 255) {
  494.     current_LED_red_PWM_level = 255;
  495.   }
  496.   if (current_LED_red_PWM_level < 0) {
  497.     current_LED_red_PWM_level = 0;
  498.   }
  499.   if (current_LED_green_PWM_level > 255) {
  500.     current_LED_green_PWM_level = 255;
  501.   }
  502.   if (current_LED_green_PWM_level < 0) {
  503.     current_LED_green_PWM_level = 0;
  504.   }
  505.   if (current_LED_blue_PWM_level > 255) {
  506.     current_LED_blue_PWM_level = 255;
  507.   }
  508.   if (current_LED_blue_PWM_level < 0) {
  509.     current_LED_blue_PWM_level = 0;
  510.   }
  511. }
  512.  
  513. void write_to_LED_pins() {
  514.   /*
  515.      This function contains some code so that the PWM values are only set if they are different than the previous values.
  516.      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.
  517.   */
  518.   if (current_LED_red_PWM_level != previous_LED_red_PWM_level) {
  519.     previous_LED_red_PWM_level = current_LED_red_PWM_level;
  520.     analogWrite(LED_red_pin, current_LED_red_PWM_level);
  521.   }
  522.   if (current_LED_green_PWM_level != previous_LED_green_PWM_level) {
  523.     previous_LED_green_PWM_level = current_LED_green_PWM_level;
  524.     analogWrite(LED_green_pin, current_LED_green_PWM_level);
  525.   }
  526.   if (current_LED_blue_PWM_level != previous_LED_blue_PWM_level) {
  527.     previous_LED_blue_PWM_level = current_LED_blue_PWM_level;
  528.     analogWrite(LED_blue_pin, current_LED_blue_PWM_level);
  529.   }
  530. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top