IR sensor driving DC Motor

i try making the dc motor run for 10 seconds after blocking the IR sensor.. problem with the code ..do i use delay or milli

alibynakish:
do i use delay or milli

millis(), for sure.

But you also need to incorporate the technique from the state change detect tutorial: you need to see when the sensor changes from not-blocked to blocked, then capture millis() into a variable, and turn the motor on. Then each time through loop() you need to compare the new (increasing) millis() to the captured value, and see if 10s has passed.

like .for instance .after blocking and the motor starts running for 10 seconds..

alibynakish:
like .for instance .after blocking and the motor starts running for 10 seconds..

You already said that in the first post... so why say it again?

My post told you exactly how to do what you want.

But it's also important to realise that you can't connect a motor directly to the Arduino; you will need some electronics in between, like a transistor or an h-bridge (if you need to be able to choose direction.) So unless you have that in place, limit your testing to using say an led as an ersatz motor.

there's a relay and sensor circuit..then i use this code

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

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

else{
digitalWrite(7,LOW);

now am stuck here

alibynakish:
now am stuck here

See #1

delay will also work fine. You can run the motor, then add a delay of 10 seconds and then turn off the motor.

jackthomson42:
delay will also work fine. You can run the motor, then add a delay of 10 seconds and then turn off the motor.

There will very possibly be some other as yet unknown or unstated requirements, such as "ok, now I need to turn the motor off if the sensor is cleared while the motor is running" and delay() will paint the OP into a corner that prevents that.

(Exactly what happened in another thread I'm helping in, in fact, where the OP's well intentioned but ignorant use of delay() has caused a lack of response and now it's very likely re-write time.)

(But delay() vs millis() isn't the real issue here anyway: the OP needs to get his or her mind round state change detection to see when the sensor becomes (rather than is) blocked.)