Sharp IR sensor - Add delay between sensor readings

So i was hired to build a prototype, Basically it reads 2 sensors, one mounted high on a door jam and one mounted lower, if both are tripped when somebody walks through it, it wont be set off, but it only the bottom one is tripped it will sound a alarm, They are Sharp IR sensors.

I have working code, and its working, but due to the face that peoples legs move when they walk, it tends to set off the alarm when someone walks through it.

I want to add a delay between the two reads, my loop that reads the sensors is as follows
(_distance1 < alertdistance1 && distance2 > alertdistance1)
i want it to take the reading of distance one, if it is less then the alert distance i want a 1 to the thrown into memory (which would be flushed back to 0 after a second) and the same to be done with the distance 2 sensor. if after half a second after the bottom sensor is tripped the top one is not, then i want the alarm to sound..

ive tried a few different methods but everything ive tried makes the alarm go off immediately after turning the device on

here is my full code

const int sensor1Pin   = 0;
const int sensor2Pin   = 1;
const int alertPin      = 2;
const int mprime       = 16667;
const int bprime       = 15;
const int k            = 10;
const int maxdistance1  = 200;
const int alertdistance1 = 80;
const int alertdistance2 = 80;
int sensor1Value = 0;
int distance1 = 0;
int sensor2Value = 0;
int distance2 = 0;
void setup() {

  pinMode(alertPin, OUTPUT);





}
void loop() {
  

  

sensor1Value = readSensor1();
sensor2Value = readSensor2();
distance1 = (mprime / (sensor1Value + bprime)) - k;
distance2 = (mprime / (sensor2Value + bprime)) - k;
soundAlert(distance1);
}
int readSensor1() {
int _i;
int _sensor1Value = 0;
for(_i = 0; _i < 5; _i ++) {
_sensor1Value += analogRead(sensor1Pin);
delay(10);
}
_sensor1Value = _sensor1Value / 5;
return _sensor1Value;
}
int readSensor2() {
int _i;
int _sensor2Value = 0;
for(_i = 0; _i < 5; _i ++) {
_sensor2Value += analogRead(sensor2Pin);
delay(10);
}
_sensor2Value = _sensor2Value / 5;
return _sensor2Value;
}
void soundAlert(int _distance1) {
if (_distance1 < alertdistance1 && distance2 > alertdistance1)
digitalWrite(alertPin, HIGH);


}

aouate3:
ive tried a few different methods but everything ive tried makes the alarm go off immediately after turning the device on

Such as what? No sense having people suggest the things you already tried.

Also, do yourself a HUGE favor and select "Auto Format" from the Tools menu, in the IDE.

ive tried this

int sensorTripData1 = 0;
int sensorTripData2 = 0;
int tripdeadline = 0;

const int sensor1Pin   = 0;
const int sensor2Pin   = 1;
const int alertPin      = 6;
const int mprime       = 16667;
const int bprime       = 15;
const int k            = 10;
const int maxdistance1  = 200;
const int alertdistance1 = 80;
const int alertdistance2 = 80;
int sensor1Value = 0;
int distance1 = 0;
int sensor2Value = 0;
int distance2 = 0;
void setup() {

  pinMode(alertPin, OUTPUT);





}
void loop() {



  lcd.setCursor(0,0);
  lcd.print("System Ready");
  lcd.setCursor(0,1);
  lcd.print("Prototype V1");
  sensor1Value = readSensor1();
  sensor2Value = readSensor2();
  distance1 = (mprime / (sensor1Value + bprime)) - k;
  distance2 = (mprime / (sensor2Value + bprime)) - k;
  soundAlert(distance1);
}
int readSensor1() {
  int _i;
  int _sensor1Value = 0;
  for(_i = 0; _i < 5; _i ++) {
    _sensor1Value += analogRead(sensor1Pin);
    delay(10);
  }
  _sensor1Value = _sensor1Value / 5;
  return _sensor1Value;
}
int readSensor2() {
  int _i;
  int _sensor2Value = 0;
  for(_i = 0; _i < 5; _i ++) {
    _sensor2Value += analogRead(sensor2Pin);
    delay(10);
  }
  _sensor2Value = _sensor2Value / 5;
  return _sensor2Value;
}
void soundAlert(int _distance1) {
  if (_distance1 < alertdistance1)
    sensorTripData1 == 1;

  if (distance2 > alertdistance1)
    sensorTripData2 == 1;

  if (sensorTripData1 > tripdeadline && sensorTripData2 > tripdeadline)
    digitalWrite(alertPin, HIGH);




  {
    if (sensorTripData1 = 1)
      delay(200);
    sensorTripData1 == 0;
  }

  {
    if (sensorTripData2 = 1)
      delay(200);
    sensorTripData2 == 0;
  }

}

