I just picked up the Ultrasonic HC-SR04 and a FEZ Spider. Has anyone had any success getting this to work?
I've been able to cobble this together from around the web. Mostly from aduino resources. However, the inches calculation seems to return the same result no matter how far I move from the sensor.
Any help would be appreciated.
private DigitalOutput output;
private InterruptInput input;
private static long beginTick, endTick, minTicks;
private double inchConversion;
void ProgramStarted()
{
minTicks = 6200L;
inchConversion = 1440.0;
Debug.Print("Program Started");
output = extender.SetupDigitalOutput(GT.Socket.Pin.Seven,false);
input = extender.SetupInterruptInput(GT.Socket.Pin.Three, GlitchFilterMode.On, ResistorMode.Disabled,
InterruptMode.RisingAndFallingEdge);
input.Interrupt += new InterruptInput.InterruptEventHandler(input_Interrupt);
GT.Timer timer = new GT.Timer(1); // every second (1000ms)
timer.Tick += new GT.Timer.TickEventHandler(timer_Tick);
timer.Start();
}
void timer_Tick(GT.Timer timer)
{
output.Write(false);
DelayMicroseconds(2);
endTick = 0;
beginTick = DateTime.Now.Ticks;
output.Write(true);
DelayMicroseconds(10);
output.Write(false);
DelayMicroseconds(2);
}
public double TicksToInches(long ticks)
{
return ticks / 148d;
}
void input_Interrupt(InterruptInput sender, bool value)
{
endTick = DateTime.Now.Ticks;
long elapsed = endTick - beginTick;
elapsed -= minTicks;
double inches = TicksToInches(elapsed);
Debug.Print("Inches : " + inches);
Debug.Print("Start: " + beginTick);
Debug.Print("End: " + endTick);
}
private const long TicksPerMicrosecond = TimeSpan.TicksPerMillisecond / 1000;
private static void DelayMicroseconds(int microSeconds)
{
long stopTicks = Utility.GetMachineTime().Ticks +
(microSeconds * TicksPerMicrosecond);
while (Utility.GetMachineTime().Ticks < stopTicks) { }
}