Digital Music Programming II: switch1.bsx
This lab demonstrates how to send on/off switch data thru MIDI into Max/MSP using a Basic Stamp IIsx microcontroller. To learn how to program the Basic Stamp to monitor more than one switch at a time, go to the switch15 lab.
Switches come in many forms and sizes, but all of them have a common simple implementation: there is a wire going through the switch which is either broken or connected inside the switch. The example program for this lab demonstrate how to detect whether the switch is opened or closed, and will send a MIDI note message when the switch is either turned on or off.
The following circuit diagram shows the best way to connect a switch to a Basic Stamp input pin. There is a 1 kohm resistor connected to an input on the Basic Stamp. This is used to remove false triggers due to static electricity, and also to prevent damaging the Basic Stamp if you accidentally set the input pin to be an output pin set to high. The 47 kohm resistor is also necessary for the circuit to work correctly and must be present or you may damage something. If you want to use an LED light to visually monitor the state of the switch, then you might want to use a smaller value resistor (say 220 ohms). 47 kohms is a good value for low power design (using the basic stamp with a 9 volt battery).
The green lines in the above picture demonstrate how the holes in the breadboard are connected underneath. The two middle rows are organized into pairs of 5 connected cells. If you plug an element into one of the 5 cells in a row, then it is connected to all other four cells in the row.
The two vertically arranged rows of cells on either side of the breadboard are all connected together. These two rows are used used for power and ground. Usually red is used to indicat power, and blue is used to indicate ground. In the above picture, the ground and power lines are not actually connected to power or ground yet. Only the piece of the circuit used to identify the state of the switch is shown.
The switch is being monitors on pin 7 of the basic stamp (the bottom left pin). This is the pin indicated in the program below, but there are 15 other pins on the Basic Stamp which the switch circuit could also be attached as well. Therefore, you can connect up to 16 switches (or 15 switch + 1 pin for MIDI output) onto the Basic Stamp using the above circuit as a guide for each switch.
Below is the program which is stored on the Basic Stamp and which monitors the state of the switch:
Lines 1-3 contain constant definitions which are similar to the cout example program. inpin is the input pin on the Basic Stamp which is used to monitor the switch.
Line 6 is used to detect when the switch is closed (turned on). If the switch is tured on, then the program will continue to line 7. If the switch is still off, then the program will return to line 5.
Once the switch is turned on, then line 7 will be executed. The gosub is similar to a function in the C language. In this case the program control will move to line 14 and a MIDI note-on message will be sent with the serout message on line 15. Then on line 16, the program control is returned to the calling location which is line 7. The program continues on to line 9, and then to line 10.
At line 10, the program will remain stuck until the switch is turned off. While the switch is turned on, the program continually loops back to line 9. Once the switch is turned off, then the program will arrive at line 11 and go to the midioff subroutine. Then the program will start all over and monitor the switch until it is turned on again at line 6.
The gosub routines are not necessary for such a simple program, but it is a good way to make the program readable by separating the program into function as long as speed is not a critical issue in the program.