Do not use glitch filter with edge both please
I am not sure but I think you canât have glitch filter on with an InterruptPort. What is the default state of the echo pin? Can you describe what does the sensor do with the echo pin once you have triggered the measurement?
Edit: Didnât see Gusâs reply.
The echo pin on the HC-SRO4 goes high (simultaneous with 10 us trigger pulse) until it sees a return echo (thus the timing) and if no echo times out &goes low at about 38 ms.
@ idafez - Thanks!
@ idafez -
+1
Guys, help!
please
itâs very important for me.
All time I find completed libraries and classes on TinyCLR, but now, for HC-SR04 I canât do it.
I just want library, what use one function to count distance in my project
Thank you!
Did you try last suggestion?
@ Architect -
No,I donât understand them (
What in particular are struggling with?
I have error:
An unhandled exception of type âSystem.ArgumentExceptionâ occurred Microsoft.SPOT.Hardware.dll
in this line:
private static InterruptPort EchoPin = new InterruptPort((Cpu.Pin)FEZ_Pin.Digital.Di10, true, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeBoth);
I donât know how to fix it,
Ok you absolutely have to read the documentation. The suggestion was not to use the glitch filter. This is the second parameter in constructor which you have set to âtrueâ effectively enabling it. Use âfalseâ instead to disable it.
Oh no, again appeared error:
âAn unhandled exception of type âSystem.ArgumentExceptionâ occurred Microsoft.SPOT.Hardware.dllâ
Try using pull down resistor mode.
How I can do it?
Change parameter 3 from disabled to pulldown
Yes, it helps, but now this error appear in this line:
EchoPin.OnInterrupt += new NativeEventHandler(port_OnInterrupt);
What error?
âAn unhandled exception of type âSystem.ArgumentExceptionâ occurred Microsoft.SPOT.Hardware.dllâ
I donât have that sensor so canât verify/adapt that code, but here is somebody implemented it for netduino. You should be able to use it:
I decide to use this code:
using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.Hardware;
using GHIElectronics.NETMF.FEZ;
namespace sonar
{
public class Program
{
private static int ticks;
private static InterruptPort EchoPin = new InterruptPort((Cpu.Pin)FEZ_Pin.Digital.Di7, false, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeLow);
private static OutputPort TriggerPin = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di6, false);
public static void Main()
{
EchoPin.OnInterrupt += new NativeEventHandler(EchoPin_OnInterrupt);
EchoPin.DisableInterrupt();
while (true)
{
Distance();
// Debug.Print("distance = " + myDistance + " mm.");
Thread.Sleep(1000);
}
}
/* private static void EchoPin_OnInterrupt(uint data1, uint data2, DateTime time)
{
//throw new NotImplementedException();
}*/
public static void Distance()
{
EchoPin.EnableInterrupt();
TriggerPin.Write(false);
Thread.Sleep(2);
TriggerPin.Write(true);
Thread.Sleep(10);
TriggerPin.Write(false);
Thread.Sleep(2);
}
private static void EchoPin_OnInterrupt(uint port, uint state, DateTime time)
{
if (state == 0) // falling edge, end of pulse
{
int pulseWidth = (int)time.Ticks - ticks;
// valid for 20A°C
int pulseWidthMilliSeconds = pulseWidth * 10 / 582;
//valid for 24A°C
//int pulseWidthMilliSeconds = (pulseWidth * 10 / (int)578.29);
Debug.Print("Distance = " + pulseWidthMilliSeconds + " millimA?tres.");
}
else
{
ticks = (int)time.Ticks;
}
EchoPin.ClearInterrupt();
}
}
}
and i have next values:
Distance = -903322 millimA?tres.
Distance = -866169 millimA?tres.
Distance = -691589 millimA?tres.
Distance = -517014 millimA?tres.
Distance = -343862 millimA?tres.
Distance = -169172 millimA?tres.
Distance = 5516 millimA?tres.
Distance = 180193 millimA?tres.
Distance = 356453 millimA?tres.
Distance = 531129 millimA?tres.
This values is wrong.
Looks like time only count (inc or dec).