'jleblanc 10.24.05: RUN 8 FSRs to MIDI Multi threshold
'For: 18F452, 20mhz, set write to HS
'Controls 5 FSRs (A0-A4) to MIDI Drum sounds. Modifies volume
'Looks for crossing of threshold and then 6 times more.
'Note that looking 6 more times actually ended up being better
'than using a more complex peak finding system. Ones that looked
'for the impulse to drop back below threshold were too slow.
'Ones that searched for first peak were often tricked by an
'initial drop. 

'Define Clock Speed at 20
DEFINE OSC 20

' Define ADCIN parameters
DEFINE  ADC_BITS        10     ' Set number of bits in result
DEFINE  ADC_CLOCK       3      ' Set clock source (3=rc)
DEFINE  ADC_SAMPLEUS    15'50     ' Set sampling time in uS

'Enable communication out to MIDI
DEFINE HSER_RCSTA 90h ' enable the receive register
DEFINE HSER_TXSTA 20h  ' enable the transmit register
DEFINE HSER_BAUD 31250 ' set the baud rate

TRISA = %11111111       ' Set PORTA to all input
ADCON1 = %10000010      ' Set PORTA analog and right justify result

'Declare VARIABLES
numinput VAR BYTE
threshold VAR WORD
i VAR BYTE

'initialize numinput and threshold VARIABLES
numinput=8 'ALSO MUST CHANGE IN ARRAY DEC
threshold=125

'Declare ARRAYS
ADCVarvolume VAR WORD[8]
hold VAR WORD[8]
volume VAR BYTE[8]
waitn VAR BYTE[8]
sounds VAR BYTE[8]

'initialize sounds ARRAY
sounds[0]=35
sounds[1]=38
sounds[2]=42
sounds[3]=46
sounds[4]=49
sounds[5]=35
sounds[6]=38
sounds[7]=42

'initialize volume and wait ARRAYS:
FOR i=0 TO numinput
volume[i]=50 'mid volume
waitn[i] = 0 'means okay to go
NEXT

main:    
    
    FOR i=0 TO (numinput-1)
        'Get reading from FSR (assume 0-1000):
        ADCIN i, ADCVarvolume[i]
        hold[i]=ADCVarvolume[i]'This is an extra step
    
        IF hold[i]>threshold AND waitn[i]=0 THEN
            'Here we look 6 more times to find peak
             ADCIN i, ADCVarvolume[i]
                IF ADCVarvolume[i] > hold[i] THEN
                    hold[i]=ADCVarvolume[i]
                ENDIF
             ADCIN i, ADCVarvolume[i]
                IF ADCVarvolume[i] > hold[i] THEN
                    hold[i]=ADCVarvolume[i]
                ENDIF
             ADCIN i, ADCVarvolume[i]
                IF ADCVarvolume[i] > hold[i] THEN
                    hold[i]=ADCVarvolume[i]
                ENDIF
             ADCIN i, ADCVarvolume[i]
                IF ADCVarvolume[i] > hold[i] THEN
                    hold[i]=ADCVarvolume[i]
                ENDIF
             ADCIN i, ADCVarvolume[i]
                IF ADCVarvolume[i] > hold[i] THEN
                    hold[i]=ADCVarvolume[i]
                ENDIF
             ADCIN i, ADCVarvolume[i]
                IF ADCVarvolume[i] > hold[i] THEN
                    hold[i]=ADCVarvolume[i]
                ENDIF
                       
            'Convert hold to volume
             volume[i]=hold[i]/8
             IF volume[i]>125 THEN
                volume[i]=125
             ENDIF
            ' noteon channel 1
            HSEROUT [$90, sounds[i] ,volume[i]] 
            'reset wait
            waitn[i]=1
        ENDIF
    NEXT
    
    ' noteoff
    HSEROUT [$80, 35, $00]
    HSEROUT [$80, 38, $00]
    HSEROUT [$80, 42, $00]
    HSEROUT [$80, 46, $00]
    HSEROUT [$80, 49, $00]
    
    'RESET waitn
    FOR i=0 TO (numinput-1)
        IF hold[i]<threshold*3/4 THEN
            waitn[i]=0
        ENDIF
    NEXT

GOTO main