automatic hand sanitizer

hello guys, i want to make a automatic hand sanitizer but one eroor are ocuured i want to use ir sensor but for for 3 seconds example(the person put hand in ir sensor the ir sens the hand and but i want to sens it only for 3 sec and it will reset it) this is my code

include "Arduino.h"

define IRFLAME_5V_PIN_SIGNAL 2

define REALYMODULE_5V_PIN_SIGNAL 6

void setup() { pinMode(2,INPUT); pinMode(7,OUTPUT); Serial.begin(9600); }

void loop() { if (digitalRead(2) == 1) { Serial.println(digitalRead(2)); digitalWrite(7,HIGH); delay(1000);

Serial.println(digitalRead(2)); digitalWrite(7,LOW); }

else{ digitalWrite(7,LOW); delay(1000); } }

Could you take a few moments to Learn How To Use The Forum. It will help you get the best out of the forum in the future. Other general help and troubleshooting advice can be found here.

assuming your button is wired correctly (normally it would pull low)

void setup()
{
    pinMode(2,INPUT);

    digitalWrite(7,LOW);
    pinMode(7,OUTPUT);
    Serial.begin(9600);
}


void loop()
{
    if (digitalRead(2) == 1)
    {
        Serial.println("go");

        digitalWrite(7,HIGH);
        delay(3000);
        digitalWrite(7,LOW);
    }
}
bool handPresent = FALSE;

void setup()
{
    pinMode(2,INPUT);

    digitalWrite(7,LOW);
    pinMode(7,OUTPUT);
    Serial.begin(9600);
}


void loop()
{
    int pinState = digitalRead(2);
    if (pinState == HIGH && handPresent == FALSE)
    {
        Serial.println("go");

        digitalWrite(7,HIGH);
        delay(3000);
        digitalWrite(7,LOW);
        handPresent = TRUE;
    }
    else if (pinState == LOW)
    {
        handPresent = FALSE;
    }
}