const int trigPin = 5;
const int echoPin = 6;
int motorPin1 = 3; // pin 2 on L293D IC
int motorPin2 = 4; // pin 7 on L293D IC
int enablePin = 7;
void setup() {
// initialize serial communication:
Serial.begin(9600);
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(enablePin, OUTPUT); // attaches the servo on pin 9 to the servo object
digitalWrite(enablePin, HIGH);
// attaches the servo on pin 9 to the servo object
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
// and the distance result in centimeters:
long duration, cm;
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(20);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
// convert the time into a distance
cm = microsecondsToCentimeters(duration);
// the condition for the distance
if ( cm > 7 && cm < 14)
{
digitalWrite(motorPin1, LOW); // set pin 2 on L293D low
digitalWrite(motorPin2,HIGH ); // sets the servo position according to the scaled value
delay(100);
}
else if ( cm <

{
digitalWrite(motorPin1, HIGH); // set pin 2 on L293D high
digitalWrite(motorPin2, LOW); // sets the servo position according to the scaled value
delay(100);
}
else
{
digitalWrite(motorPin1, HIGH); // set pin 2 on L293D high
digitalWrite(motorPin2, LOW); // sets the servo position according to the scaled value
delay(100);
}
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
}
long microsecondsToCentimeters(long microseconds) {
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
now this code is working,but now i want to make motor move in one direction when i put something in front module for 2 sec and stop ,,,and in other direction when i remove this thing for 2 sec then stop ! ? what should i do ?