Seems like my Parrallax Ultrasonic Sensor is not reading properly or my code is wrong.No matter how close or far I put something it always reads 63 with brief intervals of 1 . Does anyone have experience with this device and can shed some light as to if it is broke or what ? Thanks
Here is the output:
Distance in inches: 63
Distance in inches: 1
Distance in inches: 63
Distance in inches: 62
Distance in inches: 63
Distance in inches: 1
Distance in inches: 62
Distance in inches: 1
Distance in inches: 63
Distance in inches: 63
Distance in inches: 63
Distance in inches: 63
Distance in inches: 1
Distance in inches: 63
Distance in inches: 1
Distance in inches: 62
Distance in inches: 63
Distance in inches: 63
Distance in inches: 62
Here is the code.
public class Program
{
private static OutputPort _led_onboard;
private static ParallaxPing _sensor_ping;
public static void Main()
{
_setup();
}
private static void _setup()
{
// Setup Onboard LED
_led_onboard = new OutputPort((Cpu.Pin)4, true);
// Ping))) is attached to Digital Pin 0
_sensor_ping = new ParallaxPing((Cpu.Pin)FEZ_Pin.Digital.Di0);
_activeate_sensor_ping();
}
#region [Setup Ping Ultrasonic Sensor]
/// <summary>
/// Activates Ping Ultrasonic Sensor and starts measuring distance
/// </summary>
private static void _activeate_sensor_ping()
{
Thread measureDistance = new Thread(new ThreadStart(_sensor_ping_sense_distance));
measureDistance.Start();
}
/// <summary>
/// Thread that continuosly measures distance using
/// Ping Ultrasonic Sensor
/// </summary>
private static void _sensor_ping_sense_distance()
{
while (true)
{
Thread.Sleep(1000);
int distance = _sensor_ping.GetDistance(ParallaxPing.DistanceUnits.Inches);
Debug.Print("Distance in inches: " + distance.ToString());
}
}
#endregion
}