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

Event wired to AnalogInput?

PWM potentiometer AnalogInput

Best Answer Chris Walker, 11 January 2013 - 04:16 PM

Hi dustmouse, You could also use a timer to launch an event every ## ms if you wanted. And then disable that timer when you want to stop the LED. Personally, I would create a thread to blink the LED and have a class-wide "_ledBlinkDisabled" variable. Then you simply set that variable to true when you want the LED to stop blinking. We do this sometimes in applications with sync locks and two variables and it works consistently well. Chris Go to the full post


  • Please log in to reply
11 replies to this topic

#1 dustmouse

dustmouse

    Advanced Member

  • Members
  • PipPipPip
  • 31 posts
  • LocationEdgewater, CO

Posted 09 January 2013 - 06:25 AM

Is there a way to wire an AnalogInput to an event so that when the physical input changes, it raises an event?  Like for an LED dimmer.  I'm hoping for something similar to InterruptPort.OnInterrupted?  I know that you can accomplish the desired behavior in a while(true) loop, but I don't want it to be trapped in a loop forever - unless I can stick it on a different thread...

 

If there isn't something out-of-the-box, can I create my own custom event that is wired to the AnalogInput hardware (without my brain melting)?  So far, my experience with ICs is making the onboard LED turn on and off with the onboard button, so I'm still very wet behind the ears.


Check out my Netduino projects on GitHub.


#2 Nevyn

Nevyn

    Advanced Member

  • Members
  • PipPipPip
  • 1072 posts
  • LocationNorth Yorkshire, UK

Posted 09 January 2013 - 07:31 AM

Dustmouse,

 

Welcome to the community.

 

 

Is there a way to wire an AnalogInput to an event so that when the physical input changes, it raises an event?

 

If there isn't something out-of-the-box, can I create my own custom event that is wired to the AnalogInput hardware (without my brain melting)?

 

Currently you have to poll the analogue ports when you want to take a reading.  You could write your own class which polled the ports on a regular basis and then generated an interrupt if the difference between the last reading and the current one was outside of a certain tolerance.  Would be an interesting little software project.

 

Regards,

Mark


To be or not to be = 0xFF

 

Blogging about Netduino, .NET, STM8S and STM32 and generally waffling on about life

Follow @nevynuk on Twitter


#3 JerseyTechGuy

JerseyTechGuy

    Advanced Member

  • Members
  • PipPipPip
  • 870 posts

Posted 09 January 2013 - 01:13 PM

Is there a way to wire an AnalogInput to an event so that when the physical input changes, it raises an event?  Like for an LED dimmer.  I'm hoping for something similar to InterruptPort.OnInterrupted?  I know that you can accomplish the desired behavior in a while(true) loop, but I don't want it to be trapped in a loop forever - unless I can stick it on a different thread...

 

If there isn't something out-of-the-box, can I create my own custom event that is wired to the AnalogInput hardware (without my brain melting)?  So far, my experience with ICs is making the onboard LED turn on and off with the onboard button, so I'm still very wet behind the ears.

 

Check out this thread it may be what you are looking for.

 

http://forums.netdui...interrupt-port/



#4 dustmouse

dustmouse

    Advanced Member

  • Members
  • PipPipPip
  • 31 posts
  • LocationEdgewater, CO

Posted 11 January 2013 - 03:13 PM

Dustmouse,

 

Welcome to the community.

 

 

 

Currently you have to poll the analogue ports when you want to take a reading.  You could write your own class which polled the ports on a regular basis and then generated an interrupt if the difference between the last reading and the current one was outside of a certain tolerance.  Would be an interesting little software project.

 

Regards,

Mark

 

Thanks, Mark.  I like your polling idea.  I'd like to give that a shot.  

 

I actually haven't picked up a potentiometer yet - just got a little ahead of myself in the Getting Started with Netduino book  :unsure: .  


Check out my Netduino projects on GitHub.


#5 dustmouse

dustmouse

    Advanced Member

  • Members
  • PipPipPip
  • 31 posts
  • LocationEdgewater, CO

Posted 11 January 2013 - 03:18 PM

Check out this thread it may be what you are looking for.

 

http://forums.netdui...interrupt-port/

 

Thanks, Dave.  I don't actually have a potentiometer yet, so I went off of that example to create an event that would let an LED blink until it received an interrupt request.  

 

I made a Cycled event on the LED that was raised after each blink iteration, and I had a button subscribe to the event.  The only issue was that you would have to make sure you were holding the button down until the next time the event was raised, which wasn't a problem on fast blinking, but wasn't very elegant on slow blinking.

 

When I get a pot, I'll revisit the example in your link.


Check out my Netduino projects on GitHub.


#6 dustmouse

dustmouse

    Advanced Member

  • Members
  • PipPipPip
  • 31 posts
  • LocationEdgewater, CO

