Railroader:
Okej. Use PULLUP only for that input. What values do You use in the voltage divider? Something near 10 kOhm I would use.
You use a relay board so free wheel, or kick back, diodes are probably provided by the board.
When dig32 is found HIGH You switch both relays ina very short time. My experience is that relays react quite a lot faster when they are turned on then when they turned off. Without seing Your wiring I guess that You migh get a short moment off a short on the 12 Volt. Then the pwr supply might cut off and wait before testing fore the short to be gone. I added a delay(100) to Your code.
What do You think?
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);//relay2 is already HIGH
digitalWrite(relay3, HIGH);
delay(100);// allow relay1 to release before relay2 is activated.
digitalWrite(relay2, LOW);
do
{
// digitalWrite(relay1, HIGH);// relay1 is already set HIGH
// digitalWrite(relay2, LOW);// done above
} while (digitalRead(32) == HIGH);
if (digitalRead(32) == LOW)
{
// digitalWrite(relay1, HIGH);//relay1 already is HIGH
digitalWrite(relay2, HIGH);//DOES THIS WORK? realy2 is Active a very short time when dR32 goes LOW.
}
}
}
jremington:
Please post a link to the exact proximity sensor you are using, or to the data sheet.
Most such sensors have several options for the output. Are you certain that yours is PNP, NO?
The diagram posted in reply #2 does not state the resistor values used on the output. Please add those values to the diagram and repost.
Also, add comments to your code, so that it is clear what is intended. For example, is this line testing for presence or absence of a conductive object within the sensor range?
if (digitalRead(32) == HIGH)
I attached a picture of the inductive sensor I have.
The relay board is an 8 channel arduino relay board and I guess it has all diodes it needs.
The relays are active LOW.
I actually utilize two relays in order to be able to run the motor forward and reverse.
I attached a wiring schematic of the relays connections, it does not include the switch, look at the relays contacts only.
I tried what Railroaded suggested and I think there is a bit of improvement in stopping the door but I think there is more delay when it reverses.
The input pin is HIGH when there is a metal object in front of the sensor.
Now I added comments to the code.
int relay1 = 22; // Motor Relay 1
int relay2 = 23; // Motor Relay 2
int relay3 = 52; // Relay for the lamp indicator
void setup()
{
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
pinMode(32, INPUT); // Inductive Sensor Input
//Serial.begin(9600);
}
void loop()
{
delay(5000);
digitalWrite(relay1, LOW); // Relay 1 is ON and Relay 2 is OFF therefore the motor runs forward(door opens)
digitalWrite(relay2, HIGH);
digitalWrite(relay3, LOW); // The lamp indicator is ON while the door is opening
if(digitalRead(32) == HIGH) // When the door(steel) reaches the stationary inductive sensor, there is a metal object in front the sensor therefore it is activated
{
digitalWrite(relay1, HIGH); // The motor and the lamp indicator should first stop
digitalWrite(relay2, HIGH);
digitalWrite(relay3, HIGH);
do
{
digitalWrite(relay1, HIGH); // Relay 1 is OFF and Relay 2 is ON therefore the motor runs in reverse(door closes) until the sensor is deactivated
digitalWrite(relay2, LOW);
}while(digitalRead(32) == HIGH);
if(digitalRead(32)== LOW) // When the sensor is deactivated the motor should stop
{
digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);
}
}
}