/*
Program reqs:
When signal to operate is received detmine if gate is opened or closed.
Can receive signal from keyPin or phone.
If open close the gate.
Operate the PWM motor at 50% until the close hall effect sensor gets signal.
If closed open the gate.
Operate the PWM motor at 50% until the open hall effect sensor gets signal.
If neither open or close close the gate.
Check the close proximity sensor while closing, if object detected go back to open position.
Check the open proximity sensor while opening, if object detected go back to close position.
Automatically close the gate after open for a set ammount of time
*/
// Define PINs for gate control, proximity sensors, and motor control
const int gateOpenPin = 10; // GPIO2 for gate open
const int gateClosePin = 3; // GPIO3 for gate close
const int openProximityPin = 19; // GPIO4 for open proximity sensor
const int closeProximityPin = 2; // GPIO5 for close proximity sensor
const byte motorControlPinR = 7; // GPIO6 for right motor control (PWM)
const byte motorControlPinL = 6; // GPIO7 for left motor control (PWM)
const int keyPadPin = 9; // GPIO8 for keypad signal
// Define PWM parameters
const int motorSpeed = 128; // 50% PWM speed (0-255)
// Define the gate operation duration in milliseconds
const unsigned long gateOperationDuration = 45000; // 45 seconds
bool gateOpen = false;
bool objectDetected = false;
unsigned long gateOperationStartTime = 0;
void setup() {
// Initialize serial communication
Serial.begin(115200);
// Initialize pins
pinMode(gateOpenPin, INPUT_PULLUP);
pinMode(gateClosePin, INPUT_PULLUP);
pinMode(openProximityPin, INPUT_PULLUP);
pinMode(closeProximityPin, INPUT_PULLUP);
pinMode(keyPadPin, INPUT_PULLUP);
ledcAttachPin(motorControlPinL, 0);
ledcAttachPin(motorControlPinR, 1);
ledcSetup(0,4000,8);
ledcSetup(1,4000,8);
}
void loop() {
// Check for keypad input{
Serial.print("Open pin state: ");
Serial.println(digitalRead(gateOpenPin));
Serial.print("Close pin state: ");
Serial.println(digitalRead(gateClosePin));
Serial.print("Keypad pin state: ");
Serial.println(digitalRead(keyPadPin));
Serial.print("Open proximity pin state: ");
Serial.println(digitalRead(openProximityPin));
Serial.print("Close proximity pin state: ");
Serial.println(digitalRead(closeProximityPin));
getSignal();
delay(500);
}
void getSignal() {
if (digitalRead(keyPadPin) == 0) {
if (digitalRead(gateClosePin)== LOW) {
Serial.println(digitalRead(keyPadPin));
openGate();
} else {
Serial.println(digitalRead(keyPadPin));
closeGate();
}
}
}
void openGate() {
gateOpen = true;
int count = 0;
gateOperationStartTime = millis();
while (digitalRead(gateOpenPin)== LOW){
ledcWrite(0, motorSpeed);
while (digitalRead(openProximityPin) == LOW) {
Serial.println("Object");
ledcWrite(0, 255);
delay(1000);
}
Serial.println("Opening");
count++;
Serial.println(count);
}
Serial.println("Opening the gate...");
ledcWrite(motorControlPinL, 255);
getSignal();
}
void closeGate() {
gateOpen = false;
int count = 0;
gateOperationStartTime = millis();
while (digitalRead(gateClosePin)== LOW){
ledcWrite(1, motorSpeed);
while (digitalRead(closeProximityPin) == LOW) {
// Object detected while opening, stop and reverse the gate
Serial.println("Object");
ledcWrite(1, 255);
delay(1000);
}
Serial.println("Closing");
count++;
Serial.println(count);
}
Serial.println("Closing the gate...");
ledcWrite(motorControlPinR, 255);
getSignal();
}
Hall effect sensor open position
Hall effect sensor close position