Notification

Icon
Error

Ultrasonic HC-SR04
wbsimms
#1 Posted : Wednesday, January 23, 2013 8:25:59 PM(UTC)
Rank: Tinkerer

Joined: 1/23/2013(UTC)
Posts: 1

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) { }
}

Chips4brains
#2 Posted : Thursday, January 31, 2013 3:02:50 PM(UTC)
Rank: Tinkerer

Joined: 1/31/2013(UTC)
Posts: 1

From what I can tell, you have your timer firing every millisecond which means your calculated value will essentialy be constant.


I do not have any hardware to experiment with but what I found about the GT timer is that the interval is set in milliseconds and not seconds. 


Try changing the timer from 1 to 1000 and see what your result is.


Hope this helps.....

Rss Feed  Atom Feed
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Powered by YAF | YAF © 2003-2011, Yet Another Forum.NET
This page was generated in 0.100 seconds.