Need help with automated project

Hello ladies and gents, I could really use some insight and help. My project consists of using an arduino to control a doggy door. I am using two IR Long range sensors with a range of about 5 feet. The sensors are arranged on each side of the door to monitor each side. Now the pet has to be within range of the sensor to activate it. To prevent any "false triggers", The pet must stay in range for about five seconds. I also have two push buttons located at the top and bottom of the sliding door. They would be used to signal the motor to stop and either raise or lower according to which switch is pressed. Any help would be appreciated. Thank you in advance.

// Adapted from partial code written by cadrogui

#define sensorPin 0
#define sensorPin2 1
#define VOLTS_PER_UNIT .0049F // (.0049 for 10 bit A-D)

const int up = 2;
const int down = 3;

const int upswitch = 4;
const int downswitch = 5;

const int switchout1 = 8;
const int switchout2 = 9;

//IR SENSOR
float volts;
float inches;
float proxSens = 0;
int cm;

//IR Sensor2
float volts2;
float inches2;
float proxSens2 = 0;
int cm2;

void setup() {

Serial.begin(9600);

pinMode(sensorPin, INPUT);
pinMode(sensorPin2, INPUT);

pinMode(up, OUTPUT);
pinMode(down, OUTPUT);

pinMode(upswitch, INPUT);
pinMode(downswitch, INPUT);

pinMode(switchout1, OUTPUT);
pinMode(switchout2, OUTPUT);

}

void loop() {

//Stop Switch routine

digitalWrite(switchout1, HIGH);
digitalWrite(switchout2, HIGH);

//IR Sensor 1
proxSens = analogRead(sensorPin);
volts = (float)proxSens * VOLTS_PER_UNIT; // ("proxSens" is from analog read)
inches = 23.897 * pow(volts,-1.1907); //calc inches using "power" trend line from Excel
cm = 60.495 * pow(volts,-1.1904); // same in cm
if (volts < .2) inches = -1.0; // out of range

unsigned long present = 0;
unsigned long starttime = 0;

int distance = cm;
present = millis();

if (distance < 10 || 100 < distance){ // if it's outside the desired range
starttime=millis() ; //then the timer is constantly reset
}

if (present-starttime > 5000){

if (cm >= 10) {
digitalWrite(up, HIGH);
}
else {
digitalWrite(up, LOW);
}

if (cm <= 100) {
digitalWrite(up, HIGH);
}
else {
digitalWrite(up, LOW);
}

}

//IR Sensor 2
proxSens2 = analogRead(sensorPin2);
volts2 = (float)proxSens2 * VOLTS_PER_UNIT; // ("proxSens" is from analog read)
inches2 = 23.897 * pow(volts2,-1.1907); //calc inches using "power" trend line from Excel
cm2 = 60.495 * pow(volts2,-1.1904); // same in cm
if (volts < .2) inches2 = -1.0; // out of range

unsigned long present2 = 0;
unsigned long starttime2 = 0;

int distance2 = cm2;
present2 = millis();

if (distance2 < 10 || 100 < distance2){ // if it's outside the desired range
starttime2=millis() ; //then the timer is constantly reset
}

if (present2-starttime2 > 5000){

if (cm2 >= 10) {
digitalWrite(up, HIGH);
}

else {
digitalWrite(up, LOW);
}

if (cm2 <= 100) {
digitalWrite(up, HIGH);
}
else {
digitalWrite(up, LOW);
}

//STOP SWITCHES
if (upswitch == HIGH){
digitalWrite(up, LOW);
}
else {
digitalWrite(upswitch, LOW);
}

if (downswitch == HIGH){
digitalWrite(down, LOW);
}
else {
digitalWrite(downswitch, LOW);
}

}
Serial.print(cm);
Serial.print(' ');

}

Without time to go into details, but you realize that this system is also good for raccoons, skunks and other more or less pleasant critters, right?

Before we read your code, and decipher it, perhaps you could explain what it is you need help with. What does the code do that you don't want ti to do? What does the code not do that you want it to do?

Question: What limits exist for this to keep from being a "doggy guillotine"?

:slight_smile:

Well guys, this is just a prototype final for a class. @ crosh, well i was thinking of using a resistive strip at the bottom of the doggy door that would act as a safety stop in case of small children and pet indecisiveness. @TheGiops, yeah realize that it wont deter any other critters, but I'm not worried about that right now, as I just stated, its a simple prototype. If I really had time to develop something better, I would use RFID. @ PaulS, essentially I'm just thinking of using a Boolean and function that uses the two push buttons as the inputs. If both the inputs are pressed, the door knows to go up, but if only the top button is pressed, the door knows to go down. That is what I need help with. I need help with making the door "mode" respond to the Boolean condition.

Some facts to note. The loop function is executed in an endless loop. The speed of the Arduino is such that the instructions are executes 16,000,000 times per second. Depending on how many instructions are executed on each pass through loop, loop may end up being called millions of times per second.

Local variables (those declared in a function or block) go out of scope at the end of the function. The time variables present and starttime go out of scope at the end of loop, and will be reset to 0 at the start of each pass through loop.

Your dog will die of old age outside before the door ever gets opened for him.

This code

proxSens = analogRead(sensorPin);
    volts = (float)proxSens * VOLTS_PER_UNIT; // ("proxSens" is from analog read)
inches = 23.897 * pow(volts,-1.1907); //calc inches using "power" trend line from Excel
cm = 60.495 * pow(volts,-1.1904);     // same in cm
if (volts < .2) inches = -1.0;        // out of range

and this code

proxSens2 = analogRead(sensorPin2);
    volts2 = (float)proxSens2 * VOLTS_PER_UNIT; // ("proxSens" is from analog read)
inches2 = 23.897 * pow(volts2,-1.1907); //calc inches using "power" trend line from Excel
cm2 = 60.495 * pow(volts2,-1.1904);     // same in cm
if (volts < .2) inches2 = -1.0;        // out of range

differ only in which pin the code reads from and in where the result is stored.

There is no reason to have procSensN, voltsN, inchesN, and cmN as global variables, when the only value you care about is cmN.

Since you only care about the distance in centimeters, create a function that takes a pin number as an argument and returns the distance in centimeters. Change all the global variables to local (to that function) variables.

Then, look at that function. First, is there any reason to compute inches, since you never use it. The pow function is slow. Computing a value that you never use using a slow function, thousands to millions of times a second doesn't make sense.

Second, why is the constant in pow call in that equation different for inches and centimeters. Mapping voltage to distance does not depend on the units that the distance is going to be expressed in.

Third, if you did need both inches and centimeters, there is a fixed relationship between inches and centimeters. Multiply the distance in inches by a constant gives the distance in centimeters.

You've now got some work to do before we can meaningfully care on critiquing you program.

ok, thank you, I'll try your advice.