'Chaos Controller, by Elaine Walker 2000
 
'code for the Basic Stamp controller
 
'====================================INITIALIZATION=======================================
'-----------DATA FROM SLIDERS------------
input 0      'START button (starts the music)
input 1      'SEED button (sets seed value)
input 8      'photoresistor LSB (chaos value)
input 9      'photoresistor vld
input 10     'photoresistor MSB
input 11     'photoresistors for SLIDER 2 (seed value)
input 12
input 13
'-------------LIGHTS ON BOX--------------
output 7     'light indicator on box for SLIDER 1
output 6     'light indicator
output 5     'light indicator
output 4     'same for SLIDER 2 indicator lights
output 3
output 2
'--------------MIDI OUT PORT-------------
output 14    'serial out for MIDI
'-------------READ GREY CODE-------------
n var nib          'reads chaos slider 3bit grey code
code var nib       'puts code in the right order, 0 through 7
m var nib          'reads seed slider 3bit grey code
mcode var nib      'puts code in the right order, 0 through 7
hibits var word
medbits1 var word
medbits2 var word
product var word
 
'---------------MIDI CHAOS---------------
seed var word
chaos var word
noteout var byte
seedsquared var word
tempo var word
pedal var word
'duration var byte
'velocity var byte
seed = 45875
chaos =  55000
chaos = chaos - 32768
'=================================BLINK LIGHTS AT STARTUP================================
startup:
 pause 1000
 out2=1
 pause 200
 out3=1
 pause 200
 out4=1
 pause 200
 out5=1
 pause 200
 out6=1
 pause 200
 out7=1
 pause 200
 outs=%00000000
 pause 200
 outs=%11111100
 pause 500
 outs=%00000000
'===================WAIT FOR PLAYER TO PRESS BUTTON ON VERTICAL SLIDER===================
waitforstart:
 high 15
 pause 1
 rctime 15, 1, pedal
 pedal = pedal/150
 lookup pedal, [1000, 250, 250, 75], tempo
 IF in1=1 THEN main
 IF in0=1 THEN resetseedpaused
goto waitforstart
'==============================READ DATA FROM CHAOS SLIDER================================
main:
 IF in1=0 THEN waitforstart
 n = (in8) + (in9 * 2) + (in10 * 4)                     'read inputs 8,9,10 for chaos
 lookup n, [50000, 62259, 60000, 56000, 62000, 59000, 55000, 58000], chaos
 chaos = chaos - 32768   'ABOVE ARE PRESET CHAOS VALUES
 
 IF in0=1 THEN resetseed
'------------CHECK IF SEED IS NEGATIVE, AND SQUARE THE SEED VALUE-----------------
continue:
if(seed<32768) then negativeseed  'if seed is negative... (below 32768)
positiveseed:
 seed = seed - 32768                                    'make seed range 0 to 32768 (0 to 2, absolute value)
                                                        'however, the seed never exceedes the range of 0 to 1
                                                        'which is 1 to 16384 at this point
                                                        'the sign doesn't matter, since seed gets squared
 goto keepgoing
negativeseed:
 seed = 32768 - seed
keepgoing:
 hibits = (seed/128) * (seed/128)                       'high bits * high bits +
 medbits1 = ((seed*128)/256) * (seed/128)               'low  bits * high bits +
 medbits2 = (seed/128) * ((seed*128)/256)               'high bits * low  bits +
 seedsquared = hibits + (medbits1/256) + (medbits2/256)
'-----------------------MULTIPLY CHAOS * SEED SQUARED---------------------------
 hibits = (chaos/128) * (seedsquared/128)               'high bits * high bits +
 medbits1 = ((chaos*128)/256) * (seedsquared/128)       'low  bits * high bits +
 medbits2 = (chaos/128) * ((seedsquared*128)/256)       'high bits * low  bits +
 product = hibits + (medbits1/256) + (medbits2/256)
'---------------------- 1 - rx^2 ------ (rx^2 is "product") ------------------
 seed = 49152 - product                                 '1-rx^2
                                                        '49152 is 1 now instead of 16384 to make 0 = 32000
                                                        'so that negative numbers can be represented
'--------------------------------MIDI OUTPUT--------------------------------------
 noteout = seed / 744                                   'put in range of 88 note keyboard
 serout 14, 12, [144, noteout, 120]                     'play MIDI notes
 pause tempo
 serout 14, 12, [128, noteout, 0]
 pause 1
'debug ?tempo
goto main
'==========SET NEW SEED VALUE WITH BUTTON ON HORIZONTAL SLIDER DURING ITERATION==========
resetseed:
 m = (in11) + (in12 * 2) + (in13 * 4)                   'read inputs 11,12,13 for seed value
 lookdown n, [0,1,3,2,6,4,5,7], mcode                   '[000, 001, 011, 010, 110, 100, 101, 111]
 
 seed = mcode * 4681 + 16384                            'scale seed to be -1 to 1, which is 16384 to 49152
''''' seed = 45875                                      'TEMPORARY PRESET SEED VALUE
goto continue
'===========SET NEW SEED VALUE WITH BUTTON ON HORIZONTAL SLIDER WHILE PAUSED=============
resetseedpaused:
 m = (in11) + (in12 * 2) + (in13 * 4)                   'read inputs 11,12,13 for seed value
 lookdown n, [0,1,3,2,6,4,5,7], mcode                   '[000, 001, 011, 010, 110, 100, 101, 111]
 seed = mcode * 4681 + 16384                            'scale seed to be -1 to 1, which is 16384 to 49152
''''' seed = 45875                                      'TEMPORARY PRESET SEED VALUE
goto waitforstart