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

Trying to get started with multiple digital inputs


  • Please log in to reply
1 reply to this topic

#1 theaccent

theaccent

    Member

  • Members
  • PipPip
  • 12 posts

Posted 03 July 2014 - 09:30 AM

Hi All,

 

Have just taken my Netduino Plus 2 out of it's packaging and looking to get started on my project, some background to start with... 

 

I have a home made projector lift which I'm looking to control with the Netduino.  The control will be via 2 methods, infrared relay and manual override.  Each method will have 3 controls each (Up, Stop and Down).  The outputs will be to solid state relays.

 

I'm having a bit of trouble getting started when I have multiple digital inputs, I've looked at the code examples for input/output/interrupt ports and they all show when you know which port/pin is explicitly being used.  What I'm looking at doing is saying right, an event has occurred, figure out which port/pin it is and then do something.

 

Do I set up an interrupt port passing a pin class that fired the event to a method, then in the method figure out which pin it was with a switch statement or do I have to set up a separate interrupt port for each of the pins I'm using?

 

Or am I thinking about this wrong?

 

Appreciate some feedback, help?



#2 ShVerni

ShVerni

    Advanced Member

  • Members
  • PipPipPip
  • 138 posts
  • LocationNew York, New York

Posted 04 July 2014 - 03:01 AM

Hello and welcome!
 
You're thinking about it correctly, and in fact either method would work. That is to say, you could set up an interrupt for each pin like so:

    public class Program
    {
        static InterruptPort pin1 = new InterruptPort(Pins.GPIO_PIN_D0, false, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeLow);
        static InterruptPort pin2 = new InterruptPort(Pins.GPIO_PIN_D1, false, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeLow);
        static InterruptPort pin3 = new InterruptPort(Pins.GPIO_PIN_D2, false, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeLow);

        public static void Main()
        {
            pin1.OnInterrupt += pin1_OnInterrupt;
            pin2.OnInterrupt += pin2_OnInterrupt;
            pin3.OnInterrupt += pin3_OnInterrupt;
            Thread.Sleep(Timeout.Infinite);
        }

        static void pin1_OnInterrupt(uint port, uint state, DateTime time)
        {
            // Do something with pin 1
        }

        static void pin2_OnInterrupt(uint port, uint state, DateTime time)
        {
            // Do something with pin 2
        }

        static void pin3_OnInterrupt(uint port, uint state, DateTime time)
        {
            // Do something with pin 3
        }

    }

You'll, of course, want to change the pins, Port.ResistorMode, and Port.InterruptMode to whatever you're using. You'll notice one of the parameters passed to the OnInterrupt method is port, which actually corresponds to the number of the port that triggered the event. So, you could have all the ports trigger to one event like so:

    public class Program
    {
        static InterruptPort pin1 = new InterruptPort(Pins.GPIO_PIN_D0, false, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeLow);
        static InterruptPort pin2 = new InterruptPort(Pins.GPIO_PIN_D1, false, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeLow);
        static InterruptPort pin3 = new InterruptPort(Pins.GPIO_PIN_D2, false, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeLow);

        public static void Main()
        {
            pin1.OnInterrupt += pin_OnInterrupt;
            pin2.OnInterrupt += pin_OnInterrupt;
            pin3.OnInterrupt += pin_OnInterrupt;
            Thread.Sleep(Timeout.Infinite);
        }

        static void pin_OnInterrupt(uint port, uint state, DateTime time)
        {
            switch (port)
            {
                case 39:
                    // Do something with pin 1
                    break;
                case 38:
                    // Do something with pin 2
                    break;
                case 3:
                    // Do something with pin 3
                    break;
            }           
        }
    }

Now, you'll notice the port number in the switch statement doesn't match the Pins.GPIO_PIN_## number, this is because it's the actual CPU pin number, i think. You can easily determine which pin will give you what port number by putting a debug statement in the above pin_OnInterrupt event like so:

        static void pin_OnInterrupt(uint port, uint state, DateTime time)
        {
            Debug.Print(port.ToString());           
        }

Then, you can just trigger each pin and record the proper port number before building your switch statement. You'll probably have to worry about debouncing, depending on your setup. You can find out more about debouncing, and interrupt ports in general and their alternatives, in this thread:

 

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

 

I hope that helps, and good luck!






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.