i think im probably messing up commiting the value to a int. its my first time using a int for anything other then defining values or pins

you should make 2dimensional measurements where the first dimension is the signal and the second dimension is time.
For the latter you can use millis() or micros()

in pseudo code you could do

  • if one sensor triggers set an alarmclock (e.g. 100 ms)
  • if the second sensor triggers within 100 ms reset the alarmclock
  • if the alarmclock reaches zero => ALARM!

The system will have at least three states: IDLE <===> PRE-ALARM ===> ALARM ===> IDLE

your turn :wink:

I do not think you have formulated precisly enough what the triggering condition is. When you do that the code is half done. Here is a suggestion (which I have not thought thoroughly through - that is your pleasure)

  1. starting psoition, both sensors are off.
  2. Bottom sensor on, top sensor off. Note the time (record value of millis())
  3. if top sensor turns on, reset the time (record value of millis())
  4. if bottom sensor turns off, reset the timer
  5. if the bottom sensor is on and millis() now is 2345 greater (or whatever the threshold is) than the last recorde time - beeep
    6 loop to 3 until both sensors are OFF and timer is much greater than millis, in which case weøre back at 1.

I'm fairly certain this code is absolutely not what you want.

  {

if (sensorTripData1 = 1)
      delay(200);
    sensorTripData1 == 0;
  }

{
    if (sensorTripData2 = 1)
      delay(200);
    sensorTripData2 == 0;
  }

Braces don't group commands together. In fact, if you remove the braces, nothing about that code changes. Did you mean to set the variables when the if-statement was true?

Speaking of if-statements and setting variables...

   if (sensorTripData2 = 1)

This is an assignment operator, not a comparison operator. This is where you use "==" to compare the variable "sensorTripData2" to the value of "1".

    sensorTripData2 == 0;

This is a comparison operator. It does NOT make any change to the value of sensorTripData2. In fact, it is a meaningless statement. This is where you use "=".

@MSquare, i would love to do it that way, But i have no idea how to.. The code im using is a modified example code for use with the Sharp IR sensors. and my programming skills are not good enough. Ive been building hardware for years, but programming is new to me.

This makes sense now. Would explain why my code make it act up

please update the title, as comapring is nothing

One line "translated". The rest follow the same pattern

// 2. Bottom sensor on, top sensor off. Note the time (record value of millis())
if ( Bottomvalue > tripvalue  && TopSensorvlaue < tripvalue ) TimerValue = millis() ;

I will take this code and do some experimentation. Thank you

after that line, edited to match my code

if (distance2 > tripdeadline && _distance1 < alertdistance ) TimerValue 0 millis() ;

