2
1
2
e
1
NOTE: Expressions are not
allowed as arguments on the BS1.
The range of the Pin argument on
the BS1 is 0 – 7.
2
2
e
2
2
e
5: BASIC Stamp Command Reference – TOGGLE
TOGGLE
2
2
TOGGLE Pin
sx
p
Function
Invert the state of an output pin.
• Pin is a variable/constant/expression (0 – 15) that specifies which
I/O pin to set high. This pin will be placed into output mode.
Explanation
TOGGLE sets a pin to output mode and inverts the output state of the pin,
changing 0 to 1 and 1 to 0.
In some situations TOGGLE may appear to have no effect on a pin's state.
For example, suppose pin 2 is in input mode and pulled to +5V by a 10k
resistor. Then the following code executes:
1
DIR2 = 0
PIN2 = 0
DEBUG ? PIN2
TOGGLE 2
DEBUG ? PIN2
--or--
2
2
DIR2 = 0
sx
p
OUT2 = 0
DEBUG ? IN2
TOGGLE 2
DEBUG ? IN2
The state of pin 2 doesn't change; it's high (due to the resistor) before
TOGGLE, and it's high (due to the pin being output high) afterward. The
point is that TOGGLE works on the OUTS register, which may not match
the pin's state when the pin is initially an input. To guarantee that the state
actually changes, regardless of the initial input or output mode, do this:
1
PIN2 = PIN2' Make output driver match pin state.
TOGGLE 2 ' Then toggle.
--or—
2
2
OUT2 = IN2 ' Make output driver match pin state.
p
sx
TOGGLE 2 ' Then toggle.
BS1
BS2
BS2e BS2sx BS2p
' Pin 2 in input mode.
' Pin 2 output driver low.
' Show state of pin 2 (1 due to pullup).
' Toggle pin 2 (invert PIN2, put 1 in DIR2).
' Show state of pin 2 (1 again).
' Pin 2 in input mode.
' Pin 2 output driver low.
' Show state of pin 2 (1 due to pullup).
' Toggle pin 2 (invert OUT2, put 1 in DIR2).
' Show state of pin 2 (1 again).
BASIC Stamp Programming Manual 2.0c • www.parallaxinc.com • Page 327
Need help?
Do you have a question about the BASIC Stamp 2e and is the answer not in the manual?