Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Reddit 3-relay gate sketch
- March 12, 2018
- This activates the relays as long as the button is held down.
- Note that I have not tested this except in the serial monitor.
- */
- const int buttonPin = 2; // the number of the pushbutton pin
- const int right_gate_relay = 10; // the number of the relay pin
- const int left_gate_relay = 11; // the number of the relay pin
- const int light_gate_relay = 12; // the number of the relay pin
- int buttonCheck_interval_milliseconds = 1000; //--(500 suggested value) This is the milliseconds interval to check if the button has been pressed or not.
- 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).
- 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.
- unsigned long buttonPress_startTime = 0; // This is to save a copy of the time that the button is pressed.
- // 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.
- int left_relay_delay_milliseconds = 0; // This is the wait time in milliseconds to turn on the left gate relay.
- bool left_relay_on = false;
- int right_relay_delay_milliseconds = 4000; //--(1000 original value) This is the wait time in milliseconds to turn on the right gate relay.
- bool right_relay_on = false;
- int flash_delay_milliseconds = 3000; //--(500 original value) flash delay of the light relay
- int flash_state = 0; // This is to control the flash state
- unsigned long flashChange_previous_time = 0; // The flasher function is easiest if it can save its own two times.
- unsigned long flashChange_current_time = 0;
- bool current_gate_state = false; // This is a state flag that tells if the gate is open or not.
- // You need this state to persist outside of the button code because when the button is de-bouncing,
- // the button code that sets this variable to true won't be used (it gets skipped).
- 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.
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- // function prototypes:
- void check_button();
- void check_left_relay();
- void check_right_relay();
- void check_flasher();
- void shut_gate_off();
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- void setup() {
- Serial.begin(9600);
- pinMode(left_gate_relay, OUTPUT);
- pinMode(right_gate_relay, OUTPUT);
- pinMode(light_gate_relay, OUTPUT);
- pinMode(buttonPin, 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 { // This is what happens if the button is disabled because it's in the debouncing time.
- current_buttonCheckTime = millis();
- if (current_buttonCheckTime > buttonPress_startTime) {
- if (current_buttonCheckTime > (buttonPress_startTime + button_debounce_time_milliseconds)) {
- button_enabled = true;
- }
- }
- else {
- buttonPress_startTime = millis(); // millis() rollover condition
- }
- }
- if (current_gate_state == true) {
- if (previous_gate_state == true) {
- check_left_relay();
- check_right_relay();
- check_flasher();
- }
- }
- if (current_gate_state == false) {
- if (previous_gate_state == true) {
- // 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.
- // This means to shut the gate off:
- shut_gate_off();
- }
- }
- } // end of main loop
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- void check_button() {
- current_buttonCheckTime = millis();
- if (current_buttonCheckTime > previous_buttonCheckTime) {
- if (current_buttonCheckTime > (previous_buttonCheckTime + buttonCheck_interval_milliseconds)) {
- // You can't put the code to run the relays & flasher here in the button press event
- // because the button is only checked at the interval in [buttonCheck_interval].
- // And it is also skipped while the button is de-bouncing.
- if (digitalRead(buttonPin) == LOW) { // Since the button pin is set to input_pullup, you check for LOW instead of HIGH.
- Serial.println("button pressed");
- 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.
- Serial.println("gate is opened");
- buttonPress_startTime = millis(); // This is saving the button-press time in a second variable to use for other functions.
- button_enabled = false; // This turns off the button check long enough to avoid any bouncing.
- // Turn on the flasher:
- flashChange_previous_time = millis();
- flash_state = 1;
- digitalWrite(light_gate_relay, flash_state);
- Serial.println("flasher blinking: on"); // Since this turns the flasher light on, that flasher state change is printed here.
- 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.
- }
- }
- else {
- if (current_gate_state == true) {
- if (previous_gate_state == true) {
- Serial.println("button released"); // This tells when the button is released, but only once when it goes from pressed to released.
- }
- }
- 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_left_relay() {
- if (left_relay_on == false) {
- unsigned long relay_currentTime = millis();
- // The left relay turns on right away, but I wrote it to allow a delay anyway,,,
- if (relay_currentTime > buttonPress_startTime) {
- if (relay_currentTime > (buttonPress_startTime + left_relay_delay_milliseconds)) {
- left_relay_on = true;
- digitalWrite(left_gate_relay, HIGH);
- Serial.println("left relay = ON");
- }
- }
- else {
- buttonPress_startTime = millis(); // millis() rollover condition
- }
- }
- }
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- void check_right_relay() {
- if (right_relay_on == false) {
- unsigned long relay_currentTime = millis();
- // This turns on the right relay.
- if (relay_currentTime > buttonPress_startTime) {
- if (relay_currentTime > (buttonPress_startTime + right_relay_delay_milliseconds)) {
- right_relay_on = true;
- digitalWrite(right_gate_relay, HIGH);
- Serial.println("right relay = ON");
- }
- }
- else {
- buttonPress_startTime = millis(); // millis() rollover condition
- }
- }
- }
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- void check_flasher() {
- // This part blinks the light on and off.
- flashChange_current_time = millis();
- if (flashChange_current_time > flashChange_previous_time) {
- if (flashChange_current_time > (flashChange_previous_time + flash_delay_milliseconds)) {
- if (flash_state == 1) {
- flash_state = 0;
- Serial.println("flasher blinking: off");
- }
- else {
- flash_state = 1;
- Serial.println("flasher blinking: on");
- }
- digitalWrite(light_gate_relay, flash_state);
- flashChange_previous_time = millis(); // re-set the 'previous' flash change time to the current time.
- }
- }
- else {
- flashChange_previous_time = millis(); // millis() rollover condition
- }
- }
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- void shut_gate_off() {
- current_gate_state = false;
- previous_gate_state = false;
- left_relay_on = false; // these two lines were added in v.2
- right_relay_on = false; // these two lines were added in v.2
- digitalWrite(left_gate_relay, LOW);
- digitalWrite(right_gate_relay, LOW);
- digitalWrite(light_gate_relay, LOW);
- Serial.println("left relay off");
- Serial.println("right relay off");
- Serial.println("flasher relay off");
- }
- // ~~~~~~~ [end] ~~~~~~~~~~
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement