Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Reddit relay blinker - v 1.0
- September 14, 20
- This sketch is for randomly blinking 10 different pins/relays/LED strings on and off.
- Notes:
- 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.
- 2. I have separated all of the code that was in the main loop into functions, so that it is more understandable.
- 3. This sketch does contain some unused variables.
- 4. The last function at the bottom ---- flip_relay_state(int thisRelay) ---- has an additional note in it.
- 5. This sketch would blink the connected relays a lot. I would suggest using solid-state relays, if you were not already.
- */
- const int relay_pins[] = { -1, 2, 3, 4, 5, 6, 7, 8, 13, 10, 11};
- // In the above array, a place-holder value was used in the first index, so that the intended values begin at relay_Pins[1].
- // I initialized the values of this array literally, since the pin numbers in it are not in numerical order.
- int relay_state[11];
- // Once again--this array has 11 values, but the first one (index zero) will not be used.
- // This array will be initialized to all-zeros in the setup() function.
- unsigned long previousMillis[11];
- // Again--this array has 11 values, but the first one (index zero) will not be used.
- // This array will be initialized to all-zeros in the setup() function.
- long blink_on_interval[] = {100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100}; // 11 values total, the first value is not used.
- // These values are all the same, but you might want to set them to different values.
- // That is easier if they are declared in-line, so I declared the values here.
- /*
- Since you are setting these to random values anyway,
- you could just initialize the array to random values in the setup() function.
- */
- long blink_off_interval[] = {1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000}; // 11 values total, the first value is not used.
- // And again--these values are all the same, but you might want to set them to different values.
- // That is easier if they are declared in-line, so I declared the values here.
- /*
- The above variable is not being used at all...?
- */
- // This is the variables for the "reset" countdown timer.
- int reset_timer__reset_interval_in_seconds = 60; // This is how often a new set of random blink times will be generated.
- int reset_timer__seconds_counter = 0;
- unsigned long reset_timer_1_second_begin = 0;
- unsigned long reset_timer__current_time = 0;
- void setup() {
- Serial.begin(9600);
- //interval1 = random(100, 1000);
- for (int x = 1; x < 11; x++) {
- // set the digital pins as outputs:
- pinMode(relay_pins[x], OUTPUT);
- // Initialize the relay_state values all to low:
- relay_state[x] = LOW;
- // Initialize the previousMillis values all to zero:
- previousMillis[x] = 0;
- }
- Serial.println("Exiting setup()");
- }
- void loop() {
- 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.
- check_led_blinker_timer(); // This part always runs.
- }
- void check_reset_countdown_timer() {
- reset_timer__current_time = millis();
- if (reset_timer__current_time >= reset_timer_1_second_begin) {
- if (reset_timer__current_time >= (reset_timer_1_second_begin + 1000)) {
- reset_timer__seconds_counter--;
- Serial.print("timer = ");
- Serial.println(reset_timer__seconds_counter);
- if (reset_timer__seconds_counter <= 0) {
- reset_timer__seconds_counter = 60; // Reset the seconds counter back to 60.
- turn_all_relays_off();
- generate_random_intervals(); // This generates new blink intervals every time the reset timer resets.
- start_led_blinker_timer(); // This initializes the LED blinker and starts it running.
- turn_all_relays_on(); // This is optional to do here, but it should make all the LEDs start out blinking on.
- }
- reset_timer_1_second_begin = millis();
- }
- }
- else {
- reset_timer_1_second_begin = millis();
- }
- }
- void check_led_blinker_timer() {
- check_led_group_1();
- check_led_group_2();
- check_led_group_3();
- check_led_group_4();
- check_led_group_5();
- }
- void start_led_blinker_timer() {
- reset_all_relay_timers();
- turn_all_relays_on();
- }
- void reset_all_relay_timers() {
- for (int x = 1; x < 11; x++) {
- // Initialize the previousMillis values all to the current time:
- previousMillis[x] = millis();
- }
- }
- void turn_all_relays_on() {
- // This turns all the relays on, so they begin from the same state when the LED blinker code starts up.
- for (int x = 1; x < 11; x++) {
- relay_state[x] = HIGH;
- digitalWrite(relay_pins[x], relay_state[x]);
- }
- }
- void turn_all_relays_off() {
- // This turns off all the relays when the LED timing cycle is done.
- for (int x = 1; x < 11; x++) {
- relay_state[x] = LOW;
- digitalWrite(relay_pins[x], relay_state[x]);
- }
- }
- void generate_random_intervals() {
- Serial.println("new random intervals");
- blink_on_interval[1] = random(50, 3000);
- blink_on_interval[2] = random(50, 1000);
- blink_on_interval[3] = random(50, 10000);
- blink_on_interval[4] = random(50, 500);
- blink_on_interval[5] = random(50, 2000);
- }
- void check_led_group_1() {
- if (millis() - previousMillis[1] >= blink_on_interval[1]) {
- // save the last time you blinked the LED
- previousMillis[1] = millis();
- // if the LED is off turn it on and vice-versa:
- /*
- All the lines below were doing the same thing for all five relay groups, so you could have a function for that.
- if (relay_state[1] == LOW) {
- relay_state[1] = HIGH;
- blink_on_interval[1] = blink_on_interval[1] / 10;
- } else {
- relay_state[1] = LOW;
- blink_on_interval[1] = blink_on_interval[1] * 10;
- }
- */
- flip_relay_state(1); // This is the function that replaced the above.
- // set the LED with the ledState of the variable:
- digitalWrite(relay_pins[1], relay_state[1]);
- }
- }
- void check_led_group_2() {
- if (millis() - previousMillis[2] >= blink_on_interval[2]) {
- // save the last time you blinked the LED
- previousMillis[2] = millis();
- // if the LED is off turn it on and vice-versa:
- flip_relay_state(2);
- // set the LED with the ledState of the variable:
- digitalWrite(relay_pins[2], relay_state[2]);
- }
- }
- void check_led_group_3() {
- if (millis() - previousMillis[3] >= blink_on_interval[3]) {
- // save the last time you blinked the LED
- previousMillis[3] = millis();
- // if the LED is off turn it on and vice-versa:
- flip_relay_state(3);
- // set the LED with the ledState of the variable:
- digitalWrite(relay_pins[3], relay_state[3]);
- }
- }
- void check_led_group_4() {
- if (millis() - previousMillis[4] >= blink_on_interval[4]) {
- // save the last time you blinked the LED
- previousMillis[4] = millis();
- // if the LED is off turn it on and vice-versa:
- flip_relay_state(4);
- // set the LED with the ledState of the variable:
- digitalWrite(relay_pins[4], relay_state[4]);
- digitalWrite(relay_pins[8], relay_state[4]);
- digitalWrite(relay_pins[9], relay_state[4]);
- digitalWrite(relay_pins[10], relay_state[4]);
- }
- }
- void check_led_group_5() {
- if (millis() - previousMillis[5] >= blink_on_interval[5]) {
- // save the last time you blinked the LED
- previousMillis[5] = millis();
- // if the LED is off turn it on and vice-versa:
- flip_relay_state(5);
- // set the LED with the ledState of the variable:
- digitalWrite(relay_pins[5], relay_state[5]);
- digitalWrite(relay_pins[6], relay_state[5]);
- digitalWrite(relay_pins[7], relay_state[5]);
- }
- }
- void flip_relay_state(int thisRelay) {
- // This function inverts the relay value given to it, and also changes the blink_on_interval as desired.
- /*
- This function is not really written very well.
- If you have a program where you repeatedly perform floating-point math on the same number, back and forth repeatedly with multiply and divide,
- eventually you get rounding errors that cause the intended values to drift.
- 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.
- */
- if (relay_state[thisRelay] == LOW) {
- relay_state[thisRelay] = HIGH;
- blink_on_interval[thisRelay] = blink_on_interval[thisRelay] / 10;
- }
- else {
- relay_state[thisRelay] = LOW;
- blink_on_interval[thisRelay] = blink_on_interval[thisRelay] * 10;
- }
- return relay_state[thisRelay];
- }
- // ~~~~ the end ~~~~~
Add Comment
Please, Sign In to add comment