Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Reddit 5-relay gate sketch v.1
- March 13, 2018
- For having four relays that need delayed functions, it is easier IMO to just make a class and give them all the methods.
- If a relay does not need a turn-on delay, then you just set that property to zero.
- The flasher is also placed into a class as well.
- The classes are written in-line first, and the object declarations begin on line 255.
- Setup() begins on line 300 and the main loop() begins on line 312.
- */
- // Below is one variable that you can use to enable or disable all the serial prints at once.
- bool show_serialPrint_statements = true;
- const int button_Pin = 2; // the number of the pushbutton pin
- const int open_right_gate_relay_pin = 10; // the number of the relay pin
- const int open_left_gate_relay_pin = 11; // the number of the relay pin
- const int close_right_gate_relay_pin = 8; // the number of the relay pin
- const int close_left_gate_relay_pin = 9; // the number of the relay pin
- const int light_gate_relay_pin = 12; // the number of the relay pin
- // CLASS BEGINS ######################################################################################
- class Gate_Relay {
- private:
- unsigned long relay_start_time = 0; // This is the starting time of whatever mode the relay is in.
- unsigned long relay_current_time = 0; // This is to get the current time in.
- int pin_number; // the pin number of this relay.
- float start_delay; // This is the time delay to wait after being started, to turn the relay on.
- float run_time; // This is the time to keep the relay on.
- public:
- Gate_Relay(int, float, float);
- void beginRelay(unsigned long);
- void checkRelay();
- int relay_mode; // This is a number that tells what mode the relay is in: 0=relay off, 1=startup delay, 2=relay on, 3=shutting off
- };
- Gate_Relay::Gate_Relay(int pin, float sd, float rt) { // This is the constructor.
- pin_number = pin;
- relay_mode = 0;
- start_delay = sd;
- run_time = rt;
- };
- void Gate_Relay::beginRelay(unsigned long thisTime) {
- relay_start_time = thisTime;
- relay_mode = 1;
- };
- void Gate_Relay::checkRelay() {
- switch (relay_mode) {
- case 0:
- // There is nothing to do for relay_mode == 0, because this means the relay is [off].
- // This method does not change the relay mode from zero to 1, the beginRelay method does that.
- break;
- case 1:
- // This is what happens if the relay is in the startup delay:
- relay_current_time = millis();
- if (relay_current_time > relay_start_time) {
- if (relay_current_time > (relay_start_time + (start_delay * 1000))) { // If the start_delay time has passed, then:
- relay_mode = 2; // switch the relay mode to the next [run] mode.
- relay_start_time = millis(); // reset the relay start time, since now it has to time how long the relay pin is HIGH
- digitalWrite(pin_number, HIGH);
- // Below writes the serial.print lines for each of the relays.
- if (show_serialPrint_statements == true) {
- switch (pin_number) {
- case open_right_gate_relay_pin:
- Serial.println("open_right_gate_relay = ON");
- break;
- case open_left_gate_relay_pin:
- Serial.println("open_left_gate_relay = ON");
- break;
- case close_right_gate_relay_pin:
- Serial.println("close_right_gate_relay = ON");
- break;
- case close_left_gate_relay_pin:
- Serial.println("close_left_gate_relay = ON");
- break;
- }
- }
- }
- }
- else {
- relay_start_time = millis(); // This is to reset the relay_start_time in case millis() has rolled over.
- }
- break;
- case 2:
- // This is what happens if the relay is in the run (HIGH) mode:
- relay_current_time = millis();
- if (relay_current_time > relay_start_time) {
- if (relay_current_time > (relay_start_time + (run_time * 1000))) {
- // If the time to keep the relay on has passed, then:
- relay_mode = 3; // switch the relay mode to the [shutting off] mode
- relay_start_time = millis(); // This is to reset the relay_start_time in case millis() has rolled over.
- }
- }
- else {
- relay_start_time = millis(); // This is to reset the relay_start_time in case millis() has rolled over.
- }
- break;
- case 3:
- digitalWrite(pin_number, LOW); // Turn the relay pin off again.
- relay_mode = 0; // shut off mode
- // Below writes the serial.print lines for each of the relays.
- if (show_serialPrint_statements == true) {
- switch (pin_number) {
- case open_right_gate_relay_pin:
- Serial.println("open_right_gate_relay = OFF");
- break;
- case open_left_gate_relay_pin:
- Serial.println("open_left_gate_relay = OFF");
- break;
- case close_right_gate_relay_pin:
- Serial.println("close_right_gate_relay = OFF");
- break;
- case close_left_gate_relay_pin:
- Serial.println("close_left_gate_relay = OFF");
- break;
- }
- }
- break;
- default:
- // nothing here
- break; // The Arduino switch-case example leaves off the ending {break;} statement ???
- // For any condition you should also have a closing {break;} statement.
- // Some compilers will say it's wrong if you don't do that.
- }
- };
- // CLASS ENDS ######################################################################################
- // CLASS BEGINS ######################################################################################
- class Flasher_Light {
- private:
- unsigned long flash_start_time = 0; // This is the starting time of whatever mode the relay is in.
- unsigned long flash_current_time = 0; // This is to get the current time in, to change the mode of the flasher.
- int pin_number; // the pin number of this relay.
- int blink_interval; // This is the time in milliseconds to blink this flasher on and off.
- unsigned long blink_start_time; // These two variables are used for blinking the relay pin on and off.
- unsigned long blink_current_time;
- float start_delay; // This is the time delay in seconds to wait after the button is pressed.
- // The flasher just runs constantly until it shuts down, so it doesn't have a 'run time'.
- float shutdown_delay; // This is the time (in seconds) to keep running after the button is released.
- int flasher_mode; // 0=flasher off, 1=startup delay, 2=flasher on, 3=flasher shutting down
- int flash_pin_state = 0; // This is used for cycling the flasher pin on and off.
- public:
- Flasher_Light(int, float, int, float);
- void beginFlasher(unsigned long);
- void checkFlasher();
- void shutDownFlasher(unsigned long);
- };
- Flasher_Light::Flasher_Light(int pin, float start_d, int f_interval, float stop_d) { // This is the constructor used.
- pin_number = pin;
- blink_interval = f_interval;
- flasher_mode = 0;
- start_delay = start_d;
- shutdown_delay = stop_d;
- };
- void Flasher_Light::beginFlasher(unsigned long thisTime) {
- flash_start_time = thisTime;
- blink_start_time = thisTime;
- flasher_mode = 1;
- flash_pin_state = 1;
- digitalWrite(pin_number, flash_pin_state);
- // Below writes the serial.print lines for each of the relays.
- if (show_serialPrint_statements == true) {
- Serial.println("flasher turning on + flasher blink = ON");
- // I also print the flasher pin state here, since it is starting out set to 'on'.
- }
- };
- void Flasher_Light::checkFlasher() {
- // int flasher_mode; // 0=flasher off, 1=startup delay, 2=flasher on, 3=flasher shutting down
- switch (flasher_mode) {
- case 0:
- // flasher is not active
- break;
- case 1:
- // This is the start-up delay:
- flash_current_time = millis();
- if (flash_current_time > flash_start_time) {
- if (flash_current_time > (flash_start_time + (start_delay * 1000))) {
- // If the start_delay time has passed, then:
- flasher_mode = 2; // switch the flasher mode to the [run] mode
- // We don't need to do anything else here, because the purpose of mode#2 is just to make sure the flasher does not run mode#1 or mode#3 code.
- }
- }
- else {
- flash_start_time = millis(); // This is to reset the relay_start_time in case millis() has rolled over.
- }
- break;
- case 2:
- // -nothing here-
- // When the button is released, that triggers the flasher mode change from #2 to #3.
- break;
- case 3:
- // flasher shutting down
- flash_current_time = millis();
- if (flash_current_time > flash_start_time) {
- if (flash_current_time > (flash_start_time + (shutdown_delay * 1000))) {
- // If the shutdown_delay time has passed, then:
- flasher_mode = 0; // switch the flasher mode to the [off] mode
- if (show_serialPrint_statements == true) {
- Serial.println("flasher shutting off");
- }
- }
- }
- else {
- flash_start_time = millis(); // This is to reset the relay_start_time in case millis() has rolled over.
- }
- break;
- default:
- // nothing
- break;
- }
- // The code below blinks the light on and off.
- if (flasher_mode > 1) { // modes 0 and 1 are 'off', but modes 2 and 3 are 'flasher blinking':
- blink_current_time = millis();
- if (blink_current_time > blink_start_time) {
- if (blink_current_time > (blink_start_time + blink_interval)) {
- // This changes the flasher pin state if the blink_interval time has passed.
- if (flash_pin_state == 0) {
- flash_pin_state = 1;
- if (show_serialPrint_statements == true) {
- Serial.println("flasher blink = ON");
- }
- }
- else {
- flash_pin_state = 0;
- if (show_serialPrint_statements == true) {
- Serial.println("flasher blink = OFF");
- }
- }
- digitalWrite(pin_number, flash_pin_state);
- blink_start_time = millis(); // Reset the blink change time to now, since it just changed now.
- }
- }
- else {
- blink_start_time = millis(); // This is to reset the relay_start_time in case millis() has rolled over.
- }
- }
- };
- void Flasher_Light::shutDownFlasher(unsigned long thisTime) {
- flash_start_time = thisTime;
- flasher_mode = 3;
- };
- // CLASS ENDS ######################################################################################
- // Below is declaring Gate_Relay objects:
- //the constructor format is as follows: [int] pin number, [float] startup delay time in seconds, [float] relay run time in seconds
- Gate_Relay Open_right_gate_relay = Gate_Relay(open_right_gate_relay_pin, 0, 4);
- Gate_Relay Open_left_gate_relay = Gate_Relay(open_left_gate_relay_pin, 1, 4);
- Gate_Relay Close_right_gate_relay = Gate_Relay(close_right_gate_relay_pin, 2, 4);
- Gate_Relay Close_left_gate_relay = Gate_Relay(close_left_gate_relay_pin, 1, 4);
- // below is declaring the Flasher1 object:
- Flasher_Light Flasher1 = Flasher_Light(light_gate_relay_pin, 0, 500, 7.5);
- // constructor: [int] pin#, [float] start_delay (seconds), [int] blink_interval (milliseconds), [float] stop_delay (seconds)
- const int buttonCheck_interval_milliseconds = 500; // This is the milliseconds interval to check if the button has been pressed or not.
- const int button_debounce_time_milliseconds = 100; // This is the time used to de-bounce the button (the time that the button does not accept input after it is pressed).
- bool button_enabled = true; // This is to disable the button for debouncing.
- unsigned long previous_buttonCheckTime = 0; // This stores the clock time in milliseconds.
- unsigned long current_buttonCheckTime = 0; // This stores the clock time in milliseconds.
- // The button-checking code will constantly re-use the above two variables, so nothing else can use them.
- bool current_gate_state = false; // The button uses these two variables to make sure it only triggers on a button state change.
- bool previous_gate_state = false;
- unsigned long buttonPress_startTime = 0; // This is to save a copy of the time that the button is pressed.
- unsigned long buttonRelease_startTime = 0; // This is to save a copy of the time that the buttn is released.
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- // function prototypes:
- void check_button(); // This checks if the button has been pressed.
- void check_button_debounce(); // If the button was pressed, this checks if it can be enabled again.
- void open_the_gate(unsigned long); // This sets the gate state to "open".
- void close_the_gate(unsigned long); // This sets the gate state to "closed".
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- void setup() {
- Serial.begin(9600);
- pinMode(open_left_gate_relay_pin, OUTPUT);
- pinMode(open_right_gate_relay_pin, OUTPUT);
- pinMode(close_left_gate_relay_pin, OUTPUT);
- pinMode(close_right_gate_relay_pin, OUTPUT);
- pinMode(light_gate_relay_pin, OUTPUT);
- pinMode(button_Pin, INPUT_PULLUP); // I changed this to input_pullup because that way you don't need an external pulldown resistor.
- Serial.println("OK: exiting setup()");
- }
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- void loop() {
- if (button_enabled == true) { // This section only checks the button when it's not being de-bounced.
- check_button();
- }
- else {
- check_button_debounce();
- }
- Open_right_gate_relay.checkRelay();
- Open_left_gate_relay.checkRelay();
- Close_right_gate_relay.checkRelay();
- Close_left_gate_relay.checkRelay();
- Flasher1.checkFlasher();
- } // end of main loop()
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- void check_button() {
- // This checks if the button has been pressed.
- current_buttonCheckTime = millis();
- if (current_buttonCheckTime > previous_buttonCheckTime) {
- if (current_buttonCheckTime > (previous_buttonCheckTime + buttonCheck_interval_milliseconds)) {
- if (digitalRead(button_Pin) == LOW) { // If the button is being pressed...
- if (current_gate_state == false) {
- current_gate_state = true;
- if (previous_gate_state == false) { // This check is to make sure that the code inside ONLY runs the first time that the button goes from low to high.
- if (show_serialPrint_statements == true) {
- Serial.println("button pressed"); // This tells when the button is pressed, but only once when it goes from un-pressed to pressed.
- }
- button_enabled = false; // This turns off the button check long enough to avoid any bouncing.
- previous_gate_state = true; // This changes to true to prevent this code from running again, unless the button state goes from low to high again.
- buttonPress_startTime = millis();
- open_the_gate(buttonPress_startTime);
- }
- }
- }
- else { // If the button is NOT being pressed...
- if (current_gate_state == true) {
- if (previous_gate_state == true) {
- // This section is what happens after the button is released.
- previous_gate_state = false;
- if (show_serialPrint_statements == true) {
- Serial.println("button released"); // This tells when the button is released, but only once when it goes from pressed to released.
- }
- buttonRelease_startTime = millis(); // Save the time that the button was released.
- close_the_gate(buttonRelease_startTime);
- }
- current_gate_state = false;
- }
- }
- previous_buttonCheckTime = millis(); // Re-set the previous check time to now, since the button-press check was run.
- }
- }
- else {
- previous_buttonCheckTime = millis(); // millis() rollover condition
- }
- }
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- void check_button_debounce() {
- // If the button was pressed, this checks if it can be enabled again after the debouncing time interval has passed.
- current_buttonCheckTime = millis();
- if (current_buttonCheckTime > buttonPress_startTime) {
- if (current_buttonCheckTime > (buttonPress_startTime + button_debounce_time_milliseconds)) {
- button_enabled = true;
- if (show_serialPrint_statements == true) {
- Serial.println("button debounce finished");
- }
- }
- }
- else {
- buttonPress_startTime = millis(); // millis() rollover condition
- }
- }
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- void open_the_gate(unsigned long buttonPressedTime) {
- if (show_serialPrint_statements == true) {
- Serial.println("gate is opening");
- }
- Open_right_gate_relay.beginRelay(buttonPressedTime);
- Open_left_gate_relay.beginRelay(buttonPressedTime);
- Flasher1.beginFlasher(buttonPressedTime);
- }
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- void close_the_gate(unsigned long buttonReleasedTime) {
- // If the open-relays are not totally finished when the button is released.
- // they they get shut off before turning on the closing relays.
- if (show_serialPrint_statements == true) {
- Serial.println("gate is closing");
- }
- if (Open_right_gate_relay.relay_mode != 0) {
- Open_right_gate_relay.relay_mode = 3; // mode 3 is the relay turning-off mode
- }
- if (Open_left_gate_relay.relay_mode != 0) {
- Open_left_gate_relay.relay_mode = 3; // mode 3 is the relay turning-off mode
- }
- Close_right_gate_relay.beginRelay(buttonReleasedTime);
- Close_left_gate_relay.beginRelay(buttonReleasedTime);
- Flasher1.shutDownFlasher(buttonReleasedTime);
- }
- // ~~~~~~~ [end] ~~~~~~~~~~
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement