skizziks_53

Reddit arduino relay blinker v2.0

Sep 16th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.36 KB | None | 0 0
  1. /*
  2.   Reddit relay blinker - v 2.0
  3.   September 16, 2019
  4.  
  5.   This sketch is for randomly blinking 10 different pins/relays/LED strings on and off.
  6.   Both the blink-on and the blink-off times are randomly generated, using random(100, 1000).
  7.   It shows serial messages for the count-down time, and whenever the functions to generate new blink-on and blink-off times are called.
  8.  
  9. */
  10.  
  11. const int relay_pins[] = { -1, 2, 3, 4, 5, 6, 7, 8, 13, 10, 11};
  12.  
  13. int relay_state[11];
  14.  
  15. unsigned long previousMillis[11];
  16. unsigned long currentMillis_time = 0; // Only one of these is needed since only one check can be done at a time.
  17. unsigned long temporary_compare_time = 0;
  18.  
  19. // The below two variable arrays are milliseconds time values:
  20. long blink_on_interval[11]; // 11 values total, the first value is not used. (This is the time that the pin will stay on)
  21. long blink_off_interval[11]; // 11 values total, the first value is not used. (This is the time that the pin will stay off)
  22.  
  23.  
  24. // This is the variables for the "reset" countdown timer.
  25. int reset_timer__reset_interval_in_seconds = 20; // This is how often a new set of random blink times will be generated.
  26. int reset_timer__seconds_counter = reset_timer__reset_interval_in_seconds;
  27. unsigned long reset_timer_1_second_begin = 0;
  28. unsigned long reset_timer__current_time = 0;
  29.  
  30.  
  31.  
  32.  
  33. void setup() {
  34.   Serial.begin(9600);
  35.   for (int x = 1; x < 11; x++) {
  36.     // set the digital pins as outputs:
  37.     pinMode(relay_pins[x], OUTPUT);
  38.     // Initialize the relay_state values all to high:
  39.     relay_state[x] = HIGH;
  40.     // Turn all the pins/relays on.
  41.     digitalWrite(relay_pins[x], relay_state[x]);
  42.     // Initialize the previousMillis values all to zero:
  43.     previousMillis[x] = 0;
  44.   }
  45.   generate_random_blink_on_times();
  46.   generate_random_blink_off_times();
  47.   Serial.println("Exiting setup()");
  48. }
  49.  
  50.  
  51.  
  52. void loop() {
  53.  
  54.   check_reset_countdown_timer(); // This is set to 20 seconds, for my own testing.
  55.  
  56.   check_led_blinker_timer(); // This function gets run on every pass through the loop, but it loops through all the pins and only changes each pin if needed.
  57.  
  58. }
  59.  
  60.  
  61.  
  62. void generate_random_blink_on_times() {
  63.   Serial.println("New random blink-on times!");
  64.   for (int x = 1; x < 11; x++) {
  65.     blink_on_interval[x] = random(500, 3000);
  66.   }
  67. }
  68.  
  69.  
  70.  
  71. void generate_random_blink_off_times() {
  72.   Serial.println("New random blink-off times!");
  73.   for (int x = 1; x < 11; x++) {
  74.     blink_off_interval[x] = random(100, 1000);
  75.   }
  76. }
  77.  
  78.  
  79.  
  80. void check_reset_countdown_timer() {
  81.   reset_timer__current_time = millis();
  82.   if (reset_timer__current_time >= reset_timer_1_second_begin) {
  83.     if (reset_timer__current_time >= (reset_timer_1_second_begin + 1000)) {
  84.       reset_timer__seconds_counter--;
  85.       Serial.print("timer = ");
  86.       Serial.println(reset_timer__seconds_counter);
  87.       if (reset_timer__seconds_counter <= 0) {
  88.         reset_timer__seconds_counter = reset_timer__reset_interval_in_seconds; // Reset the seconds counter back to the starting value.
  89.         generate_random_blink_on_times();
  90.         generate_random_blink_off_times();
  91.       }
  92.       reset_timer_1_second_begin = millis();
  93.     }
  94.   }
  95.   else {
  96.     reset_timer_1_second_begin = millis();
  97.   }
  98. }
  99.  
  100.  
  101.  
  102. void check_led_blinker_timer() {
  103.   // This loops through all ten LEDs.
  104.   // If they are in the 'on' state, it checks if the 'blink-on' time has passed and changes the led to 'off' if it has.
  105.   // If they are in the 'off' state, it checks if the 'blink-off' time has passed and changes the led to 'on' if it has.
  106.   currentMillis_time = millis();
  107.   for (int x = 1; x < 11; x++) {
  108.     if (relay_state[x] == HIGH) {
  109.       temporary_compare_time = blink_on_interval[x];
  110.     }
  111.     else {
  112.       temporary_compare_time = blink_off_interval[x];
  113.     }
  114.     if (currentMillis_time >= previousMillis[x]) {
  115.       if (currentMillis_time >= (previousMillis[x] + temporary_compare_time)) {
  116.         invert_this_relay_state(x);
  117.         previousMillis[x] = millis();
  118.         digitalWrite(relay_pins[x], relay_state[x]);
  119.       }
  120.     }
  121.     else {
  122.       previousMillis[x] = millis();
  123.     }
  124.   }
  125. }
  126.  
  127.  
  128. void invert_this_relay_state(int relayNum) {
  129.   // All this function does is invert the value of the relay state (number) given.
  130.   if (relay_state[relayNum] == 0) {
  131.     relay_state[relayNum] = 1;
  132.   }
  133.   else {
  134.     relay_state[relayNum] = 0;
  135.   }
  136. }
  137.  
  138.  
  139. // ~~~~~~~ end ~~~~~~~~
Add Comment
Please, Sign In to add comment