It is reading this max value unless I put my flat hand at nearly exactly a 90deg tangent as if it were a laser. It is intended to detect the presence of someone directly in front of door as if entering (not as if visiting). Heads don't seem to provide enough reflectance. I had it make tones based on distance measured. I'm unsure if my sensor is just not the correct type for job, or it's defective.
**** Solved: Used library NewPing.h in lieu of ultrasonic.h. Much better response.
Hi,
I have worked on HC-SR04 Ultrasonic Sensor and it works really great. You can easily convert the distance from hurdle into cm and inches.
I think you must be doing something wrong in the coding. Share your code and I will check it out.
Thanks.
Well, there I think i'm fine because there isn't much to code. This function is called once per second. If somehow I couldn't divide by 2.54 to get inches, I'd still be happy. Problem is it's very insensitive. I got it from aliexpress.com (China) and one of two was a total dud... so maybe this one is bad too.
void distanceCheck() {
int distance = ultrasonic.read();
//distance = random(0,357);
int beepTone = ((distance * 3) + 1000);
noTone(HO8);
tone(HO8, beepTone, 400);
//Serial.print(distance); Serial.println("cm");
if ((distance == 0) || (distance > 350)) return; //throw out invalid readings.
distanceA[iG] = distance;
iG++;
if (iG == 8) {
iG == 0;
} //loop of iG (global)
distance = distanceArrayMin();
eventName = String(distance);
long number = 7090000;
number = number + distance;
sp(number);
if (distance < 210) {
powerBoostInc++;
if (powerBoostInc > 5) {
powerBoost(1);
sp(7015001);
powerBoostInc = 0;
eventName = "Door open."; //Because Doorbell doesn't work (doesn't sound)
beep(1, 1);
}
}
if (distance > 240) {
powerBoostInc++;
if (powerBoostInc > 5) {
powerBoost(-1);
sp(7015000);
powerBoostInc = 0;
}
}
}
Hi,
Try this code out:
// defines arduino pins numbers
const int trigPin = 12;
const int echoPin = 11;
// defines variables
long duration;
int distance;
void setup()
{
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance from the object = ");
Serial.print(distance);
Serial.println(" cm");
delay(1000);
}
It's for Ultrasonic sensor HC-SR04. Connect Pins as follows:
- Trigger Pin to Pin # 12 of Arduino.
- Echo Pin to Pin # 11 of Arduino.
After uploading the code, open your Serial Monitor and test out your software. For code source, check Ultrasonic Sensor Arduino Interfacing.
Thanks.
Thanks, in the time intervening, I found a NewPing library which when I gutted out parts of, worked much better.