Go Down

### Topic: HC-SR04 Array-Problem? pulseIn-Problem? Timing-Problem?  (Read 898 times)previous topic - next topic

#### BeobachterS

##### Dec 23, 2015, 09:26 pm
Hi, my first time in this forum.
I'm using the ultrasonic-sensor HC-SR04 and measure a distance and the time. In the serial monitor the data looks like that:

817
152
827
158
836
158

and so on. The first data is the time in µs, the second data the distance in cm. So it works. But i will separate the distance-data from the time-data in the serial monitor with loops. But it doesn't work well. First i get all the time-data, but all distance-data are zeros. Look at my sorcecode, maybe you find something.

Code: [Select]
`// Arduino Uno, IDE 1.6.7// 23.12.2015# define echoPin 2# define trigPin 1int n;int n_max = 100;int array_distance[100];int array_time[100];void setup() {  pinMode(echoPin, INPUT);  pinMode(trigPin, OUTPUT);  Serial.begin(9600);}void loop() {  for (n = 0; n < n_max; n++) {    Triggerimpuls();    Distanz_Berechnung();    Zeitmessung();    Ausgabe_Zeit();    Ausgabe_Distanz();    //delay(1000); //Ausgabe_Distanz delivers only zeros. Why?  }  //Ausgabe_Zeit();   //Ausgabe_Distanz(); // doesn't work with the loop, only zeros. Why?  Programmende(); // durch Endlosschleife}void Triggerimpuls() {  digitalWrite(trigPin, HIGH);  delayMicroseconds(10);  digitalWrite(trigPin, LOW);}void Distanz_Berechnung() {  array_distance[n] = pulseIn(echoPin, HIGH, 25000) * 0.01715;}void Zeitmessung() {  array_time[n] = millis();}void Ausgabe_Zeit() {  // for (n = 0; n < n_max; n++) {  Serial.println(array_time[n]);  // }}void Ausgabe_Distanz () {  //for (n = 0; n < n_max; n++) {    Serial.println(array_distance[n]);  //}  // Ausgabe_Distanz delivers only zeros. Why?}void Programmende () {  while (1);}`

Thank you !!

#### robtillaart

#1
##### Dec 23, 2015, 09:31 pm
try change

int array_distance[100];
==>
float int array_distance[100];
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

#### BeobachterS

#2
##### Dec 23, 2015, 10:14 pm
Thank you, but float (without the following int) doesn't change anything, only more zeros (behind the comma).

I do the same with both arrays, but why is the array_distance full of zeros and the array_time not. It's inexplicable (for me).

#### robtillaart

#3
##### Dec 23, 2015, 10:30 pmLast Edit: Dec 24, 2015, 11:45 am by robtillaart
array_distance[n] = pulseIn(echoPin, HIGH, 25000) * 0.01715;

if pulseIn return some integer value smaller than ~58 the multiplication will result in a value smaller than 1
which is truncated to 0 (because it becomes an int)

please change to this to see if pulsein returns meaningfull values.

array_distance[n] = pulseIn(echoPin, HIGH, 25000) ;
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

#### robtillaart

#4
##### Dec 23, 2015, 10:31 pmLast Edit: Dec 23, 2015, 10:31 pm by robtillaart
furthermore

# define echoPin 2
# define trigPin 1

is dangerous as pin 1 is used by Serial !!

try it with pin 3 and 4
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

#### BeobachterS

#5
##### Dec 23, 2015, 10:57 pm
Hi,

the factor 0.01715 is ok, because the smallest echo back o f the sensor ist 150µs, so the result is never smaller 1.

But your second idea is priceless. You did it !! Now i'm using Pin 2 and Pin 3 and it works well. AWESOME !!

I wish you warm xmas !!!

#### robtillaart

#6
##### Dec 24, 2015, 12:35 pm
Thanks,
Auch für Sie Frohe Weihnachten
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up