'{$STAMP BS2SX} '' '' Programmer: Craig Stuart Sapp '' Creation Date: Wed Apr 2 19:29:26 PST 2003 '' Last Modified: Fri Apr 4 20:22:40 PST 2003 '' Syntax: Basic Stamp IIsx '' '' Description: This program monitors multiple on/off switchs. '' If a switch is turned on, a MIDI note is played. '' The pitch of the note is middle C for pin 0, '' C# for pin 1, D for pin 2, etc. '' scount con 15 ' number of switchs from 1 min to 15 max sdata var bit(scount) ' array of on/off states for each switch. inputs var INS.lowbit ' array of input pin states (from IN0 to IN14) channel var byte ' Monitor the switches from IN0 upto IN14 midioutpin con 15 ' output pin on which to send MIDI data ' in this case the last pin is used for ' MIDI communication and the other pins ' are used to read the switches. midirate con 60 ' baude mode for serout: (2500000/31250)-20 msec ' note that this value should be 12 for BS2. ' Serial rate for MIDI is 31,250 data bits/sec. ' According to BASIC Stamp Programming Manual v1.9, page 208: ' "Unused pins that are not connected to circuitry should be set to output" ' to minimize power consumption. ' which is done here: DIRS = %1000000000000000 ' Don't set pins connected to input circuitry to be output, or you can ' damage the Basic Stamp. gosub InitSwitchStates ' initialize switch states top: for channel = 0 to scount - 1 if (inputs(channel) = sdata(channel)) then nextline sdata(channel) = inputs(channel) gosub sendState nextline: next goto top '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' '' '' functions: '' '''''''''''''''''''''''''''''' '' '' sendState -- send either an on message or an off message. '' Input variables to function: '' channel = the switch number which was turned on (0 to scount-1). '' sendState: if (sdata(channel) = 0) then nextline2 gosub midioff return nextline2: gosub midion return '''''''''''''''''''''''''''''' '' '' midion -- send a MIDI command when the specified switch is turned on. '' Input variables to function: '' channel = the switch number which was turned on (0 to scount-1). '' midion: ' debug "Pin ", dec channel, " on.", cr serout midioutpin, midirate, [$90, 60 + channel, 64] return '''''''''''''''''''''''''''''' '' '' midioff -- send a MIDI command when the specified switch is turned off. '' Input variables to function: '' channel = the switch number which was turned off (0 to scount-1). '' midioff: ' debug "Pin ", dec channel, " off.", cr serout midioutpin, midirate, [$90, 60 + channel, 0] return '''''''''''''''''''''''''''''' '' '' InitSwitchStates -- initialize the states of all buttons to their '' current states (either on or off). '' InitSwitchStates: for channel = 0 to scount - 1 sdata(channel) = inputs(channel) next return