Parallax Ping issue

Hi,
i’m trying to use the code found here: http://www.tinyclr.com/codeshare/entry/123 to read from a parallax ping sensor connected to digital pin D6 on the Cerebuino.

The problem i’m having is that the line never goes low while reading the echo pulse. The consequence of this is that the second while loop (line 76-79) is never exited.

What could be the issue here?

Is it low to start with?

I had some issues getting mine to work.

Can you try this code and see if it responds?


private static int ticks;
 
        private static InterruptPort EchoPin = new InterruptPort(Pins.GPIO_PIN_D6, true, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeBoth);
        private static OutputPort TriggerPin = new OutputPort(Pins.GPIO_PIN_D7, false);
 
        public static void Main()
        {
            EchoPin.OnInterrupt += new NativeEventHandler(port_OnInterrupt);
            EchoPin.DisableInterrupt();
            while (true)
            {
                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();
        }

And yes, this has the dreaded while (true) loop, but it was the most concise example (and I couldn’t find my thread with the fixed code in it… evidently I’ve been posting too much!)

Here’s the thread, I refactored the Gadgeteer code (which won’t help you in this case), but for more research: http://www.tinyclr.com/forum/topic?id=7954&page=1

Also, make sure you’ve got it plugged into the 5v power pin, not the 3.3V pin. that was part of my problem as well!

@ stevepresley - We are talking about different sensor here. It has only one pin.

It does not look like it is. to check this, i modified the code to add a read just before the first do/while loop and but a break inside that loop. the value for high=true:

i also tried another Parallax Ping sensor i have and i am seeing the same behavior…so unless i am very unlucky, i don’t think the sensor is damaged.

Show me your code please.

There isn’t much here.


    public partial class Program
    {
        ParallaxPing _ping = new ParallaxPing((Cpu.Pin)FEZCerbuino.Pin.Digital.D6);
        
        void ProgramStarted()
        {
            (new Thread(new ThreadStart(CheckDistance))).Start();
        }

        private void CheckDistance()
        {
            while (true)
            {
                Thread.Sleep(500);
                int distance = _ping.GetDistance(ParallaxPing.DistanceUnits.Inches);
                Debug.Print("Distance: " + distance.ToString());
                Thread.Sleep(500);
            }
        }
    }

OK. I gave my Cerbuino to a friend to test. I’ll try to get it back and will test it. Meanwhile try it on some other different pins.

D0 works (after 2 initial bad readings)! Nice!

I would still like to understand why d6 does not. I see from the schematics, these pins have slightly different characteristics (i.e. d0 = uart).

Sorry, I thought the ping had the same number of pins as the sensor I was using - it’s only 3 wires? Hmm.

hi Architect, did you ever get a chance to try this? still curious as to why D6 does not work on the cerebuino

Not yet :frowning: Didn’t get my Cerbuino back. I will try as soon as I get it back.