Interrupts and Events Continue Firing - General Discussion - Netduino Forums
   
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

Interrupts and Events Continue Firing

events interrupts photocell

  • Please log in to reply
10 replies to this topic

#1 Chuckles

Chuckles

    Advanced Member

  • Members
  • PipPipPip
  • 40 posts

Posted 14 November 2013 - 09:26 PM

Hey All. I have a Netduino Plus 2 with a Sparkfun photocell sensor wired up to Analog1 with a 10k resistor in the mix (equivalent to what you see in http://bildr.org/201...sistor-arduino/).

 

In my program.cs, I can drop a breakpoint at the event that is fired when the interrupt occurs, and after the first time the sensor interrupt occurs, the event is fired "forever", it does not ever stop.

 

After the event fires, I am logging the datetime ticks that the event happened, but then I do not want the event to fire again.

 

What is the proper programming pattern to accept the interrupt/event one time?

 

 

Thanks.

 

 



#2 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 15 November 2013 - 06:16 AM

Hi Chuckles, Which interrupt edge setting are you using in your constructor? Also...you can .DisableInterrupts in your event handler, to prevent them from continuing to be called after the first event handler. You may also need to set some sort of flag, in case there are already events queued up. Chris

#3 Chuckles

Chuckles

    Advanced Member

  • Members
  • PipPipPip
  • 40 posts

Posted 15 November 2013 - 02:22 PM

Chris, thanks for taking a look at this. I am putting in a big chunk of the program in the hopes that you can help me think though the right way to do eventing. This is code for a model car race track; a switch is closed and a race starts, when a car passes over a light sensor the time is logged, and if nothing happens in 10 seconds then the time is logged anyway and then a new race heat can begin.

// extraneous code removedpublic class Program{    private static InterruptPort _laneSensor = null;    private static InterruptPort _buttonOnboard ;    private static InterruptPort _gateSwitch = null;    private static Timer _heatTimer;   	 public static void Main()        {            OnboardHardwarePrep();            SensorsReset();            StartGateSwitchPrep();            TimeContainerPrep();            Thread.Sleep( Timeout.Infinite );        }                private static void OnboardHardwarePrep()        {            _ledOnboard = new OutputPort( Pins.ONBOARD_LED, false );            _buttonOnboard =                new InterruptPort ( Pins.ONBOARD_SW1, true , Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeBoth );            _buttonOnboard.OnInterrupt += new NativeEventHandler ( buttonOnboard_Fire );            _ledOnboard.Write ( false );        }                            private static void SensorsReset()        {            try            {                _laneSensor = new InterruptPort( Pins.GPIO_PIN_A1, false, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeBoth );                _laneSensor.OnInterrupt += new NativeEventHandler( laneSensor_Fire );            }            catch (Exception xxx)            {                Debug.Print( xxx.ToString() );                // Why do I only get the text "System.Exception" and not rich problem info?            }        }                private static void StartGateSwitchPrep()        {            _gateSwitch = new InterruptPort( Pins.GPIO_PIN_D12, false, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeBoth );            _gateSwitch.OnInterrupt += new NativeEventHandler( gateSwitch_Fire );        }                        static void gateSwitch_Fire( uint data1, uint data2, DateTime time )        {            RaceHeatBegin();        }                            static void RaceHeatBegin()        {            _ledOnboard.Write( true );            TimerCallback timerCallback = new TimerCallback( Tick );            _heatTimer = new Timer( timerCallback , null , 9999 , 9999 );            _laneTimeBegin = Utility.GetMachineTime().Ticks;            _ledOnboard.Write( false );        }                                static public void Tick( Object stateInfo )        {            Debug.Print( "Timer elapsed for the heat (9.999 seconds)." );            buttonOnboard_Fire( 0, 0, DateTime.Now );        }                static void buttonOnboard_Fire( uint data1, uint data2, DateTime time )	    {            try            {                if (null != _heatTimer)                {                    _heatTimer.Dispose();                }            }            catch (Exception xxx)            {                Debug.Print( xxx.Message );            }            TimingLog();            _ledOnboard.Write ( false );       		 }	   	             private static void MessagePrint( string messageToSay )        {            Debug.Print( messageToSay );            buttonOnboard_Fire( 0, 0, DateTime.Now );        }                        static void laneSensor_Fire( uint data1, uint data2, DateTime time )        {            _laneSensor.OnInterrupt -= laneSensor_Fire;            _laneTimesEnd[1] = Utility.GetMachineTime().Ticks;            MessagePrint( "Lane 1 : " + _laneTimesEnd[1] );        }            }

Also, the Micro Framework appears to throw and Exception if a property is assigned to an Interrupt port more than once (a tactic I tried in order to "reset" the hardware). Is that as-designed behavior?

 

Thanks.



#4 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 16 November 2013 - 12:36 PM

In my program.cs, I can drop a breakpoint at the event that is fired when the interrupt occurs, and after the first time the sensor interrupt occurs, the event is fired "forever", it does not ever stop.

 
I think the photo-resistor circuit is not really suitable for this scenario: at first, it has [relatively] long response time, which is not good for time measurement, and secondly, with resistance of ~10 k? and another 10 k? resistor you have voltage divider that produces voltage in the area of undefined logic levels - i.e. 0.5*VDD, while typical thresholds are <0.3*VDD for logic 0 and >0.7*VDD for logic 1; this can cause the input to oscillate or have 'strange' behavior.
 
I would recommend you to first determine behavior of the photo-resistor under conditions of your system. This means connecting it to an analog input and dump voltage readings while passing the car, varying ambient light etc. From that you should be able to determine voltage threshold that corresponds to detection of car passing. Then you can calculate the value of resistor needed to construct voltage divider for digital input (interrupt). I would also consider adding a Schmitt trigger logic gate to the input to ensure proper handling of slow edges.

Personally, I would use a photo-transistor instead, it is easier to use and it will work much better.
 

Also, the Micro Framework appears to throw and Exception if a property is assigned to an Interrupt port more than once (a tactic I tried in order to "reset" the hardware). Is that as-designed behavior?

Yes, this is by design. You have to Dispose() the port before it can be instantiated again.



#5 Chuckles

Chuckles

    Advanced Member

  • Members
  • PipPipPip
  • 40 posts

Posted 18 November 2013 - 02:53 AM

  Personally, I would use a photo-transistor instead, it is easier to use and it will work much better.  

 

Thanks for the detailed response. I have re-evaluated what I am trying to do... And now am shopping for "photo transistors". My typical part suppliers have been Adafruit and Sparkfun, neither of which appears to carry what you are talking about. Do you have a vendor recommendation?

 

 

Thanks!



#6 Dr Who

Dr Who

    Advanced Member

  • Members
  • PipPipPip
  • 261 posts
  • LocationNYC

Posted 18 November 2013 - 04:34 AM

Well yes.

Mouser. Also Digi-Key. And then Jameco.

 

Be warned Mouser loves hobbyists. Digi-Key doesn't mind them. And Jameco is still trying to figure them out. In the case of Jameco, should you decide to buy something from them, be prepared to buy a lot of items you might need. Even if that need is months to years later.



Doctor Who
"This signature does not exist!"

#7 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 18 November 2013 - 09:15 AM

The closest thing at SparkFun is Infrared Emitters and Detectors, but it is expensive and has very limited range (you can salvage such pair from an optical mouse). There are many manufacturers that make photo-transistors, you should be able to get them at your local electronics store.

 

As always, the selection depends... I am not exactly sure how you plan to construct the optical detection system, personally I would go for LED and matching photo-transistor - 'matching' means selecting one that is sensitive to LED's light (obviously). If you can place photo-transistor at location, where it has no or little ambient light that could affect it, you can select parts that work for visible light (red, green, etc.). Otherwise, it is better to use infra-red LED and infra-red photo-transistor, which is more immune to ambient [visible] light changes, of course except direct sunlight, which contains intensive infra-red light - usually, modulation is used to compensate for sunlight disturbances, exactly the same way as in TV remote controls (the LED is driven by ~40 kHz PWM signal ("modulation"), and there is a demodulator connected after photo-detector).

 

Incidentally, I made a prototype of lap timer circuit for slot car (Carrera Go) long time ago, I placed IR photo-transistor and IR LED around the slot at the bottom of the plastic track part (required a little bit of drilling on the sides of the slot), so the light from LED was interrupted by car's guide pin. I used SFH309-5 photo-transistor and (I think) L-934F3C IR LED; 3 mm diameter due to space constraints, I got them at local electronic store (GME, here in Czech Republic). I did not need to use modulation, because there was virtually no way for sunlight to disturb the IR light beam from LED to the photo-transistor.

 

Additional tips for IR part selection:

  • There are two almost standard IR wavelengths: ~880 nm and ~940 nm. IR LEDs are made to have peak emission at that wavelength, relatively narrow (see Relative Spectral Emission vs. wavelength chart in the device datasheet). IR photo-transistors can be tuned to have max sensitivity at that wavelength too, but usually it is better to get a more generic one - e.g. SFH309-5 has about >80% sensitivity for whole 700 - 1000 nm range, peak at 900 ± 50 nm (see Relative Spectral Sensitivity vs. wavelength chart in the datasheet). So it will work well enough for basically any IR LED.
  • For optical detectors, LEDs with narrow beam are better - choose one with smaller 'angle' (also listed as 'Half Ange', ? etc.). Usually around 20°. However, there will be little difference at small distances.
  • There are two IR LED power classes: "normal" and "power" - for small distances "normal" is enough (IF = ~10..20 mA), power LEDs (TV remotes, long distance sensing) can require >100 mA drive current (i.e. cannot be driven directly from a microcontroller pin, an additional transistor is needed).

 

Alternatively, there are reflective sensors, such as QRE1113, which could be mounted for example on the track and it could detect a passing car that has small mirror (reflective tape) attached at the bottom. This particular sensor has detection distance only about 1 mm, which is probably not enough to reach the car, but you get the idea...



#8 Chuckles

Chuckles

    Advanced Member

  • Members
  • PipPipPip
  • 40 posts

Posted 18 November 2013 - 03:21 PM

Posted Image

 

 

 

For this kind of derby race, cars run on top of wood guides. My plan was to drill small holes in a section of the track (image shows pink dots where holes could go), place the sensors there, and detect the shadow of the car as it passes over the holes. The crowd watching the race does not have an obscured view of the finish line. Cars have strict requirements about weight and dimensions and add-ons, so they cannot have anything added to them like reflective tape.There is approximately 3mm between the bottom of the car and the wood guide, so my plan was to drill small holes in each slot, embed the sensor in the hole, and run wiring though the hole and under the bottom of the track.

 

I thought that a light sensor would fit these requirements the best, detect when a shadow is cast over a sensor and mark the time.

 

As you can see, I am now having trouble with the detection part....

 

If I went with https://www.sparkfun.com/products/246 , what other hardware would I need to go with it? The video at seems to indicate it can accurately/quickly detect the difference between light and shadow.

 

I am also looking through stuff at Mouser (http://www.mouser.co...nsistor&FS=True) to get more clue.

 

All newbie help is very much appreciated!

 

Thanks.



#9 Chuckles

Chuckles

    Advanced Member

  • Members
  • PipPipPip
  • 40 posts

Posted 22 November 2013 - 02:46 AM

I am still digging....

 

I saw this tutorial : http://bildr.org/201...ensors-arduino/ and it is leading me to believe that I can get this to work:

 

https://www.sparkfun.com/products/246

 

the same as this :

 

http://www.mouser.co...pb%2bGHLi3qmps=

 

Is that true? The track is running indoors in a US school gymnasium (lights are fluorescent and 15+ meters in the air) so I think there should be no IR interference.

 

All comments welcomed, I don't want to buy the wrong sensors twice!  :unsure:



#10 Chuckles

Chuckles

    Advanced Member

  • Members
  • PipPipPip
  • 40 posts

Posted 26 November 2013 - 02:37 AM

...bump...

 

Anyone?



#11 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 26 November 2013 - 08:12 AM

https://www.sparkfun.com/products/246 the same as this : http://www.mouser.co...W6pb+GHLi3qmps=

Yes, in principle they are the same. QRD1114 contains IR photo-transistor and LED. It is designed for reflective sensing, it has peak sensing distance about 25 mils (around 0.5 cm). It is possible to take out IR photo-transistor and LED from the sensor casing (at least in the batch I have, your mileage may wary).





Also tagged with one or more of these keywords: events, interrupts, photocell

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.