For reference, I will post the OP's code in its entirety, here:
/*
HC-SR04 Ping distance sensor]
VCC to arduino 5v GND to arduino GND
Echo to Arduino pin 13 Trig to Arduino pin 12
Red POS to Arduino pin 11
Green POS to Arduino pin 10
560 ohm resistor to both LED NEG and GRD power rail
More info at: http://goo.gl/kJ8Gl
Original code improvements to the Ping sketch sourced from Trollmaker.com
Some code and wiring inspired by http://en.wikiversity.org/wiki/User:Dstaub/robotcar
*/
#define trigPin 13
#define echoPin 12
#define led 11
#define led2 10
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
}
void loop() {
long duration, distance;
digitalWrite(trigPin, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin, HIGH);
// delayMicroseconds(1000); - Removed this line
delayMicroseconds(10); // Added this line
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance < 4) { // This is where the LED On/Off happens
digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should turn off
digitalWrite(led2,LOW);
}
else {
digitalWrite(led,LOW);
digitalWrite(led2,HIGH);
}
if (distance >= 200 || distance <= 0){
Serial.println("Out of range");
}
else {
Serial.print(distance);
Serial.println(" cm");
}
delay(500);
}
(Note to OP: when sketches are this short, it makes things much easier if you post them this way, between code
tags.)
Anyway, I've refactored your code, and put in a conversion to inches.
Still, I can't do everything (nor should I), and the code still needs your attention: see my comments.
Also, you might want to test out the measurements against a tape measure: I'm not 100% sure of the accuracy.
/*
HC-SR04 Ping distance sensor]
VCC to arduino 5v GND to arduino GND
Echo to Arduino pin 13 Trig to Arduino pin 12
Red POS to Arduino pin 11
Green POS to Arduino pin 10
560 ohm resistor to both LED NEG and GRD power rail
More info at: http://goo.gl/kJ8Gl
Original code improvements to the Ping sketch sourced from Trollmaker.com
Some code and wiring inspired by http://en.wikiversity.org/wiki/User:Dstaub/robotcar
*/
#define trigPin 13
#define echoPin 12
#define led 11
#define led2 10
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
}
void loop() {
int duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// get duration in microseconds
duration = pulseIn(echoPin, HIGH, 11544); // max dist = 6 ft 6 in; see comments below
// The speed of sound is about 343 meters per second.
// That's about 1125 feet per second, or 13500 inches per second.
// Therefore, it takes 74 microseconds for sound to travel 1 inch,
// but that's just for a one-way trip!
// In our case, the sound is doing a round trip, so it
// takes double the time: each inch of one-way distance
// translates to 148 microseconds of round trip time.
distance = duration / 148; // distance in inches
// distance = (duration/2) / 29.1; // old code (for centimeters)
if (distance == 0){
Serial.println("Out of range");
}
else {
Serial.print(distance / 12);
Serial.print(" ft ");
Serial.print(distance % 12);
Serial.println(" in");
}
if (distance < 4) { // Only four inches?! Are you sure??!!
// This is where the LED On/Off happens
digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should turn off
digitalWrite(led2,LOW);
}
else {
digitalWrite(led,LOW);
digitalWrite(led2,HIGH);
}
// what to do with LEDs when distance == 0 (i.e. invalid reading) ??
delay(250); // because I figure 500 milliseconds is too long
}