NCIR
07
.:Button Pressing:.
.:Pushbuttons:.
(NEDX) .:Netduino Experimentation Kit:. (NEDX)

What We're Doing
Up to this point we have focused entirely on outputs, time to get our Netduino to listen, watch and feel. We'll start with a simple pushbutton. Wiring up the pushbutton is easy. There is one component, the pull up resistor, that might seem out of place. This is included because a Netduino doesn't sense the same way we do (ie button pressed, button unpressed). Instead it looks at the voltage on the pin and decides whether it is HIGH or LOW. The button is set up to pull the Netduino's pin LOW when it is pressed, however, when the button is unpressed the voltage of the pin will float (causing occasional errors). To get the Netduino to reliably read the pin as HIGH when the button is unpressed, we add the pull up resistor.
(note: the first example program uses only one of the two buttons)


The Circuit
The Parts
NCIR-07
Breadboard Sheet
x1
2 Pin Header
x4
Pushbutton
x2
Wire
10k Ohm Resistor
Brown-Black-Orange
x2
560 Ohm Resistor
Green-Blue-Brown
x1
Red LED
x1


Schematic

 

Resources
.:download:.
breadboard layout sheet
http://nedx.org/NBLS07

 

Code (no need to type everything in just)
Download the Code from ( http://nedx.org/CODE07 )
(and then copy the text and paste it into an empty Netduino Sketch)

//Libraries that our program uses
using System;                               //use base classes                            
using System.Threading;                     //use threading classes        
using Microsoft.SPOT;                       //use smart personal objects technology classes
using Microsoft.SPOT.Hardware;              //use hardware related SPOT classes
using SecretLabs.NETMF.Hardware;            //use Secret Labs hardware framework
using SecretLabs.NETMF.Hardware.Netduino;   //use the Netduino specific classes

namespace NCIR07                    /// Define the namespace we are in ///
{
    public class Program            /// Define any variables used in the program and subrout
                    //ines after Main()///
    {

        static InputPort button1 = new InputPort(Pins.GPIO_PIN_D2, false, Port.ResistorMode.
                    //Disabled);  //define pin 2 as an input for button #1
        static InputPort button2 = new InputPort(Pins.GPIO_PIN_D3, false, Port.ResistorMode.
                    //Disabled);  //define pin 3 as an input for button #2
        static OutputPort led = new OutputPort(Pins.GPIO_PIN_D13, false);                   
                    //            //define our LED as being connected to pin 13 

        public static void Main()   /// The Main loop (run at power up) ///
        {
                                    /// Setup Portion of Program, runs once at startup ///
            while (true)            /// Do Forever ///
            {
                turnLEDOnWithButton();             //Turns the LED on when button one is pre
                    //ssed and off when not
                //turnLEDOnOffWithButtons();       //Turns the LED on when button 1 is press
                    //ed and off when button 2 is pressed
            }                       /// Close Forever Loop ///  
        }                           /// Close the Main() Loop ///

        /// 
        /// If button 1 is pressed the light is turned on if it isn't the light is turned of
                    //f
        /// 

        static void turnLEDOnWithButton()
        {
            //a false condition means the button is being pressed
            if (button1.Read())   //Checks if the button is pressed
            {
             led.Write(false);    //If button 1 isn't pressed turn the light off
            }
            else
            {
             led.Write(true);    //If button 1 is pressed turn the light on
            }
        }

        /// 
        /// If button 1 is pressed the light is turned on. 
        /// If button 2 is pressed the light is turned off.
        /// 

        static void turnLEDOnOffWithButtons()
        {
         //a false condition means the button is being pressed
         if (button1.Read() == false) led.Write(true);      //If button 1 is pressed turn th
                    //e LED on
         if (button2.Read() == false) led.Write(false);     //If button 2 is pressed turn th
                    //e LED off
       }

    }                               /// Close the Program Loop ///
}                                   /// Close the Namespace Loop ///

 

Not Working? (3 things to try)
 

 
Light Not Turning On
The pushbutton is square and because of this it is easy to put it in the wrong way. Give it a 90 degree twist and see if it starts working.
 
Light Not Fading
A bit of a silly mistake we constantly made, when you switch from simple on off to fading remember to move the LED wire from pin 13 to pin 9.
 
Underwhelmed?
No worries these nciruits are all super stripped down to make playing with the components easy, but once you throw them together the sky is the limit.
 
 

Making it Better?
 
On button off button:
The initial example may be a little underwhelming (ie. I don't really need an Netduino to do this), let's make it a little more complicated. One button will turn the LED on the other will turn the LED off. Change the code after while (true) to:

//turnLEDOnWithButton();
turnLEDOnOffWithButtons();


Debug the program to your board, and start toggling the LED on and off.

Fading up and down:
Lets use the buttons to control an analog signal. To do this you will need to change the wire connecting the LED from pin 13 to pin 9.

Next copy and paste the code from:
http://nedx.org/CODE07MB
Into a blank Netduino project

Now when you press button 1 the light will increase in brightness, and when you press button 2 it will decrease. To see how this is done, look at the comments included with the code.

Changing fade speed:
If you would like the LED to fade faster or slower, there is only one line of code that needs changing;
Thread.Sleep(10); ----> Thread.Sleep(new #);
To fade faster make the number smaller, slower requires a larger number.