View Single Post
  #5   Spotlight this post!  
Unread 16-01-2015, 10:53
catacon catacon is offline
Registered User
FRC #1444 (Lightning Lancers)
Team Role: Mentor
 
Join Date: Jan 2009
Rookie Year: 2006
Location: St. Louis
Posts: 154
catacon is a splendid one to beholdcatacon is a splendid one to beholdcatacon is a splendid one to beholdcatacon is a splendid one to beholdcatacon is a splendid one to beholdcatacon is a splendid one to beholdcatacon is a splendid one to behold
Re: A relatively dumb question...

You have to convert the voltage to a distance value. The sensor should have a spec for mV/in or mV/m or something relating voltage and distance.

For example, the MaxBotics MB1010 LV-EZ1 has a spec of 9.8mV/in with a 5V supply. So, if you read 0.392V from the sensor, this would result in a distance of 40 inches. (0.392 / 0.0098)

Some simple code you could use.

Code:
AnalogInput ultrasonic(0);

float voltage(0);
float distance(0);
const float volts_to_inches(0.0098);

voltage = ultrasonic.GetVoltage();
distance = voltage / volts_to_inches;
These sensors tend to be pretty noisy, so I would suggest a simple low pass filter.

Code:
const float filter_alpha = 0.5;

voltage = ultrasonic.GetVoltage();
distance = distance + filter_alpha * ((voltage / volts_to_inches) - distance);


Hope that helps!


EDIT: Didn't realize this was in the Java forum and I wrote C++. Should still give you an idea of what to do.

Last edited by catacon : 16-01-2015 at 11:01.
Reply With Quote