How to program an IR sensor

Hello all! This is my first post here in the group. I am working on a project to get an IR sensor to do the following:

When a hand is waved over the IR sensor, the motor moves in one direction (simulating opening a door). There is a 2 second pause, and then the motor moves in the opposite direction (simulating closing the door).
The motor stops until another hand is waved over the IR sensor again.

I have the circuit built but the proper programming in alluding me. I am not very experienced with the Arduino yet and only been using it for about six weeks. I am going to attach my coding and was hoping I could get some tips on how to get it to work right.

Thanks.

Lab4_G00138460.ino (996 Bytes)

Here are some tips: Post the circuit, provide details about the IR sensor, post code in code tags, read the locked posts at the top of the forum.

...and "eluding"

The code you attached was small enough to post.

//Hbridge Motor Control
const int EN = 9; //half bridge 1 enabled
const int MC1 = 3; //motor control 1
const int MC2 = 2; //motor control 2
const int IR = 0; //IR on analog pin 0

int val = 0; //reading from IR sensor
int velocity = 0;

void setup()
{
  pinMode(EN, OUTPUT);
  pinMode(MC1, OUTPUT);
  pinMode(MC2, OUTPUT);
  brake();
}

void loop()
{
  val = analogRead(IR);

  //go forward
  if (val > 200)
  {
    velocity = map(val, 563, 1023, 0, 255);
    forward(velocity);
    delay (5000);
  }
  else if (val < 199)
  {
    velocity = map(val, 461, 0, 0, 255);
    reverse(velocity);
  }
  else
  {
   brake();
  }
}
void forward (int rate)
{
 digitalWrite(EN, LOW);
 digitalWrite(MC1, HIGH);
 digitalWrite(MC2, LOW);
 analogWrite(EN, rate);
}
void reverse (int rate)
{
 digitalWrite(EN, LOW);
 digitalWrite(MC1, LOW);
 digitalWrite(MC2, HIGH);
 analogWrite(EN, rate);
}
void brake ()
{
 digitalWrite(EN, LOW);
 digitalWrite(MC1, LOW);
 digitalWrite(MC2, LOW);
 analogWrite(EN, HIGH);
}

It does something. You need to tell us what it does, and how that differs from what you want.

I spins the motor but it does not run then delay for two seconds run reverses and then stops until i wave my hand over the IR sensor.

There are many things wrong with this short program, such as "if" statements that don't match the map function calls, mixing analog and digital writes, analog writing a value of HIGH, and not handling the case when "val" is 200.

In addition, the OP replied but I am confused and can make little sense of the reply. Maybe the reply has been badly translated into English?