When I put my hand 12 inches from the sensor, the motor goes backwards. Any other distance has no effect. Any ideas?
Boards:
HC-SR04 Ping Sensor
http://www.sainsmart.com/sainsmart-l293d-motor-drive-shield-for-arduino-duemilanove-mega-uno-r3-avr-atmel.html?___store=en&___store=en
#include <AFMotor.h>
#define pingPin A5
#define echoPin A3
AF_DCMotor motor(4, MOTOR12_64KHZ); // create motor #4, 64KHz pwm
void setup() {
Serial.begin(9600);
motor.setSpeed(200); // set the speed to 200/255
}
void loop() {
long duration, inches;
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
inches = microsecondsToInches(duration);
Serial.print(inches);
Serial.print("in, ");
Serial.println();
if (inches < 12){
motor.run(BACKWARD); // Reverse for 2 seconds
delay(2000);
}
}
long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}