I want to rotate the motor for 3 sec and stop and then wait for ir signal and rotate for 3 sec in reverse and stop,but my motor rotates infinitely

int IRSensor = 2;
int LED = 13; //
int pin7=7;
int pin8=8;

void setup()
{
pinMode (IRSensor, INPUT);
pinMode (LED, OUTPUT);
pinMode(pin7,OUTPUT);
pinMode(pin8,OUTPUT);
}

void loop()
{
int statusSensor = digitalRead (IRSensor);

if (statusSensor == 1)
{
digitalWrite(LED,LOW);
digitalWrite(pin8,LOW);
digitalWrite(pin7,LOW);
digitalWrite(pin8,HIGH);
delay(3000);
digitalWrite(pin8,LOW);
delay(1000);

}

else

{
digitalWrite(LED,HIGH);

digitalWrite(pin8,LOW);
digitalWrite(pin7,HIGH);
delay(3000);
digitalWrite(pin7,LOW);
delay(1000);
}

}Preformatted text

Please edit your message and insert your code in code tags

Read the forum guidelines to see how to properly post code and some good information on making a good post.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

You can go back and fix your original post by highlighting the code and clicking the </> in the menu bar.
code tags new

When asking a question about hardware you need to include a schematic and technical data for the components. A hand drawn schematic, photographed and posted is fine. Include all pin names/numbers, components, their part numbers and/or values and power supplies.

Hi, @narenk
Welcome to the forum.

Can you also post a circuit diagram?

Thanks.. Tom.... :smiley: :+1: :coffee: :australia:

You should at least post the brand and typenumber of the IR-sensor.
Even better would be to post a link to the datasheet.
best regards Stefan

I don't have it,I'm sorry

I want to rotate the motor for 3 sec and stop and then wait for ir signal and rotate for 3 sec in reverse and stop,but my motor rotates infinitely

Your description in the headline is in any case incomplete:

  1. What makes it start the 3s forward rotation: should it do that as soon as the Arduino gets power, or what?
  2. And then once it's stopped after the 3s reverse rotation, what happens next?

The coding for this will be pretty simple; as is often the case, the tricky part is the unambiguous writing down of what the thing's supposed to do.

And which combination of highs and lows on pins 7 and 8 are you calling "forward" and "reverse"?.

Tell us about the LED. Is it on or off or blinking?

What you told it to do was Rotate for 3 seconds in a direction that depends on the IRSensor reading, then pause 1 second. You didn't write the part where it waits for a change in the IRSensor.

If it isn't pausing for one second after three seconds of moving then the wiring or hardware are incorrect.

Low-off,High-On

It pauses

But the loop continues forever like it runs 3sec and stops 1 sec,I just want the output to be like it should run for 3sec and stop wait for ir signal and then rotate in reverse direction for 3 sec and stop

What makes it start the 3s forward rotation: should it do that as soon as the Arduino gets power, or what?
Yeah as soon as Arduino gets power,

  1. And then once it's stopped after the 3s reverse rotation, what happens next?

Loop continues infinitely it runs 3 sec and then stops i

Good! If it pauses that means your hardware is working. Now you just have to look at the StateChangeDetection example to see how to determine when the input has changed.

Have you set the sensitivity level for your IR sensor?

No
Sir

Well, since your continuous motor rotation is based on the signal from the IR sensor, it might be time to set the sensor control.

Hi,

It will help us if you could draw a schematic, not a Fritzy image.
An image of a hand drawn circuit would be great.
Please include power supplies, hardware, component names and pin labels.

How are you driving the motor?

Thanks.. Tom.... :smiley: :+1: :coffee: :australia: