'{$STAMP BS2SX} '' '' Programmer: Craig Stuart Sapp '' Creation Date: Sat Apr 5 08:23:10 PST 2003 '' Last Modified: Sat Apr 5 19:45:38 PST 2003 '' Filename: rcfsr.bsx '' Syntax: Basic Stamp IIsx '' '' Description: This program sends out MIDI notes based on the '' pressure measured from a force sensing resistor. '' Sensor data is read with an RC timing circuit. '' '' fcount con 1 ' number of connected force sensing resistors. timevar var word ' RC time variable for measuring FSR resistances. scaledval var byte ' used to scale the time values tempval var word ' used to scale the time values channel var byte ' one FSR on each channel. fdata var byte(fcount) ' current FSR positions lastnote var byte(fcount) ' for MIDI output demonstration midioutpin con 15 ' output pin on which to send MIDI data 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. gosub initialize ' initialize FSR states and lastnote array top: for channel = 0 to fcount - 1 gosub readFsrSensor gosub processSensorValue pause 100 next goto top ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' '' '' Functions -- '' '''''''''''''''''''''''''''''' '' '' readFsrSensor -- read states FSRs via RC circuit. '' Input variable: channel -- which channel to measure. '' Output variable: timevar -- holds the time to discharge the circuit. '' readFsrSensor: high channel pause 1 ' wait 1 millisecond rctime channel, 1, timevar ' debug "FSR ", dec channel, " Timing: ", dec timevar, cr return '''''''''''''''''''''''''''''' '' '' processSensorValue -- send out the new FSR reading if it is different '' than the past value. '' Input variables: channel, timevar '' processSensorValue: gosub scaleTiming if (scaledval = fdata(channel)) then endProcessTiming fdata(channel) = scaledval ' debug "FSR ", dec channel, "MIDI value: ", dec scaledval, cr gosub sendMidiData endProcessTiming: return '''''''''''''''''''''''''''''' '' '' scaleTiming -- scale the timing data into the MIDI data range '' input variables: timevar '' output variables: scaledval '' scaleTiming: tempval = (65535 - timevar + 1) / 256 if (tempval > 128) then nextline2 tempval = 0; goto nextline3 nextline2: tempval = tempval - 128 nextline3: if (tempval < 128) then nextline tempval = 127 nextline: scaledval = tempval return '''''''''''''''''''''''''''''' '' '' initialize -- store the initial states of the FSRs. '' initialize: for channel = 0 to fcount - 1 gosub readFsrSensor gosub scaleTiming fdata(channel) = scaledval lastnote(channel) = 0 ' initialize lastnote with dummies next return '''''''''''''''''''''''''''''' '' '' sendMidiData -- send the sensor data over MIDI. In this case play a '' note, but for final applications, probably a continuous '' controller would be best. '' sendMidiData: ' don't play note 0 (use for rest) if (fdata(channel) = 0) then sendMidiDataEnd ' turn off the last note before playing the next note: serout midioutpin, midirate, [$90, lastnote(channel), 0] serout midioutpin, midirate, [$90, fdata(channel), 64] lastnote(channel) = fdata(channel) sendMidiDataEnd: return