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