Posted 11 January 2013 - 03:33 PM

I know in standard .NET programming, it is generally frowned upon to use Thread.Abort() to stop a thread because you don't know what state it will be in when it is aborted.  But when working with basic components in the Micro Framework, is it as much of an issue?

 

I created a blinking LED that when set to 'on', blinks ad infinitum on a separate thread.  When set to 'off' it aborts the thread, stopping the blinking.  The hardware behavior is correct.  And the button doesn't have to worry about interrupting the blinking itself, it just turns the LED on and off.  

 

The only reason I'm using a separate thread and aborting is because the only way I've found to create blinking behavior in an LED is by turning it on/off in an infinite loop.  I don't want to use a count down (no matter how big) to turn it off - but have it keep blinking until an input component like a button turns it off.  Is there a better way of accomplishing this?  


Check out my Netduino projects on GitHub.


#7 Nevyn

Nevyn

    Advanced Member

  • Members
  • PipPipPip
  • 1072 posts
  • LocationNorth Yorkshire, UK

Posted 11 January 2013 - 04:12 PM

The only reason I'm using a separate thread and aborting is because the only way I've found to create blinking behavior in an LED is by turning it on/off in an infinite loop.  I don't want to use a count down (no matter how big) to turn it off - but have it keep blinking until an input component like a button turns it off.  Is there a better way of accomplishing this?  

You could always use a Timer.  The code in that post uses a GPIO pin for output but these is no reason that could not be the onboard LED.

 

Regards,

Mark


To be or not to be = 0xFF

 

Blogging about Netduino, .NET, STM8S and STM32 and generally waffling on about life

Follow @nevynuk on Twitter


#8 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 11 January 2013 - 04:16 PM   Best Answer

Hi dustmouse, You could also use a timer to launch an event every ## ms if you wanted. And then disable that timer when you want to stop the LED. Personally, I would create a thread to blink the LED and have a class-wide "_ledBlinkDisabled" variable. Then you simply set that variable to true when you want the LED to stop blinking. We do this sometimes in applications with sync locks and two variables and it works consistently well. Chris

#9 dustmouse

dustmouse

    Advanced Member

  • Members
  • PipPipPip
  • 31 posts
  • LocationEdgewater, CO

Posted 11 January 2013 - 08:39 PM

You could always use a Timer.  The code in that post uses a GPIO pin for output but these is no reason that could not be the onboard LED.

 

Regards,

Mark

 

Okay, I'll try out the timer approach.  Somehow I missed that when I first looked at the blog.  Selective reading I guess...


Check out my Netduino projects on GitHub.


#10 dustmouse

dustmouse

    Advanced Member

  • Members
  • PipPipPip
  • 31 posts
  • LocationEdgewater, CO

Posted 11 January 2013 - 08:43 PM

Hi dustmouse, You could also use a timer to launch an event every ## ms if you wanted. And then disable that timer when you want to stop the LED. Personally, I would create a thread to blink the LED and have a class-wide "_ledBlinkDisabled" variable. Then you simply set that variable to true when you want the LED to stop blinking. We do this sometimes in applications with sync locks and two variables and it works consistently well. Chris

 

Thanks, Chris.  So if I understand what you're suggesting, I would still use a separate thread for the blinking, but the LED behavior can be changed by setting the _ledBlinkDisabled value.  

 

If _ledBlinkDisabled is true, then does it just receive a constant high voltage, displaying solid light?  

 

Would you still need to abort the thread to kill the blinking?


Check out my Netduino projects on GitHub.


#11 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 12 January 2013 - 09:01 AM

If _ledBlinkDisabled is true, then does it just receive a constant high voltage, displaying solid light?     Would you still need to abort the thread to kill the blinking?
Basically, every time your background thread changed the state of the LED...it would watch the blinking disabled flag. If the flag was set to true, you could either [a] simply set the LED to off for that blink cycle or return from the function and exit that thread. It depends on how you prefer to restart blinking (i.e. by simply setting the flag to false or by starting up the thread again). Chris

#12 dustmouse

dustmouse

    Advanced Member

  • Members
  • PipPipPip
  • 31 posts
  • LocationEdgewater, CO

Posted 12 January 2013 - 04:43 PM

Basically, every time your background thread changed the state of the LED...it would watch the blinking disabled flag. If the flag was set to true, you could either [a] simply set the LED to off for that blink cycle or return from the function and exit that thread. It depends on how you prefer to restart blinking (i.e. by simply setting the flag to false or by starting up the thread again). Chris

 

Okay, that is way more cleaner than aborting the thread.  Thanks!


Check out my Netduino projects on GitHub.






Also tagged with one or more of these keywords: PWM, potentiometer, AnalogInput

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.