I want to stop my dc water pump after 3 sec

Sir I am new to Arduino
I want to stop my dc water pump after 3 sec the following is my code please suggest a better solution
thank you

#define IRsensor 2
#define DCwater_pump 13

void setup() {
// put your setup code here, to run once:
pinMode(IRsensor, INPUT);
pinMode(DCwater_pump, OUTPUT);
Serial.begin(9600);
}
int readPin = 0;
void loop()
{

// put your main code here, to run repeatedly:
readPin = digitalRead(IRsensor);
if (readPin == HIGH)

{
digitalWrite(DCwater_pump, HIGH);
Serial.println("DC Pump is OFF now!!");
delayMicroseconds(2);

}
else
{
digitalWrite(DCwater_pump, LOW);
Serial.println("DC Pump is ON Now !!");
}

}

Not sure exactly what you want to do. Start the pump with a LOW and 3 seconds later turn the pump off regardless of the state of the signal or run the pump as long as the signal is LOW and turn the pump off 3 seconds after the signal goes HIGH.

Here are some tutorials that cover timing with millis() that may help.
Several things at a time.
Beginner's guide to millis().
Blink without delay().

Inserting code in the forum as a code-section

I want to write some basic information. You should follow some rules to get better help.

If you want to post code this is the most comfortable way to do it:

  • make the Arduino-IDE the active window

  • point with the mouse-arrow somewhere into the code

  • do a rightclick on the mouse

  • and choose "copy for forum"
    This will copy the whole code into the clipboard
    This will add the code-tags right on top of the first line of code and right below the last line of code

  • make your browser the active window and paste clipboard

As a code-Section it shows up as a smaller window which is browsable on its own
and has a select-function which makes it possible to select the entire code by one mouseclick for other users.

To your code delayMicroseconds(2); does what its name says: delay for 0,000002 seconds.
So as a human beeing you won't recognize such a short "delay".

baldengineer.com has a very good tutorial about timing with function millis() too .

There is one paragraph that nails down the difference between function delay() and millis() down to the point:

The millis() function is one of the most powerful functions of the Arduino library. This function returns the number of milliseconds the current sketch has been running since the last reset. At first, you might be thinking, well that’s not every useful! But consider how you tell time during the day. Effectively, you look at how many minutes have elapsed since midnight. That’s the idea behind millis()!

Instead of “waiting a certain amount of time” like you do with delay(), you can use millis() to ask “how much time has passed”?

and this is the essence and the fundamental difference between blocking timing with delay() ;
and non-blocking timing with function millis();

baldengineers: millis() Tutorial: Arduino Multitasking

As a general introduction to programming Arduinos this website is very good
Arduino Programming Course

It is easy to understand and a good mixture between writing about important concepts
and get you going.

best regards Stefan

consider

#if 0
# define IRsensor 2
#else
# define IRsensor A1
#endif

#define DCwater_pump 13
void setup() {
    // put your setup code here, to run once:
    pinMode(IRsensor, INPUT);
    pinMode(DCwater_pump, OUTPUT);
    Serial.begin(9600);
}

unsigned long msecLst = 0;
unsigned long msec;

int readPin = 0;

void loop()
{
    msec = millis ();

    readPin = digitalRead(IRsensor);
    if (readPin  == HIGH)
    {
        if (0 != msecLst && msec - msecLst > 3000)  {
            digitalWrite(DCwater_pump, HIGH);
            Serial.println("DC Pump is OFF now!!");
            delayMicroseconds(2);
            msecLst = 0;
        }
    }

    else
    {
        digitalWrite(DCwater_pump, LOW);
        Serial.println("DC Pump is ON Now !!");
        msecLst = msec;
    }
}