Digital Music Programming II: switch15.bsx




This lab demonstrates how to send states for multiple switches through MIDI into Max/MSP using a Basic Stamp IIsx microcontroller. This program allows multiple switches to be connected to the Basic Stamp. Refer to the switch1 lab to learn how the individual switches are connected to the Basic Stamp.

switch15.bsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
scount     con    15
sdata      var    bit(scount)
inputs     var    INS.lowbit
channel    var    byte
midioutpin con    15
midirate   con    60

gosub InitSwitchStates

top:
   for channel = 0 to scount - 1
      if (inputs(channel) = sdata(channel)) then nextline
         sdata(channel) = inputs(channel)
         gosub sendState
      nextline:
   next
   goto top

sendState:
   if (sdata(channel) = 0) then nextline2
      gosub midioff
      return
   nextline2:
   gosub midion
return

midion:
   serout midioutpin, midirate, [$90, 60 + channel, 64]
return

midioff:
   serout midioutpin, midirate, [$90, 60 + channel, 0]
return

InitSwitchStates:
   for channel = 0 to scount - 1
      sdata(channel) = inputs(channel)
   next
return

The header section of the code (lines 1-6) contains constants and variables for the program. Note the inputs variable which is set equal to the first bit of the INS input pin state word. This is done so that the input pins can be accessed as an array of bits. The scount variable is used to indicate the number of switches connected to the Basic Stamp.

Line 8 initializes the states of the switches so that MIDI data will only be sent when the state of the switch changes.

Line 10-17 form the main body of the program. The for loop cycles through each input switch checking to see if it has changed states. If it has changed states then lines 13 and 14 are executed and a MIDI message is sent.


Exercises

  1. Set the variable scount to 15, and run the program on the basic stamp with no switches or wires connected to the input pins for the switches. Uncomment the debug statements. What happens when you touch the input pins? (No, you will not get shocked or lose a finger).

  2. Make your own switch. Find two metal objects and solder wires to them:

    Then plug one object into each side of the switch location as shown in the circuit below:

    Touch the two metal objects together while the basic stamp is connected to a MIDI synthesizer to listen to the note generated by your switch.

  3. Try playing the switch you created with the scount set to 1 and then to 15. Does the switch behave differently?

  4. Attach several switches to the Basic Stamp and run the program. Play the switches to make a melody. Also, try making a lookup array of notes to play rather than playing notes of the chromatic scale.

  5. Modify the switch15.bsx program so that the sensors' current states are sent even if the state has not changed from on to off or off to on.

  6. Extra Credit: design a button instrument which acts like a MIDI keyboard but can be played with a non-keyboard technique. Or design an instrument where the button states imitate the holes on a flute, saxophone, etc.

  7. Extra Credit: make a computer-music windchime out of aluminum cans.

  8. Extra Extra Credit: figure out how to connect many more than 15 switches to one Basic Stamp.