Automatic door with HC-SR04 and L298N Motor driver

Hello Community,
I'm totally new to Arduino and the programming. I've build a dog door which I want to open when the dog approaches the door (for ex. 2ft distance), stay open for 10sec and close after. I have 2 HC-SR04 one for the inside and one for the outside. I put together a code from different projects, but I ran into a PROBLEM:

even with one sensor (I left out all sensor 2 related parts) when I power it up, the motor turns left 2 sec and right 3-4sec, then it has a delay and it just runs half of the program and shuts of. after that when I wave my hand over the sensor it runs the program correctly every time. and with two sensors its the same and sometimes it never stops.

I want it to be activate from the inside, runs the opening time, pause time and close time. When the dogs show up from the outside the same.

Not necessary for the project I think, but i want to activate the whole project with a Smart WIFI wall outlet, for example when we leave the house and turn it off when we come home.

I would really appriciate some help with this, as I've did some research and I couldn't find a project which exactly fits my needs. And I hope that this is the right topic, because I think its a sensor problem, because the part for the motor does exactly what I want when I let the sensors out and use for ex. a button to trigger the action.

Thank you in advance =)

Here is the code:

#include <NewPing.h>

int in1 = 2; //Motor Controller
int in2 = 3; //Motor Controller
const int trigPin = 9; //Define the HC-SE04 triger on pin 9 on the arduino
const int echoPin = 10; //Define the HC-SE04 echo on pin 10 on the arduino
const int trigPin2 = 8; //Define the HC-SE04 echo on pin 8 on the arduino
const int echoPin2 = 7; //Define the HC-SE04 echo on pin 7 on the arduino

void setup()

{

Serial.begin (9600); //Start the serial monitor
pinMode(trigPin, OUTPUT); //set the trigpin to output
pinMode(trigPin2, OUTPUT); //set the trigpin to output
pinMode(echoPin, INPUT); //set the echopin to input
pinMode(echoPin2, INPUT); //set the echopin to input
pinMode(in1, OUTPUT); //MOTOR OUTPUT
pinMode(in2, OUTPUT); //MOTOR OUTPUT
digitalWrite(in1,LOW); //MOTOR OUTPUT
digitalWrite(in2,LOW); //MOTOR OUTPUT
digitalWrite(in1,LOW); //MOTOR OUTPUT
digitalWrite(in2,HIGH); //MOTOR OUTPUT
}

void motorStop(){
digitalWrite(in1,LOW); // Pause the Motor for X time
digitalWrite(in2,LOW);
delay(5000);

}

void loop()
{
long duration, distance, distance2;
digitalWrite(trigPin, HIGH);
digitalWrite(trigPin2, HIGH);
delayMicroseconds(20);
digitalWrite(trigPin, LOW);
digitalWrite(trigPin2, LOW);
duration = pulseIn(echoPin, HIGH);
duration = pulseIn(echoPin2, HIGH);
distance = (duration/2) / 29.1;
duration = duration + pulseIn(echoPin, HIGH);
duration = duration + pulseIn(echoPin2, HIGH);
distance2 = (duration/2) / 60;
if (distance < 30 || distance < 30)
{

{ digitalWrite(in1,HIGH);
digitalWrite(in2,LOW);
delay(6000);

motorStop();

digitalWrite(in1,LOW);
digitalWrite(in2,LOW);
digitalWrite(in1,LOW);
digitalWrite(in2,HIGH);
delay(8000);
digitalWrite(in1,LOW);
digitalWrite(in2,LOW);

}
}
else {
digitalWrite(in1,LOW);
digitalWrite(in2,LOW);
}
delay(500);
}

Did you ensure your sensors would not hear each others?

Paul

You've included NewPing library, but you haven't used it. Suggest using its median method for more stable results. Look at its examples.

What is the following code supposed to be doing? The duration from the first pulseIn of the first sensor (the one with the desired information) is never used. And why four pulseIns? (All this goes away if you use NewPing, btw.)

  duration = pulseIn(echoPin, HIGH);
  duration = pulseIn(echoPin2, HIGH);
  distance = (duration/2) / 29.1;
  duration = duration + pulseIn(echoPin, HIGH);
  duration = duration + pulseIn(echoPin2, HIGH);
  distance2 = (duration/2) / 60;

