i have connected HC-SR04 ultrasonic sensor Echo pin to the digital pin 4, i need to read the signal from the pin for 500 milliseconds, and find the difference between the maximum and minimum values within that period and make a decision with that result. Can anyone give me the program for this problem
Here's a start:
int minimum ;
int maximum ;
unsigned long start_time ;
void read_rate ()
{
start_time = millis () ;
int value = read () ;
minimum = maximum = value ;
while (millis () - start_time < 500)
{
value = read () ;
// now deal with maintaining minimum and maximum
..
}
..
}
Hi Kishore
Just wanted to check I understand what you are trying to do.
The HC-SR04 puts out an ultrasonic "ping" when it sees a pulse on its input Trig pin. It then measures how long it takes for an echo to come back. Then it outputs a pulse on its output Echo pin - the duration of this pulse is the same as the time between the ping and the echo.
So, I assume you are generating pings every 50ms (for example) and you want to measure the echo times for 10 of them and then find the maximum and minimum echo times?
In that case, I would suggest you use a library called NewPing to generate the pings and get the echo times. Then use code like Mark T suggested to get max and min.
Note that there is a lower limit on how frequently you can send "pings". You obviously need to allow enough time for a valid echo to come back and be processed before sending another ping. From memory, it is 30ms.
EDIT: Bad use of "frequently". To clarify, lower limit on the interval between one ping and the next.
Hope this helps
Ray
i am new to this Arduino environment, i am not able to apply the program what Mr, Mark T posted. I want to store the pulses of particular period from sensor and i want to retrieve the maximum and minimum values from that, i ll find the difference of those values and proceed with my decision making using "If" statement. so suggest me the program to store the "duration" in my program attached below and retrieve the max and min duration value.
#define LED_PIN 13
#include <ZumoMotors.h>
const int trigPin = 2;
const int echoPin = 4;
int duration = 0;
int durationMin = 1023;
int durationMax = 0;
int aproach = 0;
const int threshold1 = 5;
ZumoMotors motors;
void setup()
{
Serial.begin(9600);
pinMode(LED_PIN, OUTPUT);
}
void loop()
{
long duration, inches, cm;
pinMode (trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
inches = microsecondsToInches(duration);
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
while (millis() < 1000) {
duration = pulseIn(echoPin, HIGH);
// record the maximum sensor value
if ( duration > durationMax) {
durationMax = duration;
}
// record the minimum sensor value
if ( duration < durationMin) {
durationMin = duration;
}
}
aproach = durationMax - durationMin;
Serial.print(duration);
Serial.print("du, ");
Serial.print(aproach);
Serial.print("ap, ");
Serial.print(durationMin);
Serial.print("min, ");
Serial.print(durationMax);
Serial.print("max, ");
delay(100);
if (aproach > threshold1)
{
motors.setSpeeds(0, 200);
}
else {
motors.setSpeeds(0, 0);
}
}
long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}
Thanks for posting the code.
I think this part sends one "ping" each time round the loop, measures the echo duration, prints some values and then waits 100ms. Are you getting sensible answers from the print statements?
long duration, inches, cm;
pinMode (trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
inches = microsecondsToInches(duration);
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
Next, the while loop will not work the way it is written. Remove the while loop for now. Also remove the next pulseIn statement. You already have a value in duration.
while (millis() < 1000) {
duration = pulseIn(echoPin, HIGH);
When you remove the while loop, remember to remove the closing "}" (the one before "aproach =").
Just remove the loop, do not remove this code which is in it. It looks to be doing what you need - finding the maximum and minimum durations since the program started running.
// record the maximum sensor value
if ( duration > durationMax) {
durationMax = duration;
}
// record the minimum sensor value
if ( duration < durationMin) {
durationMin = duration;
}
But I am confused about what you do next.
aproach = durationMax - durationMin;
if (aproach > threshold1)
{
motors.setSpeeds(0, 200);
}
else {
motors.setSpeeds(0, 0);
}
The variable "aproach" will get bigger if the spread of echo durations gets bigger. So you adjust the motor speed. But if this is meant to change the echo durations, there is nothing to reduce aproach - it can only stay the same or get bigger.
Can you tell us what the program is trying to do in real world terms? What are you pinging, what are you controlling?
Thanks
Ray
ye,s i am getting good response from the sensors duration value.
The duration is the value from Ultrasonic sensor. I want to reverse the motor when an object is approaching in high speed towards my robot. so i will take the time of 500 milliseconds as reference time and i ll find the change in pulse value (max duration - min duration), this means an object is approaching to attack) of ultrasonic sensor for that 500 millisec, if the difference value is significantly higher i ll reverse the motor
Why do you need more than 3 values to determine the rate of change? If the time between echo one and echo two is n, and the time between echo two and echo three is m, then the relationship between n and m gives you all the information that you need. If m is less than n, something is approaching. If m is greater than n, something is retreating. If m is slightly smaller than n, whatever it is that is approaching is doing so slowly. If m if significantly smaller than n, then whatever is approaching is doing so quickly.
Time between two echos will be very short. when we compare the speed of opponent robot and the speed of sound then there will be no change in robot position between to echos. so how is it possible to define the approach?
Time between two echos will be very short.
That depends on how often you send a pulse.
Send a pulse. Get an echo. Record the distance based on the time to get the echo.
Wait a bit (say 5 milliseconds).
Send a pulse. Get an echo. Record the distance based on the time to get the echo.
Wait a bit (say 5 milliseconds).
Send a pulse. Get an echo. Record the distance based on the time to get the echo.
Three pulses. Three times (or distances).
Yes, the individual times will be small. The relative time, though, will be quite a bit larger.
Your way of blindly doing nothing but pinging will have your robot dead in no time.
I agree with Paul, but suggest a longer inter-ping time.
Teckel, who wrote the NewPing library after a lot of experiments with these sensors, found that less than 30ms between pings increases the chance of false readings due to picking up late echoes from the previous ping.
A robot travelling at e.g. 30 kph (is that fast for a robot?) will travel only 25cm in 30ms.
There more information here about NewPing: NewPing Library: HC-SR04, SRF05, SRF06, DYP-ME007, Parallax PING))) - v1.7 - Sensors - Arduino Forum