Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

HC-SR04 Ultrasonic Sensor Memory Problem

HC-SR04 Ultrasonic Sensor Memory

Best Answer Heroduino, 16 July 2013 - 08:06 AM

Hello Grant,

 

sorry. I had no try this. The other way is, before you return from Read() is clear the eventhandler

public static int Read(){   EchoPin.OnInterrupt += new NativeEventHandler(port_OnInterrupt);      EchoPin.DisableInterrupt();                         Distance();   Thread.Sleep(25);   EchoPin.OnInterrupt -= new NativeEventHandler(port_OnInterrupt);   return uDistance;}

Or you write a methode for allocate one time only the event handler.

 

Other Example

public static void SetEventInterupt(){      EchoPin.OnInterrupt += new NativeEventHandler(port_OnInterrupt);}public static int Read(){  EchoPin.DisableInterrupt();                         Distance();  Thread.Sleep(25);  return uDistance; }

And your main 'while' you write:

SetEventInterupt();while (true) {       lcd.Move(2, 0);       lcd.Show(Sonic.Read().ToString() + "cm ");       Thread.Sleep(1000); }

Or look on my blog and Translate my content. Well, I posted a example code. ^_^

http://meineweltinme...m-netduino.html

 

Heroduino

Go to the full post


  • Please log in to reply
5 replies to this topic

#1 stotech

stotech

    Advanced Member

  • Members
  • PipPipPip
  • 143 posts
  • LocationAustralia

Posted 16 July 2013 - 03:08 AM

Hi everyone,

 

I got some code that is working fantastic. But i found it on the forums and i don't really understand it completely. After running once a second for about twenty minutes my N+2 runs out of  memory though. I think it gets to big a queue up of interrupts built up. That's what i guess from this thread.

I really need to poll this sensor at, at least 1 Hz but preferably 2 or 4. But it runs out of memory. Can someone suggest changes to the code to allow for this. Like I said earlier, It is otherwise perfect.

 

Here is the class.

using System;using System.Threading;using Microsoft.SPOT;using Microsoft.SPOT.Hardware;using SecretLabs.NETMF.Hardware;using SecretLabs.NETMF.Hardware.Netduino;namespace Pulsator{    public class Sonic    {        public static int uDistance;        private static int ticks;        private static InterruptPort EchoPin = new InterruptPort(Pins.GPIO_PIN_D2, true, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeBoth);        private static OutputPort TriggerPin = new OutputPort(Pins.GPIO_PIN_D1, false);        public static int Read()        {            EchoPin.OnInterrupt += new NativeEventHandler(port_OnInterrupt);            EchoPin.DisableInterrupt();                                      Distance();                //Debug.Print("distance = " + myDistance + " mm.");                Thread.Sleep(25);                return uDistance;        }        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 * 1 / (int)578.29); //change 1 to 10 for mm                //Debug.Print("Distance = " + pulseWidthMilliSeconds.ToString() + " millimètres.");                uDistance = pulseWidthMilliSeconds;            }            else            {                ticks = (int)time.Ticks;            }            EchoPin.ClearInterrupt();        }    }}

Here is how I call it.

            while (true)            {                lcd.Move(2, 0);                lcd.Show(Sonic.Read().ToString() + "cm ");                Thread.Sleep(1000);            }

Thanks in advance.



#2 Heroduino

Heroduino

    Member

  • Members
  • PipPip
  • 14 posts
  • LocationGermany

Posted 16 July 2013 - 05:00 AM

Hello Grant,

 

if you used the Read() method, you allocate each time a new Event, but it dont clear it. Try this to the Methode.

public static int Read(){   if(EchoPin.OnInterrupt == null)   {      EchoPin.OnInterrupt += new NativeEventHandler(port_OnInterrupt);   }   EchoPin.DisableInterrupt();                         Distance();   //Debug.Print("distance = " + myDistance + " mm.");   Thread.Sleep(25);   return uDistance;}

Heroduino



#3 stotech

stotech

    Advanced Member

  • Members
  • PipPipPip
  • 143 posts
  • LocationAustralia

Posted 16 July 2013 - 06:49 AM

Thanks for the quick reply. I thought it was going to work but the first oninterrupt underlines and the error is

 

"the event 'microsoft.spot.hardware.nativeeventdispatcher.oninterupt can only appear on the left hand side of += or -="

 

if i change it to +=. it says that it can't convert void to bool?

 

Any other idea's. I'm way over my head?



#4 Heroduino

Heroduino

    Member

  • Members
  • PipPip
  • 14 posts
  • LocationGermany

Posted 16 July 2013 - 08:06 AM   Best Answer

Hello Grant,

 

sorry. I had no try this. The other way is, before you return from Read() is clear the eventhandler

public static int Read(){   EchoPin.OnInterrupt += new NativeEventHandler(port_OnInterrupt);      EchoPin.DisableInterrupt();                         Distance();   Thread.Sleep(25);   EchoPin.OnInterrupt -= new NativeEventHandler(port_OnInterrupt);   return uDistance;}

Or you write a methode for allocate one time only the event handler.

 

Other Example

public static void SetEventInterupt(){      EchoPin.OnInterrupt += new NativeEventHandler(port_OnInterrupt);}public static int Read(){  EchoPin.DisableInterrupt();                         Distance();  Thread.Sleep(25);  return uDistance; }

And your main 'while' you write:

SetEventInterupt();while (true) {       lcd.Move(2, 0);       lcd.Show(Sonic.Read().ToString() + "cm ");       Thread.Sleep(1000); }

Or look on my blog and Translate my content. Well, I posted a example code. ^_^

http://meineweltinme...m-netduino.html

 

Heroduino



#5 stotech

stotech

    Advanced Member

  • Members
  • PipPipPip
  • 143 posts
  • LocationAustralia

Posted 16 July 2013 - 11:18 AM

Thanks Heaps. You live up to your name. I'm very grateful.



#6 chevi99

chevi99

    Member

  • Members
  • PipPip
  • 19 posts

Posted 15 June 2014 - 10:27 PM

pls debug.print does not work. I don't know why because it doesn't give me any error and nothing happens too when i run the program







Also tagged with one or more of these keywords: HC-SR04, Ultrasonic, Sensor, Memory

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.