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);
}