Well, I made a typo, so it should be: (And I have not checked if "distance2" is the correct sensor or suchlike. I'm just helping on principles and algorithms)

if (distance2 > tripdeadline  && _distance1 < alertdistance ) TimerValue = millis() ;

To make amends, here is line 5

//if the bottom sensor is on and millis() now is 2345 greater (or whatever the threshold is) than the last recorded time - beeep
if ( millis()-TimerValue > 2345 && distance2 > tripdeadline ) AlertRoutine() ;

BTW, make sure TimerValue is declared as unsigned long.

Okay i have the code segments you provided put into my code, but before i can test it i need to figure out what the AlertRoutine(); part of it is for, From my understanding, and i could be wrong, it would call to the "digitalWrite(alertPin,HIGH);" code to set off the alarm. But how would i do that?

Msquare:
To make amends, here is line 5

//if the bottom sensor is on and millis() now is 2345 greater (or whatever the threshold is) than the last recorded time - beeep

if ( millis()-TimerValue > 2345 && distance2 > tripdeadline ) AlertRoutine() ;


BTW, make sure TimerValue is declared as `unsigned long`.

ehr? I can not make sense of your question the question. ByTheWay, now would be good time to post the edited code to see how far it is, in particular after all improvments suggested by James C4S.

well i edited it, but now it runs the alert routine, its not waiting until the sensor is tripped before starting the timer, also i need this to reset the timer after the distance2 sensor is tripped

const int sensor1Pin   = 0;
const int sensor2Pin   = 1;
const int alertPin      = 6;
const int sirenPin  = 10;
const int mprime       = 16667;
const int bprime       = 15;
const int k            = 10;
const int maxdistance1  = 200;
const int alertdistance1 = 80;
const int alertdistance2 = 80;

unsigned long TimerValue;

int sensor1Value = 0;
int distance1 = 0;
int sensor2Value = 0;
int distance2 = 0;
void setup() {
pinMode(alertPin, OUTPUT);
pinMode(sirenPin, OUTPUT);
}
void loop() {
sensor1Value = readSensor1();
sensor2Value = readSensor2();
distance1 = (mprime / (sensor1Value + bprime)) - k;
distance2 = (mprime / (sensor2Value + bprime)) - k;
soundAlert(distance1);
}
int readSensor1() {
int _i;
int _sensor1Value = 0;
for(_i = 0; _i < 5; _i ++) {
_sensor1Value += analogRead(sensor1Pin);

}
_sensor1Value = _sensor1Value / 5;
return _sensor1Value;
}
int readSensor2() {
int _i;
int _sensor2Value = 0;
for(_i = 0; _i < 5; _i ++) {
_sensor2Value += analogRead(sensor2Pin);

}
_sensor2Value = _sensor2Value / 5;
return _sensor2Value;
}
void soundAlert(int _distance1) {

  if (_distance1 < alertdistance1  )
  TimerValue = millis() ;
  if ( millis()-TimerValue > 2345 && distance2 > alertdistance1 )
{
digitalWrite(alertPin, HIGH);
delay(500);
digitalWrite(alertPin, LOW);
delay(500);
digitalWrite(alertPin, HIGH);
delay(500);
digitalWrite(alertPin, LOW);
delay(500);
digitalWrite(alertPin, HIGH);
delay(500);
digitalWrite(alertPin, LOW);
delay(500);
digitalWrite(alertPin, HIGH);
delay(500);
digitalWrite(alertPin, LOW);
delay(500);
digitalWrite(alertPin, HIGH);
delay(500);
digitalWrite(alertPin, LOW);
delay(500);
digitalWrite(alertPin, HIGH);
delay(500);
digitalWrite(alertPin, LOW);
delay(500);
digitalWrite(sirenPin, HIGH);
delay(200);
digitalWrite(sirenPin, LOW);
}
}

Is your tab key broken?

What is the difference between readSensor1() and readSensor2(), besides the pin number? Why not have one function where the pin number is an argument?

What is the purpose of reading the second sensor? You never do anything with the value.

its to detect the "height" of a passing human basically instead of sensing when the door opens, it senses the height of a passing object, using 2 Sharp IR sensors. These would be mounted on a door jamb at determined heights, so the first sensor is around 3 feet from the ground (or lower if wanted) and the top sensor would be around 5 feet. How this system works is by detecting which sensors are tripped by a passing object, taking that information, comparing the voltage values from each sensor, throwing that value into an algorithm, and throwing out a distance in centimeters, say for instance that you have a door that is 3 feet wide, that would be 91 centimeters. So you would program the unit for about 91, I would put 85 to 87 just because the sensors have some width to them. So each sensor knows that all objects should be that far away, but if an object comes in between that reading the reading drops, when that drops it sets off a buzzer.

if your curious, read my write-up: http://thedeconstruction.org/team/a-shopping-cart/

if you read my original post, you would understand why there are 2 sensors

also if i knew what i was doing i most likely wouldn't be posting