IR proximity sensor: how to keep the output signal high after 1000 ms?

Hi all, I am working on a project for my study, but have a small issue now. I made a simple proximity sensor, by using an IR LED and an IR Receiver (photo-diode). These circuits are relatively easy: turn on a LED if an obstacle is in front of the IR Receiver.

However, what I want to add next is the following:

  • If an obstacle (a hand) is in front of the IR Receiver for 1000 ms or more (and after that time that obstacle will move away from the IR Receiver (not in front of it anymore)), the output of a LED remains HIGH. In other words after 1000 ms: LED must stay HIGH, IR Receiver is HIGH until the obstacle is not in front of the IR Receiver anymore, than IR receiver is LOW.
  • If again an obstacle (a hand) is in front of the IR Receiver for 1000 ms or more, IR Receiver is HIGH, (and after that time that obstacle is not in front of the IR Receiver anymore), the output of a LED turns back to its initial value, LOW (LED is off).

Can someone explain me how to tackle this and with which commands should I do this?

If I understood your requirements correctly, the following algorithm will do what you want if you execute it repeatedly:

Read the proximity sensor and decide whether there is an obstacle.

If there is no obstacle, record the time and turn the LED off.

If there is an obstacle, and the time recorded above is more than 1000ms ago, turn the LED on.

Thanks for the reply, but I would like to ADD that command to the circuit I already have. So the normal circuit that a LED will turn on if the output is HIGH ‘and’ that if the output of the IR Receiver stays HIGH for more than 1000 ms than the LED should stay on.

And I understand the algorithm, but I don’t know how to write the code?

We can't help unless you post the code you have.
In the Arduino IDE click Edit then click Copy for Forum and Paste in reply.

BartvD:
Thanks for the reply, but I would like to ADD that command to the circuit I already have.

I'm not sure what you mean by 'command' and 'circuit'. Do you have an existing sketch that you want to modify?

With ‘command’ I mean a function, like turn a led on or off, trigger a buzzer, start a servo and so fort.

I attached my Arduino sketch for your convenience. This sketch is based on the sketch that I used from: http://www.instructables.com/id/Simple-IR-proximity-sensor-with-Arduino/

Now I would like to have another function and that is what I try to explain. I want that if the IRsensor is HIGH for more than 1000 ms the LED will stay on, even if there is no obstacle. If after, let’s say a minute, an obstacle is in front of the IRsensor for more than 1000 ms again, the LED will turn off.
If I am right, this is basically the same with a normal press button. Press the button for more than a second, LED will turn on. Press the button for more than a second again, LED will turn off. In stead of having a press button, which value is HIGH, I use a IRsensor.

Does this clarify what I want?

int IRled = 4; // IRled on digital pin 4
int LED = 11; // LED on digital pin 11
int times = 4; // Number of readings, before Arduino sends the output signal. Serves as a filter for measurement errors.
int IRsensor = A0; // IRsensor on analog pin A0
int thres = 3; // Threshold

void setup() {
Serial.begin(9600); // initializing Serial monitor

pinMode(IRled,OUTPUT); // IRled is OUTPUT
pinMode(LED,OUTPUT); // LED is OUTPUT
digitalWrite(LED,LOW); // Setup LED is off

}

void loop() {

int distance = 0; // integer that will tell if there is an obstacle or not

digitalWrite(IRled,HIGH); // turning the IRLED on to read the IR coming from the ambient
delay(2); // minimum delay necessary to read values

int sensorValuehigh = analogRead(IRsensor); // storing IR coming from the ambient
digitalWrite(IRled,LOW); // turning the IR LED off to read the IR coming from the obstacle
delay(2); // minimum delay necessary to read values

int sensorValuelow = analogRead(IRsensor); // storing IR coming from the obstacle
distance += sensorValuelow-sensorValuehigh; // calculating changes in IR values and storing it
Serial.println(sensorValuehigh);
Serial.println(sensorValuelow);

Serial.println(distance/times); // print the final value

if (distance/times > thres) { // if there is an obstacle: final value > thres
digitalWrite(LED,HIGH); // turn LED on
} else {
digitalWrite(LED,LOW); // if there is no obstacle: final value < thres
}

}

Thanks for the link, but I can't figure it out yet. I do not really understand the sketch in that topic to be honest.
Isn't there just an easy way to do it?