Quick question about range sensor

I use an arduino mega and an EZ1
The 7 analog pin is connected to the sensor’s analog out, and the digital 6 pin to RX

void setup()                   
{delay(1000);
Serial.begin(9600);
pinMode(6, OUTPUT);
}

void loop()                     
{    
 s=0;delay(100);
  for(int i=1;i<=l;i++)
   {a();int y=analogRead(7);s+=y/l;Serial.print(y);Serial.print(" ");}// averaging out data
 Serial.print("->");Serial.println(s);     

}

void a()
{digitalWrite(6, LOW);
   digitalWrite(6, HIGH); //sensor reset (I think)
   delay(150);   
}

The output is fairly random. First bunch is when I had the sensor covered with my hand, second when there were no objects for 2 meters in front of the sensor, and third when I held my had roughly 50 cm away from the sensor.

644 0 1023 0 853 842 0 1023 486 0 ->485

0 1023 11 765 1023 15 818 1023 0 1023 ->567

638 1023 0 898 775 0 1023 191 149 1023 ->568

1023 508 0 1023 272 111 1023 472 0 1023 ->543

1023 401 0 1023 0 840 902 0 1023 263 ->546

539 0 1023 226 168 1023 162 254 1023 74 ->445

314 10 1023 15 415 1023 12 790 949 0 ->452

0 1023 171 41 1023 72 673 1023 54 742 ->480




491 317 882 301 291 838 239 395 750 106 ->457

183 401 781 149 432 747 51 564 500 161 ->394

223 461 403 225 491 331 248 498 305 294 ->344

250 487 280 299 484 225 353 456 190 367 ->335

282 470 219 296 455 210 353 401 184 412 ->325

368 421 184 385 403 181 402 349 198 444 ->32



380 354 521 259 332 486 233 373 422 200 ->353

209 372 438 195 382 419 180 393 367 191 ->310

180 400 354 182 409 343 179 448 331 210 ->300

179 453 319 216 459 305 233 461 246 243 ->306

200 448 278 211 458 264 225 457 251 274 ->302

235 458 240 284 454 197 295 452 187 307 ->306

265 450 179 319 407 173 377 400 171 390 ->309

Since I’m quite new, I’m not sure if I fumbled the code somewhere or if I’m not interpreting something right, or any other reasons why things might go wrong.

If anybody can help shed some light on the output and why it seems so random I’d appreciate it.

Thank you for your time.

I'm not sure if you have to, but you're not declaring pin 7 as an input pin. I've used the EZ0, and I just used +5v, GND, and the analog wire.

but you're not declaring pin 7 as an input pin

It's an analogue pin; no need.

Have you got grounds connected?

Out of interest, what is the value of "l" ("ell"), the averaging/loop control?

Also, have you tried removing the call to "a()" from the loop? (put it in "setup()") According to the datasheet http://www.maxbotix.com/uploads/LV-MaxSonar-EZ1-Datasheet.pdf, if you leave Rx high, the unit ranges continuously, which is what you want, isn't it?

Rather than using analogue, have you considered using the PW pin on the sensor, in conjunction with "pulseIN"?

I’ve tried the example for the PING sensor with values modified after the datasheet of the EZ1, and I had better results… of sorts.

long microsecondsToInches(long microseconds)
{
  return microseconds / 147;
}

long microsecondsToCentimeters(long microseconds)
{
  return microseconds / 58;
}

It almost pinpointed the ceiling from the table at 179cm(within 15-20 cm), but it thought it was at 174cm, from floor level. I can get the value to drop by 2cm if I move my hand close enough( 5 cm away) and I can get a drop of 1cm if my hand is about 30 cm away.

I’ve also had some trial and error with the serial input, but I can’t seem to figure it out. If anybody could provide a sample of working code, and maybe a picture of the wiring and some output I’d really appreciate it.

edit: forgot to mention that the gnd is connected

As an update, it proved to be a faulty sensor. I had it replaced and everything works now.

Thank you for the great help.