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

NEWBIE ON DIGITAL INPUT


  • Please log in to reply
9 replies to this topic

#1 RandallFlagg

RandallFlagg

    Member

  • Members
  • PipPip
  • 12 posts
  • LocationMANTOVA - ITALY

Posted 25 January 2011 - 06:02 PM

Hi all, i'm very newbie on Netduino and electronic. So i try to explain my problem. My hardware Netduino alimented by PC USB Rs232 shield Industrial Button. my goal : Intercept when user press the button reading the digital pin on netduino and send data to serial. my result : On serial communication i've got no problem. My main problem i how to realize the circuit between netduino and the external button without using a breadbord. I suppose i've to create an extern circuit to give power to the button then i've to pin the +3,3 V on arduino with the power, pin the gnd with the groud and insert the output from the button in my arduino pin but i'm really confused.....

#2 Chris Seto

Chris Seto

    Advanced Member

  • Members
  • PipPipPip
  • 405 posts

Posted 25 January 2011 - 07:23 PM

Check out the button tutorial on the Arduino site. http://www.arduino.c...Tutorial/Button

#3 RandallFlagg

RandallFlagg

    Member

  • Members
  • PipPip
  • 12 posts
  • LocationMANTOVA - ITALY

Posted 26 January 2011 - 07:15 AM

Check out the button tutorial on the Arduino site. http://www.arduino.c...Tutorial/Button


thank's for reply. I've seen this tutorial yesterday after posting my problem.
Only 1 question : When you pin the 5 V on arduino is it an input (you are giving 5v to arduino ) or it is an output (arduino gives you the 5 v ) ?

#4 Chris Seto

Chris Seto

    Advanced Member

  • Members
  • PipPipPip
  • 405 posts

Posted 26 January 2011 - 07:31 AM

You will want to use the 3.3V source on the Netduino. It is neither an input or an output, but a current source.

#5 RandallFlagg

RandallFlagg

    Member

  • Members
  • PipPip
  • 12 posts
  • LocationMANTOVA - ITALY

Posted 26 January 2011 - 09:16 AM

in this photo you can see my circuit. The 2 wires from button goes into pin 10 and into pin 5,5 V. (i've tried also to pin the AREF instead of 5,5 V) In this way the event extbutton = new InterruptPort(Pins.GPIO_PIN_D10, false, Port.ResistorMode.Disabled,port.InterruptMode.InterruptEdgeHigh); extbutton.OnInterrupt += new NativeEventHandler(bottoneesterno_OnInterrupt); trigger continuosly What is wrong?

Attached Files



#6 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 26 January 2011 - 09:21 AM

Hi Randall, Do you have a pull-down resistor in your switch circuit? Try to wire the switch to the GND pin and to D10 instead. Use the internal pull-up resistor ("ResistorMode.PullUp") when you create the InterruptPort. Does that work? Chris

#7 RandallFlagg

RandallFlagg

    Member

  • Members
  • PipPip
  • 12 posts
  • LocationMANTOVA - ITALY

Posted 26 January 2011 - 11:10 AM

Hi Randall,

Do you have a pull-down resistor in your switch circuit?

Try to wire the switch to the GND pin and to D10 instead. Use the internal pull-up resistor ("ResistorMode.PullUp") when you create the InterruptPort. Does that work?

Chris



WOW! not it works!
only problem is that sometimes events triggers 2 or 3 times .

I've tried also to not use interrupt mode but read directly button state (code below) and sees a little better.

So what is better using INTERRUPT or read BUTTON STATE?

Really thank for your patience!



OutputPort luce = new OutputPort(Pins.ONBOARD_LED, false);

bool buttonState = true;
byte counter = 49;
while (true)
{

if (buttonState != button.Read() )

{
if (buttonState == false)
{
inTray = new byte[3];

inTray[0] = counter ;
inTray[1] = 13;
inTray[2] = 10;
port.Write(inTray, 0, inTray.Length);
buttonState = true;
counter++ ;
if (counter == 59)
{ counter = 49; }
}
else
{
buttonState = false;
}


}


}

#8 PeterB

PeterB

    Member

  • Members
  • PipPip
  • 17 posts
  • LocationSydney, Australia

Posted 26 January 2011 - 12:33 PM

WOW! not it works!
only problem is that sometimes events triggers 2 or 3 times .


A physical problem with buttons/switches is something called DEBOUNCE. In essence the make-break cycle is not clean and instead of getting a single event it registers as multiple events. Look into filtering the input either in hardware or software. I saw a software option somewhere with interrupts.

But take everything I have said with a rock size chunk of salt as I have only had my Netduino for 2 days and still learning. Some of the experts I'm sure will set you right. Cheers and Happy Australia Day.

#9 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 26 January 2011 - 07:11 PM

Hi Randall, The second option in the InterruptPort constructor is "glitchFilter". If you set that to "true" it'll filter out most switch bouncing. If your switch bouncing is too strong for the glitch filter, I'd recommend creating a bit of software de-bouncing logic. Chris

#10 Fred

Fred

    Advanced Member

  • Members
  • PipPipPip
  • 302 posts
  • LocationUK

Posted 30 January 2011 - 12:20 PM

Debounce is a very common problem when dealing with the switches. Here's some code I used recently that does a couple of things.
1. Debounces by ignoring any further interrupts within 500ms. (Tune this if you need to catch quicker presses.)
2. Adds two events OnPress and OnLongPress. For long press it checks every 500ms and if the button's still down after 3s it fires OnLongPress.

using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using System.Threading;

namespace Fred.Useful
{
    /// <summary>
    /// A debounced button wired to an input pin.

    /// </summary>
    /// <remarks>
    /// Ignores clicks within 200ms
    /// Has simple OnPress and OnLongPress events (3s)
    /// /// </remarks>
    public class DebouncedButton
    {
        const int DEBOUNCE_TIME = 200;

        const int HOLD_SAMPLE_TIME = 500;
        const int HOLD_COUNT = 6;

        InterruptPort _button;
        DateTime _lastButtonPress = DateTime.Now;
        Timer _timer;
        public delegate void PressedDelegate();
        int _holdCounter;

        public DebouncedButton(Cpu.Pin pin)
        {
            _button = new InterruptPort(pin, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeLow);
            _button.OnInterrupt += button_Pressed;

            _timer = new Timer(timer_Fired, null, Timeout.Infinite, Timeout.Infinite);
        }

        public PressedDelegate OnPress;
        public PressedDelegate OnLongPress;

        private void button_Pressed(uint data1, uint data2, DateTime time)
        {
            // Debounce - ignore multiple presses within 200ms
            if (time < _lastButtonPress.AddMilliseconds(DEBOUNCE_TIME)) return;
            _lastButtonPress = time;
            _holdCounter = 0;

            // Set up timer for OnLongPress
            if (OnLongPress != null)
                _timer.Change(HOLD_SAMPLE_TIME, HOLD_SAMPLE_TIME);

            // Fire event
            if (OnPress != null)
                OnPress();

        }

        /// <summary>
        /// Check whether the button's still being held down
        /// </summary>
        /// <param name="state"></param>
        public void timer_Fired(object state)
        {
            if (_button.Read())
            {
                // Button released
                _timer.Change(Timeout.Infinite, Timeout.Infinite);
                return;
            }

            Debug.Print("Holding x" + _holdCounter.ToString());

            // Keep sampling. Button's not been held downlong enough

            if (++_holdCounter < HOLD_COUNT) return;

            // Fire event
            if (OnLongPress != null)
                OnLongPress();

            // Stop timer
            _timer.Change(Timeout.Infinite, Timeout.Infinite);
        }
    }
}





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.