I am very new to arduinos and I have an uno with the srf05 distance sensor. What I am trying to do is start a motor that delays for 5 sec. and then runs the motor for 3 sec. after the distance sensor senses that there is nothing within 10 inches in front of it. When I run this program, the sensor still reads the distance in inches but I am having trouble actually getting the motor to start. Below is my code
const int trigPin = 12;
const int echoPin = 11;
int motorPin = 9;
void setup() {
// initialize serial communication:
Serial.begin(9600);
pinMode(motorPin, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
digitalWrite(trigPin, LOW);
}
void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches:
long duration, inches;
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
// convert the time into a distance
inches = microsecondsToInches(duration);
Serial.print(inches);
Serial.print("in, ");
Serial.println();
delay(500);
}
long microsecondsToInches(long microseconds)
{
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
while(echoPin< 10);
delay(5000);
digitalWrite(motorPin, HIGH);
delay(3000);
digitalWrite(motorPin, LOW);
while(1) { }
}