Why bother with two conditions when they're the same? Looks like you left out a "2".

if (distance < 30 || distance < 30)

Suggest you read up on how to not use delays. See the sticky "using millis() for timing" at the top of the programming section of this forum.

delay(5000);
delay(6000);
delay(8000);
delay(500);

Suggest also reading "Planning and implementing a program" in the "useful links" post in the programming section.

Paul_KD7HB:
Did you ensure your sensors would not hear each others?

Paul

is there any minimum distance between them? i want to have one on the outside and one on the inside, for test reasons I've just placed them on the table pointing to the ceiling about 4 inches apart.? is this maybe the never ending loop problem?

DaveEvans:
You've included NewPing library, but you haven't used it. Suggest using its median method for more stable results. Look at its examples.

What is the following code supposed to be doing? The duration from the first pulseIn of the first sensor (the one with the desired information) is never used. And why four pulseIns? (All this goes away if you use NewPing, btw.)

  duration = pulseIn(echoPin, HIGH);

duration = pulseIn(echoPin2, HIGH);
  distance = (duration/2) / 29.1;
  duration = duration + pulseIn(echoPin, HIGH);
  duration = duration + pulseIn(echoPin2, HIGH);
  distance2 = (duration/2) / 60;




Why bother with two conditions when they're the same? Looks like you left out a "2".



if (distance < 30 || distance < 30)





Suggest you read up on how to [u]not[/u] use delays. See the sticky "using millis() for timing" at the top of the programming section of this forum.



delay(5000);
delay(6000);
delay(8000);
delay(500);





Suggest also reading "Planning and implementing a program" in the "useful links" post in the programming section.

OMG, this is all a little bit to much as a newbie =(...I found this code online and bought that i just ad everything for the second sensor...

  duration = pulseIn(echoPin, HIGH);
  duration = pulseIn(echoPin2, HIGH);
  distance = (duration/2) / 29.1;
  duration = duration + pulseIn(echoPin, HIGH);
  duration = duration + pulseIn(echoPin2, HIGH);
  distance2 = (duration/2) / 60;

for this part i've used a part of an arduino door project, all the values are to adjust opening and closing time, because of some light mechanical reasons the door closes minimal slower then it opens just about a second so I have all the delays to adjust them because I'm not using any switches.... (not yet in this stage of not understanding the programming language so well....)

delay(5000);
delay(6000);
delay(8000);
delay(500);

I will definitely try to separate the sensors, pointing in two directions...it works with one sensor, and with two it worked but sometimes it gets into a kind of infinite loop...

and the main problem is when i power it up, there is a unwanted movement, 2 sec left 3 sec right... and this is sth I really dont understand.....

but thank you for the first tipps....

Here's another link you should study very carefully. It, along with the other tips, could help you resolve your "main problem" (by getting you to think through the desired behavior of your system and then code it in a systematic way). It also has an example of a good way to use millis()...

Don't be put off by the "finite state machine" jargon.

PS: there isn't a minimum distance between two HC-SR04s, but there is a minimum time between triggering them, otherwise the echo from one could confuse the other (if that is physically possible...as it would be for two side-by-side on a table pointing up).

.

Chris2201:
OMG, this is all a little bit to much as a newbie =(...I found this code online and bought that i just ad everything for the second sensor...

Code you find online can be a good starting point but make sure you understand every single line and know what it does, and why it's there. Look up commands you don't know.

Delay() calls are best to be avoided. A finite state machine is in order here, combined with millis() based timing. This way you can continue to monitor the area, and as long as you detect something (presumably your dog rather than the neighbour's cat) the door stays open.

You should also add some safety device to prevent the door from closing on your dog. E.g. as push bar with switch on the edge of the door, when it touches something the door opens again.

Ultrasound sensors at times give false signals (too high or too low). If you measure say 10 times a second, only open the door after 1 second (10 measurements) that are < 2 ft. That should take care of strays. Or add a longer delay, of say 5 seconds, so the door won't open when the dog just walks by, only when he sits in front of it for that period of time. I suppose it will be pretty easy to teach your dog how to open its door.

If you don't use newPing, add a timeout to pulseIn or it sits there for a full second if there's nothing in range and no echo comes back to the sensor. Waiting for 30 ms is more than enough (that's about 5 meter distance - 10 meter round trip).