This is an updated version of the Switch post from here.
To build the electronic circuit you need the following parts:
- a switch,
- a 1kΩ resistor,
- and a couple of wires.
The schematic for the circuit looks like this:
Switches have three pins. In one position the switch closes the connection between the middle pin an outer pin while the opposite position establishes a connection between the middle pin and the other outer pin. Therefore, we connect 5V to one of the outer pins and the 1kΩ resistor to the middle pin and ground. We will measure ‘between’ the middle pin and the resistor and therefore have a wire leading from there to pin 7 of the Arduino board. The circuit on your breadboard should look something like this:
Working circuit on breadboard
If everything is set up right, you can connect the board to the USB port of your computer. The power LED should light up and the next step is to export the program which reads the state of the switch to the board. Open the ap_ReadDigital.pde file in Arduino set the right serial port (from the menu Tools->Serial Port), compile the program, and then click on the Upload icon () to send the program to the board. To check if everything works fine so far, see if the control LED lights up when the switch is in ON-position. If that’s the case, you can then download and unzip pa_Switch.zip and open the file pa_Switch.pde in Processing.
- List of serial ports in MS Windows and Mac OS X
Now, you can run your Processing program. A new window will open and you should see something like this:
Output of the program
From the middle a red (or blue) bar will start growing to the right (or left), depending on the position of the switch. If you change the position of the switch, the other bar will start growing and when both bars are of equal size they will both turn green. Play around a little bit and you will see that rhythmic switching will maintain the bar green, isn’t that great?