[Need help] FEZ Panda II with HC-SR04

@ BUBLIK - for your code to work you need to interrupt on both edges, not just falling.

also not sure you need to enable/clear interrupts.

How I can do it without error or other fails ? :slight_smile:

what errors?

No error, because i havent started do it.
I doт’t know how to do it and how to avoid mistakes

@ BUBLIK - Have you tried the code at the link I have provided earlier?

Yes!
I talk about this code now.
ON Arduino he work exatecly, but on my Panda I have values what i show in latest messages

Arduino?

@ Architect -
Sorry, NetDuino

This code was for NetDuino, but I fix it for Panda.

Take the class HC_SR04 from that code as is. And only modify example usage (provide the two pins you are using on Panda), It should work. The way you “fixed” it for Panda is wrong.

hi can i use this sensor with FEZ SPIDER and the extender module?

yes

I am totally confused. What don’t you know how to do?

Levity to this discussion we add and prove, we will, that the HC-SRO4 plays well with the FEZ. Maybe we could start an ugly robot contest? The more stray wires the better.

Had some trouble getting the embedded video to work so alternate is: the ugly autobot - YouTube

the ugly autobot - YouTube

3 Likes

@ idafez - Classic

(I think if you strip off the “&feature=youtu.be” from your link it will embed better)

My entry (Domino based):

In action (one motor was faster than the other, so it had a slight pull to one side):

1 Like

@ mhectorgato - Your autobot is quite impressive - I like the touch of hand hewed wood!

Thanks for the YouTube suggestion - it worked.

Awesome!

using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.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.Di10, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptNone);


    private static OutputPort TriggerPin = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di12);

    public static void Main()
    {
        EchoPin.OnInterrupt += new NativeEventHandler(port_OnInterrupt);
        EchoPin.DisableInterrupt();
        while (true)
        {
            Distance();
            //Debug.Print("distance = " + myDistance + " mm.");
            Thread.Sleep(1000);
        }
    }

    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 port_OnInterrupt(uint port, uint state, DateTime time)
    {
        if (state == 0) // falling edge, end of pulse
        {
            int pulseWidth = (int)time.Ticks - ticks;
            // valid for 20°C
            //int pulseWidthMilliSeconds = pulseWidth * 10 / 582;
            //valid for 24°C
            int pulseWidthMilliSeconds = (pulseWidth * 10 / (int)578.29);
            Debug.Print("Distance = " + pulseWidthMilliSeconds.ToString() + " millimètres.");
        }
        else
        {
            ticks = (int)time.Ticks;
        }
        EchoPin.ClearInterrupt();
    }

}

}

ให้คุณแก้ไขตรงที่

private static InterruptPort EchoPin = new InterruptPort((Cpu.Pin)FEZ_Pin.Digital.Di10, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptNone);

@ woraphon - Welcome to the forum. Are you trying to ask a question?

1 Like