skizziks_53

Reddit arduino relay blinker v1.0

Sep 14th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.74 KB | None | 0 0
  1. /*
  2.   Reddit relay blinker - v 1.0
  3.   September 14, 20
  4.  
  5.   This sketch is for randomly blinking 10 different pins/relays/LED strings on and off.
  6.  
  7.   Notes:
  8.       1. I used arrays to replace all of the single declarations, since doing so is less typing. Also it allows progressing through all of them with a loop, although that was not done in this sketch.
  9.       2. I have separated all of the code that was in the main loop into functions, so that it is more understandable.
  10.       3. This sketch does contain some unused variables.
  11.       4. The last function at the bottom ---- flip_relay_state(int thisRelay) ---- has an additional note in it.
  12.       5. This sketch would blink the connected relays a lot. I would suggest using solid-state relays, if you were not already.
  13. */
  14.  
  15. const int relay_pins[] = { -1, 2, 3, 4, 5, 6, 7, 8, 13, 10, 11};
  16. // In the above array, a place-holder value was used in the first index, so that the intended values begin at relay_Pins[1].
  17. // I initialized the values of this array literally, since the pin numbers in it are not in numerical order.
  18.  
  19. int relay_state[11];
  20. // Once again--this array has 11 values, but the first one (index zero) will not be used.
  21. // This array will be initialized to all-zeros in the setup() function.
  22.  
  23. unsigned long previousMillis[11];
  24. // Again--this array has 11 values, but the first one (index zero) will not be used.
  25. // This array will be initialized to all-zeros in the setup() function.
  26.  
  27. long blink_on_interval[] = {100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100}; // 11 values total, the first value is not used.
  28. // These values are all the same, but you might want to set them to different values.
  29. // That is easier if they are declared in-line, so I declared the values here.
  30. /*
  31.    Since you are setting these to random values anyway,
  32.    you could just initialize the array to random values in the setup() function.
  33. */
  34.  
  35.  
  36. long blink_off_interval[] = {1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000}; // 11 values total, the first value is not used.
  37. // And again--these values are all the same, but you might want to set them to different values.
  38. // That is easier if they are declared in-line, so I declared the values here.
  39. /*
  40.    The above variable is not being used at all...?
  41. */
  42.  
  43. // This is the variables for the "reset" countdown timer.
  44. int reset_timer__reset_interval_in_seconds = 60; // This is how often a new set of random blink times will be generated.
  45. int reset_timer__seconds_counter = 0;
  46. unsigned long reset_timer_1_second_begin = 0;
  47. unsigned long reset_timer__current_time = 0;
  48.  
  49.  
  50.  
  51.  
  52. void setup() {
  53.   Serial.begin(9600);
  54.   //interval1 = random(100, 1000);
  55.   for (int x = 1; x < 11; x++) {
  56.     // set the digital pins as outputs:
  57.     pinMode(relay_pins[x], OUTPUT);
  58.     // Initialize the relay_state values all to low:
  59.     relay_state[x] = LOW;
  60.     // Initialize the previousMillis values all to zero:
  61.     previousMillis[x] = 0;
  62.   }
  63.   Serial.println("Exiting setup()");
  64. }
  65.  
  66.  
  67.  
  68.  
  69. void loop() {
  70.  
  71.   check_reset_countdown_timer(); // As it is set, this will shut the LEDs off, generate new random blink values and start the LEDs up again every 60 seconds.
  72.  
  73.   check_led_blinker_timer(); // This part always runs.
  74.  
  75. }
  76.  
  77.  
  78.  
  79.  
  80. void check_reset_countdown_timer() {
  81.  
  82.   reset_timer__current_time = millis();
  83.   if (reset_timer__current_time >= reset_timer_1_second_begin) {
  84.     if (reset_timer__current_time >= (reset_timer_1_second_begin + 1000)) {
  85.       reset_timer__seconds_counter--;
  86.       Serial.print("timer = ");
  87.       Serial.println(reset_timer__seconds_counter);
  88.       if (reset_timer__seconds_counter <= 0) {
  89.         reset_timer__seconds_counter = 60; // Reset the seconds counter back to 60.
  90.         turn_all_relays_off();
  91.         generate_random_intervals(); // This generates new blink intervals every time the reset timer resets.
  92.         start_led_blinker_timer(); // This initializes the LED blinker and starts it running.
  93.         turn_all_relays_on(); // This is optional to do here, but it should make all the LEDs start out blinking on.
  94.       }
  95.       reset_timer_1_second_begin = millis();
  96.     }
  97.   }
  98.   else {
  99.     reset_timer_1_second_begin = millis();
  100.   }
  101. }
  102.  
  103. void check_led_blinker_timer() {
  104.   check_led_group_1();
  105.   check_led_group_2();
  106.   check_led_group_3();
  107.   check_led_group_4();
  108.   check_led_group_5();
  109. }
  110.  
  111.  
  112. void start_led_blinker_timer() {
  113.   reset_all_relay_timers();
  114.   turn_all_relays_on();
  115. }
  116.  
  117.  
  118. void reset_all_relay_timers() {
  119.   for (int x = 1; x < 11; x++) {
  120.     // Initialize the previousMillis values all to the current time:
  121.     previousMillis[x] = millis();
  122.   }
  123. }
  124.  
  125.  
  126. void turn_all_relays_on() {
  127.   // This turns all the relays on, so they begin from the same state when the LED blinker code starts up.
  128.   for (int x = 1; x < 11; x++) {
  129.     relay_state[x] = HIGH;
  130.     digitalWrite(relay_pins[x], relay_state[x]);
  131.   }
  132. }
  133.  
  134.  
  135. void turn_all_relays_off() {
  136.   // This turns off all the relays when the LED timing cycle is done.
  137.   for (int x = 1; x < 11; x++) {
  138.     relay_state[x] = LOW;
  139.     digitalWrite(relay_pins[x], relay_state[x]);
  140.   }
  141. }
  142.  
  143.  
  144. void generate_random_intervals() {
  145.   Serial.println("new random intervals");
  146.   blink_on_interval[1] = random(50, 3000);
  147.   blink_on_interval[2] = random(50, 1000);
  148.   blink_on_interval[3] = random(50, 10000);
  149.   blink_on_interval[4] = random(50, 500);
  150.   blink_on_interval[5] = random(50, 2000);
  151. }
  152.  
  153.  
  154. void check_led_group_1() {
  155.   if (millis() - previousMillis[1] >= blink_on_interval[1]) {
  156.     // save the last time you blinked the LED
  157.     previousMillis[1] = millis();
  158.     // if the LED is off turn it on and vice-versa:
  159.     /*
  160.       All the lines below were doing the same thing for all five relay groups, so you could have a function for that.
  161.       if (relay_state[1] == LOW) {
  162.       relay_state[1] = HIGH;
  163.       blink_on_interval[1] = blink_on_interval[1] / 10;
  164.       } else {
  165.       relay_state[1] = LOW;
  166.       blink_on_interval[1] = blink_on_interval[1] * 10;
  167.       }
  168.     */
  169.     flip_relay_state(1); // This is the function that replaced the above.
  170.     // set the LED with the ledState of the variable:
  171.     digitalWrite(relay_pins[1], relay_state[1]);
  172.   }
  173. }
  174.  
  175.  
  176. void check_led_group_2() {
  177.   if (millis() - previousMillis[2] >= blink_on_interval[2]) {
  178.     // save the last time you blinked the LED
  179.     previousMillis[2] = millis();
  180.     // if the LED is off turn it on and vice-versa:
  181.     flip_relay_state(2);
  182.     // set the LED with the ledState of the variable:
  183.     digitalWrite(relay_pins[2], relay_state[2]);
  184.   }
  185. }
  186.  
  187.  
  188. void check_led_group_3() {
  189.   if (millis() - previousMillis[3] >= blink_on_interval[3]) {
  190.     // save the last time you blinked the LED
  191.     previousMillis[3] = millis();
  192.     // if the LED is off turn it on and vice-versa:
  193.     flip_relay_state(3);
  194.     // set the LED with the ledState of the variable:
  195.     digitalWrite(relay_pins[3], relay_state[3]);
  196.   }
  197. }
  198.  
  199.  
  200. void check_led_group_4() {
  201.   if (millis() - previousMillis[4] >= blink_on_interval[4]) {
  202.     // save the last time you blinked the LED
  203.     previousMillis[4] = millis();
  204.     // if the LED is off turn it on and vice-versa:
  205.     flip_relay_state(4);
  206.     // set the LED with the ledState of the variable:
  207.     digitalWrite(relay_pins[4], relay_state[4]);
  208.     digitalWrite(relay_pins[8], relay_state[4]);
  209.     digitalWrite(relay_pins[9], relay_state[4]);
  210.     digitalWrite(relay_pins[10], relay_state[4]);
  211.   }
  212. }
  213.  
  214.  
  215. void check_led_group_5() {
  216.   if (millis() - previousMillis[5] >= blink_on_interval[5]) {
  217.     // save the last time you blinked the LED
  218.     previousMillis[5] = millis();
  219.     // if the LED is off turn it on and vice-versa:
  220.     flip_relay_state(5);
  221.     // set the LED with the ledState of the variable:
  222.     digitalWrite(relay_pins[5], relay_state[5]);
  223.     digitalWrite(relay_pins[6], relay_state[5]);
  224.     digitalWrite(relay_pins[7], relay_state[5]);
  225.   }
  226. }
  227.  
  228.  
  229. void flip_relay_state(int thisRelay) {
  230.   // This function inverts the relay value given to it, and also changes the blink_on_interval as desired.
  231.   /*
  232.      This function is not really written very well.
  233.      If you have a program where you repeatedly perform floating-point math on the same number, back and forth repeatedly with multiply and divide,
  234.         eventually you get rounding errors that cause the intended values to drift.
  235.      It would be safer to generate the two values ahead of time and store them as whole numbers (with no decimal component) and then just copy them as needed.
  236.   */
  237.  
  238.   if (relay_state[thisRelay] == LOW) {
  239.     relay_state[thisRelay] = HIGH;
  240.     blink_on_interval[thisRelay] = blink_on_interval[thisRelay] / 10;
  241.   }
  242.   else {
  243.     relay_state[thisRelay] = LOW;
  244.     blink_on_interval[thisRelay] = blink_on_interval[thisRelay] * 10;
  245.   }
  246.   return relay_state[thisRelay];
  247. }
  248.  
  249.  
  250. // ~~~~ the end ~~~~~
Add Comment
Please, Sign In to add comment