'{$STAMP BS2} 'adapted by Perry Hoberman 11/03 from: 'MIDI.BS2 - MIDI out program for BASIC Stamp II. 'Copyright 1997-2000 Jeff Mann jefman@utcc.utoronto.ca 'Get the latest version or add links to: 'http://www.interaccess.org/arg/arg-knowledge/MIDI.BS2 'MIDI-out only - Stamp may have trouble receiving MIDI-in because serial 'port is unbuffered. MIDI-in is not covered here. midibaudmode con 32780 '31.25kb, 8n1, non-inverted, open collector 'MIDI status bytes: controller con %10110000 + midichannel noteon con %10010000 + midichannel pitchbend con %11100000 + midichannel valueIn var word 'holds the 16-bit value read from the pot valueOut var byte 'current MIDI value (between 0 and 127) prevValueOut var byte 'previous MIDI value potPin con 8 'pin/pot to read statusbyte var byte 'MIDI status; controller, noteon, or pitchbend data1 var byte 'first data byte, eg. controller or note number data2 var byte 'second MIDI data byte, eg. value or velocity minInput var byte 'smallest value so far minInput = 255 'initialize arbitrarily high maxInput var word 'largest value input so far maxInput = 0 'initialize arbitrarily low rangeInput var word 'maxInput - minInput rangeOutput con 127 'output range for MIDI (0 to 127) switchPin con 0 'switch to reset range DIRS = %1111111111111110 'all outs except pin 0 OUTS = %0000000000000000 'all low midichannel con 1 'MIDI transmit channel midioutpin con 7 'pin connected to MIDI out connector controlleroffset con 0 'add to pot's pin # to get MIDI controller # label1: if in0 = 1 then resetRange 'reset minInput and maxInput high potPin 'read the value of the pot pause 1 rctime potPin, 1, valueIn if valueIn < minInput then resetMin 'check for lowest input value if valueIn > maxInput then resetMax 'check for highest input value label2: rangeInput = maxInput - minInput 'calculate range of input values valueOut = ((valueIn - minInput)*rangeOutput)/rangeInput 'find output value if valueOut = prevValueOut then label1 'if value hasn't changed, start over debug "input:min: ",DEC minInput," max: ",DEC maxInput, " range: ",DEC rangeInput," value:in: ",DEC valueIn," out: ",DEC valueOut,cr statusbyte = controller 'sending controller message data1 = potPin + controlleroffset 'controller # data2 = valueOut serout midioutpin, midibaudmode, [statusbyte, data1, data2] goto label1 resetMin: minInput = valueIn goto label2 resetMax: maxInput = valueIn goto label2 resetRange: if in0 = 1 then resetRange 'loop until switch is lifted minInput = 255 'initialize arbitrarily high maxInput = 0 'initialize arbitrarily low debug "range reset",cr goto label1