|
||
| Introduction | ||||
|---|---|---|---|---|
| There are several ways for the OOPic to read the state
of a switch that it has been connected to, but the simplest way is to
use the oDio1 Object.
When a switch is presses or flipped, the switch's contact points either come into contact or are separated resulting in a change in electrical state of the switch. This change of electrical state can be read by an oDio1 Object used in an OOPic application which can then cause the application to react to the switching action. |
||||
| Connecting the Hardware | ||||
In the diagram, two
different configurations are shown for connecting the
switch. You should take note that in each diagram, a resistor is connected to the
I/O Line and then to the opposite voltage reference that
the switch is connected to. The purpose of this resistor
is to determine the electrical state of the I/O Line when
the switch's contacts are not closed. This is needed
because when an I/O Line is configured as an input, and
it is not connected to anything, the electrical state
will float. This is to say that the state is undetermined
and could be either 1 or 0 at any given time and can even oscillate
between the two.In the active-low diagram, a switch is shown that is connected between the I/O Line and Ground. In this configuration, a Pull-Up resistor is connected to the I/O Line and +5 Volts. The Pull-Up resistor will keep the I/O Line in a logical high state (1) until the button is pressed at which time the I/O Line will go to a logical low state (0). This is the recommended configuration for switches. The cvPressed constant is used with this kind of configuration and the OOPic Object has a Pullup property that is used to turn on internal Pull-Up resistors on I/O Lines 8 - 15. In the active-high diagram, a switch is shown that is connected between the I/O Line and +5 Volts. In this configuration, a Pull-Down resistor is connected to the I/O Line and Ground. The Pull-Down resistor will keep the I/O Line in a logical low state (0) until the button is pressed at which time the I/O Line will go to a logical high state (1).
With any of these configurations, when the I/O Line is connected or pulled to +5 Volts, the logical state is high. This means that the oDio1's Value property will read 1. And when the I/O Line is connected or pulled to Ground, the logical state is low. This means that the oDio1's Value property will read 0. |
||||
| Configuring the oDio1 Object to read a switch | ||||
| In order for an application to use an oDio1 to read a
switch, the application must first declare that it needs
an oDio1 Object. The oDio1 Object can be named any
appropriate name. In this example it will be named
"S".
In the top portion of the program, type the following line of code:
After the oDio1 Object has been created, some properties must be set. First, the IOLine property must be set to the I/O Line that the switch is connected to. Second, the Direction property must be set to specify that the IOLine just set needs to be an input. The constant cvInput can be used here. NOTE: the Direction property must be set after the IOLine property has been set so that the Direction property knows which I/O line to set the direction for. In a sub procedure, type the following two lines of code:
After these three lines are executed, The Value property of the S Object will reflect the electrical state of the switch. If you are using one of the I/O Lines 8-15 in an active low switch configuration and would like to leave out the Pull-Up resistor, you can set the OOPic Object's Pullup property to 1. the constant cvTrue can be used here. In a sub procedure, type the following line of code:
After this line of code executes, the OOPic's internal Pull-Up resistors will be activated for I/O Lines 8-15. If your application is going to use this method, it is recommended that this be one of the first lines of code executed. |
||||
| Testing for the Switches State in Code | ||||
| If order for the application program to react to the
any switches that have been connected, code must be
written. The most common way to detect that a switch has been pressed is with an If statement. The If statement tests a condition, and if the condition is true it cause certain other statements within the application to be executed. In the following example, an If statement is used to test the state of a switch:
Another way to check for the state of a switch in code is to use the optional argument in a Do statement. The Do statement has several variations of optional argument that can be used. In the following example, the While argument is used:
|
||||
| Send mail and comments to: Savage Innovations. |