On and off signals
A simpe use of a digital output pin is to write a steady high or low signal. This might be used to turn something connected to the pin either on or off.
On signal
To send an on signal you write a true
value in ||pins:digital write pin||
to make the pin output go to the high level.
pins.A1.digitalWrite(true)
The voltage output at the pin is set to the high level, near 3.3 v.
Off signal
To set an output pin to low you write out a false
value in ||pins:digital write pin||
.
pins.A1.digitalWrite(false)
In this example, the output level on pin A1 is switched to high when button A is pressed and it’s switched to low when button B is pressed:
pins.A1.digitalWrite(false)
input.buttonA.onEvent(ButtonEvent.Click, function () {
pins.A1.digitalWrite(true)
})
input.buttonB.onEvent(ButtonEvent.Click, function () {
pins.A1.digitalWrite(false)
})
Experiment: Send yourself an on and off signal
Setup:
- Connect an alligator clip lead to the A1 pin.
- Connect the other end of the lead to the A2 pin.
- Download the following code to the board:
input.buttonA.onEvent(ButtonEvent.Click, function () {
pins.A1.digitalWrite(true)
})
input.buttonB.onEvent(ButtonEvent.Click, function () {
pins.A1.digitalWrite(false)
})
pins.A1.digitalWrite(false)
forever(function () {
if (pins.A2.digitalRead()) {
light.setAll(0xff0000)
} else {
light.clear()
}
})
Test: Press button A to output an “on” signal at pin A1 and press button B to output an “off” signal.
Result: The pixels show red when button A is pressed and then will go off when button B is pressed.
Experiment: Turn on and off an your own LED
Setup:
- Connect the anode lead (+) of the LED to on end of an output resistor with alligator clip lead.
- Connect the other end of the output resistor to the A2 pin.
- Connect the cathode (-) lead of the LED to the GND pin with another alligator lead.
- Download the following code to the board:
pins.A2.digitalWrite(false)
input.buttonA.onEvent(ButtonEvent.Click, function () {
pins.A2.digitalWrite(true)
})
input.buttonB.onEvent(ButtonEvent.Click, function () {
pins.A2.digitalWrite(false)
})
Test: Press button A to output an “on” signal at pin A2 and press button B to output an “off” signal.
Result: The LED lights when button A is press and turns off when button B is pressed.