We now know how to create an object that will connect to an output pin. We can set the state of the object and also read back the state that we have set. Next, we need to create an object to interact with an input pin connected to the switch.
InputPort switchInput = new InputPort(switchPin, false, Port.ResistorMode.PullUp);
The construction process for input pins is very similar to that for output pins, except that the constructor needs more information.
The first parameter identifies the pin to be used; the second specifies whether a glitch filter is to be used on this signal. A glitch filter removes stray pulses from an input signal. Such pulses can be caused by electrical noise and might make our lamp flicker on and off. The third parameter is a value that indicates the mode in which the pin is to be configured. The ResistorMode type is enumerated, which means that it contains a particular set of values. In this case, each of the values represents a way in which the input pin can be configured. These values are described here:
ResistorMode.PullUp A resistor inside the hardware is set to pull the pin up. If nothing is connected to the pin, it will produce the value high. This means that a switch wired between the pin and ground (0 volts) will pull the pin low when it connects the two together, so the port will read false when the switch is closed.
ResistorMode.PullDown This is the reverse of the PullUp arrangement, in that the pin is pulled low by an internal resistor. Any switch must be connected between the high supply rail and the pin so that when it is closed, the port will read high.
ResistorMode.None In this configuration, no internal resistors are configured on the input. This means that the external hardware must provide them. If this mode is selected and the pin is left without a pull-up/pull-down resistor, it will generate noisy results because any voltages induced in the pin may be interpreted as signals.
Once we have our input port, we can read it by calling the method it provides to get the current status.
if (switchInput.Read()) Debug.Print("input high");
This statement reads the port and prints the message if the input is being held high. We now know enough to create our first complete program.