The project is about a swing gate opener utilizing two motors.
I am trying to get the motors to work utilizing an arduino relay board and an inductive PNP (NO) proximity sensors.
What I am trying to achieve is that the door starts to open until it reaches and activates the proximity sensor. Then the door should stop and reverse until the sensor is deactivated.
The issue is that when I put a metal object to the proximity sensor the reaction is very slow and the door will hit and break the sensor. But when I remove the metal object the reaction is immediate and the motor stops.
The relay board is with 12V coils.
The proximity sensors are powered with 12V as well and a voltage divider is used to make the voltage 5V which goes to the arduino digital input.
I use two relays to make the motor go forward and reverse:
relay1
and
relay2
The third relay
relay3
switches a signal lamp on when the motor is activated.
The code does the following - the motor and the lamp indicator are switched on. If a high input from the proximity sensor is detected the motor and the indicator stop and the motor direction is reversed until the proximity sensor is deactivated and then the motor stops.
You can find the code attached.
int relay1 = 22;
int relay2 = 23;
int relay3 = 52;
void setup()
{
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
pinMode(32, INPUT);
//Serial.begin(9600);
}
void loop()
{
delay(5000);
digitalWrite(relay1, LOW);
digitalWrite(relay2, HIGH);
digitalWrite(relay3, LOW);
if(digitalRead(32) == HIGH)
{
digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);
digitalWrite(relay3, HIGH);
do
{
digitalWrite(relay1, HIGH);
digitalWrite(relay2, LOW);
}while(digitalRead(32) == HIGH);
if(digitalRead(32)== LOW)
{
digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);
}
}
}