HC-SR04 Distance Sensor and Arduino Troubles

Okay, i am having troubles with my arduino and my HC-SR04 distance sensor.

I am building a halloween prop that will be triggered by the arduino if the distance reads on the sensor less than 3 meters. I have the circuit currently connected to 3 LEDs to represent the prop going off, lights, and sound. But the problem i am having, is that even when there is nothing within 3 meters of it, it still wants to ‘go off’ or trigger the leds about every half a second. I do not know if this is because of not having an else statement or because of the delay(50) between each reading of distance or what, but any help would be appreciated.

Here is the code i am currently using:

/*
 HC-SR04 Ping distance sensor:
 VCC to arduino 5v 
 GND to arduino GND
 Echo to Arduino pin 7 
 Trig to Arduino pin 8
 
 This sketch originates from Virtualmix: http://goo.gl/kJ8Gl
 Has been modified by Winkle ink here: http://winkleink.blogspot.com.au/2012/05/arduino-hc-sr04-ultrasonic-distance.html
 And modified further by ScottC here: http://arduinobasics.blogspot.com/
 on 10 Nov 2012.
 */


#define echoPin 7 // Echo Pin
#define trigPin 8 // Trigger Pin
#define LEDPin1 9 // LED 2
#define LEDPin2 10 // LED 2
#define LEDPin3 11 // LED 3

int maximumRange = 100; // Maximum range needed
int minimumRange = 10; // Minimum range needed
long duration, distance; // Duration used to calculate distance

void setup() {
 Serial.begin (9600);
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 pinMode(LEDPin1, OUTPUT); 
 pinMode(LEDPin2, OUTPUT);
 pinMode(LEDPin3, OUTPUT);
}

void loop() {
/* The following trigPin/echoPin cycle is used to determine the
 distance of the nearest object by bouncing soundwaves off of it. */ 
 digitalWrite(trigPin, LOW); 
 delayMicroseconds(2); 

 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10); 
 
 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);
 
 //Calculate the distance (in cm) based on the speed of sound.
 distance = duration/58.2;
 
 if (distance <= maximumRange || distance >= minimumRange){
 /* Send a negative number to computer and Turn LED ON 
 to indicate "out of range" */
 Serial.println("Prop Goes Off");
 digitalWrite(LEDPin1, HIGH);
 delay(200);
 digitalWrite(LEDPin2, HIGH);
 digitalWrite(LEDPin3, HIGH);
 digitalWrite(LEDPin1, LOW);
 delay(200);
 digitalWrite(LEDPin2, LOW);
 digitalWrite(LEDPin3, LOW);
 delay(50);
 }
 if (distance >= maximumRange || distance <= minimumRange) {
   Serial.println("Nothing Happens");
   digitalWrite(LEDPin1, LOW);
   digitalWrite(LEDPin2, LOW);
   digitalWrite(LEDPin3, LOW);
 }
   
 
 
 //Delay 50ms before next reading.
 delay(50);
}

I originally took the code off of this website/tutorial if that is any help.
http://arduinobasics.blogspot.com/2012/11/arduinobasics-hc-sr04-ultrasonic-sensor.html
Also, the numbers in my code might not be how i said “3 meters” because i will change the values later, i may have just put 1 meter for now because of testing purposes.

Thanks alot.
Macke

if (distance <= maximumRange || distance >= minimumRange){
 /* Send a negative number to computer and Turn LED ON 
 to indicate "out of range" */
 Serial.println("Prop Goes Off");
 digitalWrite(LEDPin1, HIGH);
 delay(200);
 digitalWrite(LEDPin2, HIGH);
 digitalWrite(LEDPin3, HIGH);
 digitalWrite(LEDPin1, LOW);
 delay(200);
 digitalWrite(LEDPin2, LOW);
 digitalWrite(LEDPin3, LOW);
 delay(50);
 }

Or? shouldn’t that be And? Does that comment make sense? Not to me it doesn’t.

If your talking about the "/* Send a negative number to computer and Turn LED On to indicate "out of range" */", Ignore that that was from the origianal code and i forgot to take it out. If your talking about something else, then i dont understand your question.

if (distance <= maximumRange || distance >= minimumRange){

is worng!

Mark

Hi Macke,

As indicated by Mark - you need to understand how your if-else statements work. In your code, you have told the Arduino to trigger the LEDs when the sensor detects an object that is LESS THAN 1 meter OR greater than 10 cm. Essentially, you have asked the Arduino to trigger the LEDs all of the time.
I have modified your code (see below) , so that when the sensor detects an object which is less than 10cm away or greater than 3 meters, it will turn the LEDs off. You could simplify it further by removing all references to a minimum distance, but I have included it (because you have it in your code).
If the sensor detects an object which is between 10cm and 3 m away, it will trigger the LEDs.

#define echoPin 7 // Echo Pin
#define trigPin 8 // Trigger Pin
#define LEDPin1 9 // LED 2
#define LEDPin2 10 // LED 2
#define LEDPin3 11 // LED 3

int maximumRange = 300; // Maximum range needed
int minimumRange = 10; // Minimum range needed
long duration, distance; // Duration used to calculate distance

void setup() {
    Serial.begin (9600);
    pinMode(trigPin, OUTPUT);
    pinMode(echoPin, INPUT);
    pinMode(LEDPin1, OUTPUT); 
    pinMode(LEDPin2, OUTPUT);
    pinMode(LEDPin3, OUTPUT);
}

void loop() {
/* The following trigPin/echoPin cycle is used to determine the
 distance of the nearest object by bouncing soundwaves off of it. */ 
     digitalWrite(trigPin, LOW); 
     delayMicroseconds(2); 

     digitalWrite(trigPin, HIGH);
     delayMicroseconds(10); 
 
     digitalWrite(trigPin, LOW);
     duration = pulseIn(echoPin, HIGH);
 
 //Calculate the distance (in cm) based on the speed of sound.
 distance = duration/58.2;

//If the distance is beyond the limits - then nothing happens ie, more than 3 meters or less than 10 cm
if (distance >= maximumRange || distance <= minimumRange) {
   Serial.println("Nothing Happens");
   digitalWrite(LEDPin1, LOW);
   digitalWrite(LEDPin2, LOW);
   digitalWrite(LEDPin3, LOW);
 } else {
Serial.println("Prop Goes Off");
     digitalWrite(LEDPin1, HIGH);
     delay(200);
     digitalWrite(LEDPin2, HIGH);
     digitalWrite(LEDPin3, HIGH);
     digitalWrite(LEDPin1, LOW);
     delay(200);
     digitalWrite(LEDPin2, LOW);
     digitalWrite(LEDPin3, LOW);
     delay(50);
 }