Advertisement
skizziks_53

3-relay gate sketch v.2

Apr 12th, 2018
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.83 KB | None | 0 0
  1. /*
  2.   Reddit 3-relay gate sketch
  3.   March 12, 2018
  4.  
  5.   This activates the relays as long as the button is held down.
  6.   Note that I have not tested this except in the serial monitor.
  7.  
  8. */
  9.  
  10. const int buttonPin = 2;     // the number of the pushbutton pin
  11. const int right_gate_relay =  10;      // the number of the relay pin
  12. const int left_gate_relay =  11;      // the number of the relay pin
  13. const int light_gate_relay =  12;      // the number of the relay pin
  14.  
  15. int buttonCheck_interval_milliseconds = 1000; //--(500 suggested value) This is the milliseconds interval to check if the button has been pressed or not.
  16. int button_debounce_time_milliseconds = 2000; //--(300 suggested value) This is the time used to de-bounce the button (the time that the button does not accept input after it is pressed).
  17. bool button_enabled = true; // This is to disable the button for debouncing.
  18. unsigned long previous_buttonCheckTime = 0; // This stores the clock time in milliseconds.
  19. unsigned long current_buttonCheckTime = 0; // This stores the clock time in milliseconds.
  20. // The button-checking code will constantly re-use the above two variables, so nothing else can use them.
  21.  
  22. unsigned long buttonPress_startTime = 0; // This is to save a copy of the time that the button is pressed.
  23. // Since you want other events to start after a set time after the button is pressed, then you need to save a copy the button press start time.
  24.  
  25. int left_relay_delay_milliseconds = 0; // This is the wait time in milliseconds to turn on the left gate relay.
  26. bool left_relay_on = false;
  27.  
  28. int right_relay_delay_milliseconds = 4000; //--(1000 original value) This is the wait time in milliseconds to turn on the right gate relay.
  29. bool right_relay_on = false;
  30.  
  31. int flash_delay_milliseconds = 3000; //--(500 original value) flash delay of the light relay
  32. int flash_state = 0; // This is to control the flash state
  33. unsigned long flashChange_previous_time = 0; // The flasher function is easiest if it can save its own two times.
  34. unsigned long flashChange_current_time = 0;
  35.  
  36. bool current_gate_state = false; // This is a state flag that tells if the gate is open or not.
  37. // You need this state to persist outside of the button code because when the button is de-bouncing,
  38. // the button code that sets this variable to true won't be used (it gets skipped).
  39. bool previous_gate_state = false; // If you want an event to occur at a change, then you need to track the current state & the previous state.
  40.  
  41. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  42.  
  43. // function prototypes:
  44. void check_button();
  45. void check_left_relay();
  46. void check_right_relay();
  47. void check_flasher();
  48. void shut_gate_off();
  49.  
  50. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  51.  
  52. void setup() {
  53.   Serial.begin(9600);
  54.   pinMode(left_gate_relay, OUTPUT);
  55.   pinMode(right_gate_relay, OUTPUT);
  56.   pinMode(light_gate_relay, OUTPUT);
  57.   pinMode(buttonPin, INPUT_PULLUP); // I changed this to input_pullup because that way you don't need an external pulldown resistor.
  58.   Serial.println("OK: exiting setup()");
  59. }
  60.  
  61. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  62.  
  63. void loop() {
  64.   if (button_enabled == true) { // This section only checks the button when it's not being de-bounced.
  65.     check_button();
  66.   }
  67.   else { // This is what happens if the button is disabled because it's in the debouncing time.
  68.     current_buttonCheckTime = millis();
  69.     if (current_buttonCheckTime > buttonPress_startTime) {
  70.       if (current_buttonCheckTime > (buttonPress_startTime + button_debounce_time_milliseconds)) {
  71.         button_enabled = true;
  72.       }
  73.     }
  74.     else {
  75.       buttonPress_startTime = millis(); // millis() rollover condition
  76.     }
  77.   }
  78.  
  79.   if (current_gate_state == true) {
  80.     if (previous_gate_state == true) {
  81.       check_left_relay();
  82.       check_right_relay();
  83.       check_flasher();
  84.     }
  85.   }
  86.  
  87.   if (current_gate_state == false) {
  88.     if (previous_gate_state == true) {
  89.       // When the button is released, the current-current_gate_state will get set to false, but the previous_gate_state will still be set to true.
  90.       // This means to shut the gate off:
  91.       shut_gate_off();
  92.     }
  93.   }
  94.  
  95. } // end of main loop
  96.  
  97. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  98.  
  99. void check_button() {
  100.  
  101.   current_buttonCheckTime = millis();
  102.   if (current_buttonCheckTime > previous_buttonCheckTime) {
  103.     if (current_buttonCheckTime > (previous_buttonCheckTime + buttonCheck_interval_milliseconds)) {
  104.       // You can't put the code to run the relays & flasher here in the button press event
  105.       // because the button is only checked at the interval in [buttonCheck_interval].
  106.       // And it is also skipped while the button is de-bouncing.
  107.       if (digitalRead(buttonPin) == LOW) { // Since the button pin is set to input_pullup, you check for LOW instead of HIGH.
  108.         Serial.println("button pressed");
  109.         current_gate_state = true;
  110.         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.
  111.           Serial.println("gate is opened");
  112.           buttonPress_startTime = millis(); // This is saving the button-press time in a second variable to use for other functions.
  113.           button_enabled = false; // This turns off the button check long enough to avoid any bouncing.
  114.           // Turn on the flasher:
  115.           flashChange_previous_time = millis();
  116.           flash_state = 1;
  117.           digitalWrite(light_gate_relay, flash_state);
  118.           Serial.println("flasher blinking: on"); // Since this turns the flasher light on, that flasher state change is printed here.
  119.           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.
  120.         }
  121.       }
  122.       else {
  123.         if (current_gate_state == true) {
  124.           if (previous_gate_state == true) {
  125.             Serial.println("button released"); // This tells when the button is released, but only once when it goes from pressed to released.
  126.           }
  127.         }
  128.         current_gate_state = false;
  129.       }
  130.       previous_buttonCheckTime = millis(); // Re-set the previous check time to now, since the button-press check was run.
  131.     }
  132.   }
  133.   else {
  134.     previous_buttonCheckTime = millis(); // millis() rollover condition
  135.   }
  136.  
  137. }
  138.  
  139. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  140.  
  141. void check_left_relay() {
  142.   if (left_relay_on == false) {
  143.     unsigned long relay_currentTime = millis();
  144.     // The left relay turns on right away, but I wrote it to allow a delay anyway,,,
  145.     if (relay_currentTime > buttonPress_startTime) {
  146.       if (relay_currentTime > (buttonPress_startTime + left_relay_delay_milliseconds)) {
  147.         left_relay_on = true;
  148.         digitalWrite(left_gate_relay, HIGH);
  149.         Serial.println("left relay = ON");
  150.       }
  151.     }
  152.     else {
  153.       buttonPress_startTime = millis(); // millis() rollover condition
  154.     }
  155.   }
  156. }
  157.  
  158. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  159.  
  160. void check_right_relay() {
  161.   if (right_relay_on == false) {
  162.     unsigned long relay_currentTime = millis();
  163.     // This turns on the right relay.
  164.     if (relay_currentTime > buttonPress_startTime) {
  165.       if (relay_currentTime > (buttonPress_startTime + right_relay_delay_milliseconds)) {
  166.         right_relay_on = true;
  167.         digitalWrite(right_gate_relay, HIGH);
  168.         Serial.println("right relay = ON");
  169.       }
  170.     }
  171.     else {
  172.       buttonPress_startTime = millis(); // millis() rollover condition
  173.     }
  174.   }
  175. }
  176.  
  177. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  178.  
  179. void check_flasher() {
  180.   // This part blinks the light on and off.
  181.   flashChange_current_time = millis();
  182.   if (flashChange_current_time > flashChange_previous_time) {
  183.     if (flashChange_current_time > (flashChange_previous_time + flash_delay_milliseconds)) {
  184.       if (flash_state == 1) {
  185.         flash_state = 0;
  186.         Serial.println("flasher blinking: off");
  187.       }
  188.       else {
  189.         flash_state = 1;
  190.         Serial.println("flasher blinking: on");
  191.       }
  192.       digitalWrite(light_gate_relay, flash_state);
  193.       flashChange_previous_time = millis(); // re-set the 'previous' flash change time to the current time.
  194.     }
  195.   }
  196.   else {
  197.     flashChange_previous_time = millis(); // millis() rollover condition
  198.   }
  199. }
  200.  
  201. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  202.  
  203. void shut_gate_off() {
  204.   current_gate_state = false;
  205.   previous_gate_state = false;
  206.   left_relay_on = false; // these two lines were added in v.2
  207.   right_relay_on = false; // these two lines were added in v.2
  208.   digitalWrite(left_gate_relay, LOW);
  209.   digitalWrite(right_gate_relay, LOW);
  210.   digitalWrite(light_gate_relay, LOW);
  211.   Serial.println("left relay off");
  212.   Serial.println("right relay off");
  213.   Serial.println("flasher relay off");
  214. }
  215.  
  216. // ~~~~~~~ [end] ~~~~~~~~~~
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement