Below is the coding for my Electric Gate which controls both side of the gate. I will post the schematic diagram later. I am sharing with anyone who is interested on this. Enjoy!
Recently, I had just updated the code because I had found some minor bugs on my gate. This will actually speed up the signal responds time.
Code:
/*Simple Gate Controller*/ /* Author: Englebert Lai Email: englebert.lai@gmail.com Date: 6th June 2011 Description: This is just a simple PWM driven Electric gate System. A replacement board for the existing fried board due to lightning strikes. It works the same and performs better than the original system. Next phase for this code will be using some electronics current sensor for detecting the load of the motors so it will know when to cut off the power of the gate instead of using the delay as the timing to open or close the gate. */ #define REMOTE_BUTTON_1 7 #define REMOTE_BUTTON_2 8 #define GATE_A_DIRECTION 9 #define GATE_B_DIRECTION 10 #define PWM_A 12 #define PWM_B 11 #define RUN 1 #define STOP 0 // Variables int remote_button_1_state = LOW; // Getting the reading from the remote controller int remote_button_2_state = LOW; // Getting the reading from the remote controller int gate_1_direction_state = LOW; // LOW = close, HIGH = open int gate_2_direction_state = HIGH; // LOW = close, HIGH = open int gate_function = 0; // 0 = do nothing (idle) // 1 = button one pressed // 2 = button two pressed // 3 = button one and two pressed unsigned long lastTick = 0; unsigned long lastDebounce = 0; void detect_remote_command() { unsigned long lastTick = 0; // Delay for 2 seconds before starting the detection process delay(500); lastTick = millis(); Serial.println("Detecting remote command while executing motor process."); while(millis() - lastTick < 17000) { // If detected some commands from the remote....emergency stop // Getting data from the remote controller remote_button_1_state = digitalRead(REMOTE_BUTTON_1); remote_button_2_state = digitalRead(REMOTE_BUTTON_2); if(remote_button_1_state == LOW || remote_button_2_state == LOW) { // Stop all the motors... motor(0, 0, STOP); motor(1, 0, STOP); Serial.println("EMERGENCY STOPPED the MOTORS as remote key signal received."); delay(500); return; } } // Stops all motor now motor(0, 0, STOP); motor(1, 0, STOP); Serial.println("Normal stopping on Motors"); } void motor(int id, int direction, int mode) { if(id == 0) { if(mode == RUN) { if(direction == 0) { digitalWrite(GATE_A_DIRECTION, LOW); } else { digitalWrite(GATE_A_DIRECTION, HIGH); } digitalWrite(PWM_A, HIGH); } else { digitalWrite(GATE_A_DIRECTION, direction); digitalWrite(PWM_A, LOW); } } if(id == 1) { if(mode == RUN) { if(direction == 0) { digitalWrite(GATE_B_DIRECTION, LOW); } else { digitalWrite(GATE_B_DIRECTION, HIGH); } digitalWrite(PWM_B, HIGH); } else { digitalWrite(GATE_B_DIRECTION, direction); digitalWrite(PWM_B, LOW); } } } void gate_command(int mode) { switch(mode) { case 1: // Open/Close gate A gate_1_direction_state = !gate_1_direction_state; Serial.print("GATE A DIRECTION (BUTTON 1): "); Serial.println(gate_1_direction_state); // Initiate open gate process motor(0, gate_1_direction_state, RUN); // Detect button press for emergency stop opening or closing gate detect_remote_command(); break; case 2: // Open/Close gate A & B gate_1_direction_state = !gate_1_direction_state; gate_2_direction_state = !gate_2_direction_state; Serial.println("GATE DIRECTION (BUTTON 2)"); Serial.print("GATE A DIRECTION: "); Serial.println(gate_1_direction_state); Serial.print("GATE B DIRECTION: "); Serial.println(gate_2_direction_state); // Initiate open gate process motor(0, gate_1_direction_state, RUN); motor(1, gate_2_direction_state, RUN); // Detect button press for emergency stop opening or closing gate detect_remote_command(); break; case 3: // Open/Close gate B gate_2_direction_state = !gate_2_direction_state; Serial.print("GATE B DIRECTION (BUTTON 1 and BUTTON 2): "); Serial.println(gate_2_direction_state); // Initiate open gate process motor(1, gate_2_direction_state, RUN); // Detect button press for emergency stop opening or closing gate detect_remote_command(); } } void setup() { Serial.begin(9600); Serial.println("Starting GateDuino Controller Version 1.0 alpha"); Serial.println("Initializing Parameters"); pinMode(REMOTE_BUTTON_1, INPUT); // set the pin as input digitalWrite(REMOTE_BUTTON_1, HIGH); // turn on the internal pullup resistor 20k pinMode(REMOTE_BUTTON_2, INPUT); // set the pin as input digitalWrite(REMOTE_BUTTON_2, HIGH); // turn on the internal pullup resistor 20k pinMode(GATE_A_DIRECTION, OUTPUT); // set the pin as output pinMode(GATE_B_DIRECTION, OUTPUT); // set the pin as output pinMode(PWM_A, OUTPUT); // set the pin as output pinMode(PWM_B, OUTPUT); // set the pin as output } void loop() { // Getting data from the remote controller remote_button_1_state = digitalRead(REMOTE_BUTTON_1); remote_button_2_state = digitalRead(REMOTE_BUTTON_2); // Debugging only will be removed Serial.print("BUTTON 1: "); Serial.println(remote_button_1_state); Serial.print("BUTTON 2: "); Serial.println(remote_button_2_state); // When getting the button 2 then will try to open or close the gate based on the previous gate state. if(remote_button_1_state == LOW && remote_button_2_state == LOW) { gate_command(3); } if(remote_button_1_state == HIGH && remote_button_2_state == LOW) { gate_command(2); } if(remote_button_1_state == LOW && remote_button_2_state == HIGH) { gate_command(1); } if(remote_button_1_state == HIGH && remote_button_2_state == HIGH) { // Do nothing.... // gate_command(0); } delay(500); }