In this tutorial we’ll use a button to turn an LED on and off. I’ve got a breadboard with an LED and a 220 ohm resistor hooked up to pin d3 as described in an earlier video. I have a little button with 4 pins that can plug into the bread board, and a 10k ohm resistor. First I push the button into the bread board across the center trench because each of the 4 pins its own connection. On one side of the button I attach one of the legs to the voltage channel. On the other leg I attach the resistor, and plug the other end of the resistor into the ground channel. On the other side, on the same side as the resistor, I connect a wire to a digital input pin, I’ll use pin D2.
In Embrio I add both an input and output node from the Input/Output menu. The output node will control the LED, which is plugged into pin D3, so I can leave node at its default value. The input node will read digital pin 2, so I change the pin type to digital and select pin 2 from the drop down. Now that the pins are set up, I connect to the Arduino. At this point I can control the LED by dragging the output node’s activation, and pressing the button makes the input node output a 1. I can connect these nodes and pressing the button turns the LED on, and releasing the button turns it back off.
I want a button press to act as a trigger and ignore the button release. To get a trigger from the button input value, I disconnect it from the output node, and add an Above Or Below node from the flow control menu. I attach the button activation, and now when I press the button and the activation goes from 0 to 1, the Go Above trigger fires. Next I add a switch node, attach the Go Above trigger to its Toggle input trigger, and connect its output activation to the output node. Now pressing the button toggles the LED!
I want to limit how fast the button can be pressed, so I’ll do one more step. I’ll disconnect the Above or Below node from the switch and move them apart. In between them I’ll add a Timer node. There are two triggers that start a timer, start and restart. Start only starts the timer when it is not already running. If I click the start trigger when the timer is already running, nothing happens. So if I connect the Go Above trigger to the Start input, and connect the Timer’s “Started” trigger to the switch toggle, now the button can only be pressed once per second. This is a bit too slow, so I’ll adjust the duration of the timer to something small until I’m happy with the interaction. Now clicking the button toggles the LED, but no matter how fast I click the LED will only change about every 200 milliseconds.
A - An Above or Below node captures a switch when the digital input pin goes from 0 to 1.
B - A Timer node limits how fast the button can fire. The Started output trigger only fires when the Start input trigger fires and the timer is not running. If the timer is already running, the Started output trigger will not fire. Change the duration input to set how long the timer takes, thereby setting the cool down period on the button.
C - A Switch is toggled by the timer to turn the LED on and off.