How to check for a particular condition for a specific amount of time?

I have a situation in which I have one ir sensor, if it detects it has to perform certain action and again if it detects it has to do other set of action, I am not sure how to perform the second task, Posting the code which I have tried.

void setup()
{
pinMode(2, INPUT);// IR SENSOR
pinMode(3, OUTPUT);
pinMode((4, OUTPUT);
}

void loop()
{
int a = digitalRead(2); // STORING IR SENSOR VALUE IN VARIABLE
if( a == 1) // CHECKING IF IT IS HIGH
{
digitalWrite(3, HIGH);// SOME ACTION
int  b = digitalRead(2); //storing the current value of ir sensor in another variable
if (b ==1)  //  if b is high, and now I want this part to keeping checking until the b is "1", how to //program it, 
{
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
delay(5000);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
}
}
}

if it detects it has to perform certain action

If "it".. What is it?

"it" has to... What is this it?

Wishy-washy statements like "perform some action" don't cut it. Something has to do something explicit. What, exactly, needs to be done? By what? When?

and again if it detects it has to do other set of action

After what interval? I really don't understand this part, or what has to do what.

Posting the code which I have tried.

Which doesn't even compile.

int a = digitalRead(2); // STORING IR SENSOR VALUE IN VARIABLE
if( a == 1) // CHECKING IF IT IS HIGH
{
digitalWrite(3, HIGH);// SOME ACTION
int  b = digitalRead(2); //storing the current value of ir sensor in another variable

What are the odds that reading the same sensor a few nanoseconds apart is going to result in different values?

if (b ==1)  //  if b is high, and now I want this part to keeping checking until the b is "1",

If b is one, keep checking until b is one. I really think that you need to understand, or state, your requirements better.

Perhaps explain them in terms of real-world events.

Using single ir sensor I would have to perform two actions at two different timings, To be more clear,

At first,

  1. check ir sensor, if it is high
  2. switch motor1 ON
  3. delay(5000);
  4. switch motor1 OFF
    5.again check for same ir sensor, if it is high
  5. switch motor2 ON.
    7.delay(5000);
  6. switch motor2 OFF.

// coming back to 5th point, checking ir sensor, if the condition is low, it has to keep checking the state until the sensor turns high. I am not sure how to code it. and I have modified code a little bit

void setup()
{
pinMode(2, INPUT);// IR SENSOR
pinMode(3, OUTPUT);
pinMode((4, OUTPUT);
}

void loop()
{
int a = digitalRead(2); // STORING IR SENSOR VALUE IN VARIABLE
if( a == 1) // CHECKING IF IT IS HIGH
{
digitalWrite(3, HIGH);
delay(5000);
digitalWrite(3, LOW);
int  b = digitalRead(2); //storing the current value of ir sensor in another variable
if (b ==1)  
{
digitalWrite(4, HIGH);
delay(5000);
digitalWrite(4, LOW);
}
}
}

Do you mean something like:

  1. check ir sensor until it is HIGH

In other words:

  1. Repeatedly check ir sensor for as long as it is LOW.
 while (digitalRead(2) == LOW) { /* do nothing */ }

Yeah you are right, Thanks for that, sorry if I have confused you in any way..