Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Reddit relay blinker - v 2.0
- September 16, 2019
- This sketch is for randomly blinking 10 different pins/relays/LED strings on and off.
- Both the blink-on and the blink-off times are randomly generated, using random(100, 1000).
- It shows serial messages for the count-down time, and whenever the functions to generate new blink-on and blink-off times are called.
- */
- const int relay_pins[] = { -1, 2, 3, 4, 5, 6, 7, 8, 13, 10, 11};
- int relay_state[11];
- unsigned long previousMillis[11];
- unsigned long currentMillis_time = 0; // Only one of these is needed since only one check can be done at a time.
- unsigned long temporary_compare_time = 0;
- // The below two variable arrays are milliseconds time values:
- long blink_on_interval[11]; // 11 values total, the first value is not used. (This is the time that the pin will stay on)
- long blink_off_interval[11]; // 11 values total, the first value is not used. (This is the time that the pin will stay off)
- // This is the variables for the "reset" countdown timer.
- int reset_timer__reset_interval_in_seconds = 20; // This is how often a new set of random blink times will be generated.
- int reset_timer__seconds_counter = reset_timer__reset_interval_in_seconds;
- unsigned long reset_timer_1_second_begin = 0;
- unsigned long reset_timer__current_time = 0;
- void setup() {
- Serial.begin(9600);
- 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 high:
- relay_state[x] = HIGH;
- // Turn all the pins/relays on.
- digitalWrite(relay_pins[x], relay_state[x]);
- // Initialize the previousMillis values all to zero:
- previousMillis[x] = 0;
- }
- generate_random_blink_on_times();
- generate_random_blink_off_times();
- Serial.println("Exiting setup()");
- }
- void loop() {
- check_reset_countdown_timer(); // This is set to 20 seconds, for my own testing.
- 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.
- }
- void generate_random_blink_on_times() {
- Serial.println("New random blink-on times!");
- for (int x = 1; x < 11; x++) {
- blink_on_interval[x] = random(500, 3000);
- }
- }
- void generate_random_blink_off_times() {
- Serial.println("New random blink-off times!");
- for (int x = 1; x < 11; x++) {
- blink_off_interval[x] = random(100, 1000);
- }
- }
- 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 = reset_timer__reset_interval_in_seconds; // Reset the seconds counter back to the starting value.
- generate_random_blink_on_times();
- generate_random_blink_off_times();
- }
- reset_timer_1_second_begin = millis();
- }
- }
- else {
- reset_timer_1_second_begin = millis();
- }
- }
- void check_led_blinker_timer() {
- // This loops through all ten LEDs.
- // 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.
- // 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.
- currentMillis_time = millis();
- for (int x = 1; x < 11; x++) {
- if (relay_state[x] == HIGH) {
- temporary_compare_time = blink_on_interval[x];
- }
- else {
- temporary_compare_time = blink_off_interval[x];
- }
- if (currentMillis_time >= previousMillis[x]) {
- if (currentMillis_time >= (previousMillis[x] + temporary_compare_time)) {
- invert_this_relay_state(x);
- previousMillis[x] = millis();
- digitalWrite(relay_pins[x], relay_state[x]);
- }
- }
- else {
- previousMillis[x] = millis();
- }
- }
- }
- void invert_this_relay_state(int relayNum) {
- // All this function does is invert the value of the relay state (number) given.
- if (relay_state[relayNum] == 0) {
- relay_state[relayNum] = 1;
- }
- else {
- relay_state[relayNum] = 0;
- }
- }
- // ~~~~~~~ end ~~~~~~~~
Add Comment
Please, Sign In to add comment