Blocking time of IR sensor?

I have interface a IR sensor with a arduino and i can observe the analog values giving by IR receiver . If the sensor is blocked then it gives a particular range of values and for unblocked it gives another range of values .
Now i need to determine for how long (time) it is giving blocking values. which procedure i can apply ? or which function may help?

Arduino time is given by the millis() or micros() functions.

In this Arduino Reference Page (bookmark, make a folder for it)

The millis() function - milliseconds since startup.

The value returned is type unsigned long, new to this person should use unsigned long variable to do time math with.

Time math should always use the subtraction of end-time minus start-time to get time-difference.

That will ALWAYS work up to the limit of the time variables used. With unsigned long variables, the limit (longest interval you can time) is 49.71~ days.

The usual way it is used:

unsigned long tStart, tWait = 1000UL; // the UL means make the constant 1000 unsigned long 

..... other code, setup() 

void loop()
{
...... some code

  if ( millis() - tStart >= tWait ) // if so then the wait is over, run the code inside this if()
  {
      // change something
      tStart += tWait; // this to set up the next wait then change, perhaps change back/blink the led
  }
...... some code
}

When I want a single timed event per trigger, not automatic over and over, I use the wait value to tell the code.

unsigned long tStart, tWait = 1000UL; // the UL means make the constant 1000 unsigned long 

..... other code, setup() 

void loop()
{
...... some code with somewhere it sets up a timed action

  if ( tWait > 0 ) // set tWait to 0 to turn the timer code off, > 0 to run it
  {
    if ( millis() - tStart >= tWait ) // if so then the wait is over, run the code inside this if()
    {
      // change something
      tWait = 0; // one-shot event, turn itself off
    }
  }
...... some code
}

Or just to know how long between things...

unsigned long tStart;

..... other code, setup() 

void loop()
{
.... possibly some code

  if ( digitalRead( myPin, LOW )
  {
    tStart = millis(); // save the time NOW to tStart
  }
  else 
  {
    Serial.print( F( "My pin was LOW for " )); // the extra F() keeps the text stored in flash, not RAM
    Serial.print( millis() - tStart );
    Serial.println( F( "milliseconds" ));
  } 

.... possibly some code
}

thanks for your help.I have already go throw this function but can't understand how to integrate this with my concept to get my reasult

Fazlay:
thanks for your help.I have already go throw this function but can't understand how to integrate this with my concept to get my reasult

"How long?" type measurements are the simplest type, using millis(). You store the value at time A, and the value at time B, and then subtract B-A.

Fazlay:
thanks for your help.I have already go throw this function but can't understand how to integrate this with my concept to get my reasult

I have interface a IR sensor with a arduino and i can observe the analog values giving by IR receiver . If the sensor is blocked then it gives a particular range of values and for unblocked it gives another range of values .
Now i need to determine for how long (time) it is giving blocking values. which procedure i can apply ? or which function may help?

unsigned long tStart, tDifference; // time variables, t is for time

So when the sensor changes from unblocked to blocked, you

tStart = millis();

And when it changes from blocked to unblocked, you

tDifference = millis() - tStart;

The duration is in tDifference.

the problem with this method is ..whenever the block state is activated the t_start start to saving that time bt every time the loop runs it updates it time. and just before the unblock state comes it saves that time and subtract it from mills() . so insted of getting real duration i am getting the last t_start and mills() diffrence.

Fazlay:
the problem with this method is ..

That isn't at all like the two similar methods that were suggested. You should only save t_start on a rising edge (which I must say is obvious). You have to change your program so that it does that. In reply #4, it is stated as, "when the sensor changes from unblocked to blocked".

Fazlay:
the problem with this method is ..whenever the block state is activated the t_start start to saving that time bt every time the loop runs it updates it time. and just before the unblock state comes it saves that time and subtract it from mills() . so insted of getting real duration i am getting the last t_start and mills() diffrence.

If you want to be a programmer, you have to spend time getting around limitations especially since many will be in your view rather than being "can't". I did some jobs "doing the impossible" where I saw ways that others had not considered.

What I wrote in my last post, I didn't waste words did I?

So when the sensor changes from unblocked to blocked, you

..............

And when it changes from blocked to unblocked, you

I have my own change detection technique that only uses 1 byte for both previous and current state.
It involves digital read returning 1 or 0.

byte pinStateHistory; // uses bit 0 for current read and bit 1 for previous read

..............

void loop()
{
pinStateHistory = pinStateHistory << 1; // move the bits so bit 0 (current) is now bit 1 (previous)
pinStateHistory = pinStateHistory & 3; // mask off bits 2 to 7
pinStateHistory += digitalRead( myPin ); // the new current read is now in bit 0, last read is in bit 1
..............
switch ( pinStateHistory )
{
case 0 : // current and previous reads are 0, LOW, no change
// no change, this case usually does nothing but here is where a something would go
break;
case 1 : // change from previous LOW to current HIGH
// change code goes here
break;
case 2 : // change from previous HIGH to current LOW
// change code goes here
break;
case 3 : // current and previous reads are 1, HIGH, no change
// no change, this case usually does nothing but here is where a something would go
break;
}
...................
}