User Tools

Site Tools

한국어

cubloc:switch_input:index

Switch Input

Let’s say you are developing some kind of machine controlled by a Cubloc. The first thing you need is a user interface. Our task today is to build a machine that will receive input from a switch and process it to perform a task.

We will make a “start” and “stop” button that will turn a lamp on and off.

As you can see above, ports P0 and P4 will be connected to a pull-down resistor (resistor attached to ground). The CB220 will read these switches as logic-low or “off” when the switch is not pressed. To find out if these switches are pressed or unpressed, we can use Cubloc BASIC command In.

Const Device = cb220
Dim a As Byte
Do
  If In(0) = 1 Then a = 1
  If In(4) = 1 Then a = 0
  Out 14,a
Loop

When the switch is pressed, a “bouncing” effect occurs from the switch’s mechanical spring.

The above picture shows how bouncing can confuse Cubloc controller with alternating high and low signal levels. To get rid of this bouncing effect, a capacitor and resistor can be added to filter it out.

A simpler method is to use the KeyInH command instead of In which will remove the bouncing effect through software.

Const Device = cb220
Dim a As Byte
Do
  If KeyInH(0,20) = 1 Then a = 1
  If KeyInH(4,20) = 1 Then a = 0
  Out 14,a
Loop

The 2nd parameter of KeyInH(0, 20) a delay before reading the input so the signal bounce has time to settle. This delay is called the debouncing time. The software will wait for 20ms before reading the input.

In industrial environments, there can be a lot of electromagnetic noise which can affect switch signals. To prevent this noise from affecting the circuit, a circuit diagram similar to one shown below can be used. Using a photocoupler, the user is able to raise the voltage and minimize the effect the noise has on the switch input.

cubloc/switch_input/index.txt · Last modified: 2016/04/14 11:07 (external edit)