'jdl+cdk 11.4.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 CONSTANTS AND COUNTER VARIABLE
numinput CON 8
threshold CON 125
resetthreshold CON threshold*3/4
volumemin CON 25
i VAR BYTE

'Declare VARIABLES
ADCVarvolume VAR WORD
volume VAR BYTE

'Declare ARRAYS
hold VAR WORD[numinput]
impulsetoggle VAR BYTE[numinput]
sounds VAR BYTE[numinput]
mutetoggle VAR BYTE[numinput]

'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 toggle ARRAYS:
volume=50 'mid volume
FOR i=0 TO numinput
impulsetoggle[i] = 0 'means okay to go
mutetoggle[i]=0 'means don't sound off
NEXT


main:    
    
    FOR i=0 TO (numinput-1)
        'Get reading from FSR (assume 0-1000):
        ADCIN i, ADCVarvolume
        hold[i]=ADCVarvolume'This is an extra step
    
        IF hold[i]>threshold AND impulsetoggle[i]=0 THEN
            'Here we look 6 more times to find peak
             ADCIN i, ADCVarvolume
                IF ADCVarvolume > hold[i] THEN
                    hold[i]=ADCVarvolume
                ENDIF
             ADCIN i, ADCVarvolume
                IF ADCVarvolume > hold[i] THEN
                    hold[i]=ADCVarvolume
                ENDIF
             ADCIN i, ADCVarvolume
                IF ADCVarvolume > hold[i] THEN
                    hold[i]=ADCVarvolume
                ENDIF
             ADCIN i, ADCVarvolume
                IF ADCVarvolume > hold[i] THEN
                    hold[i]=ADCVarvolume
                ENDIF
             ADCIN i, ADCVarvolume
                IF ADCVarvolume > hold[i] THEN
                    hold[i]=ADCVarvolume
                ENDIF
             ADCIN i, ADCVarvolume
                IF ADCVarvolume > hold[i] THEN
                    hold[i]=ADCVarvolume
                ENDIF
                       
            'Convert hold to volume
             volume=hold[i]/8
             volume=volume+volumemin
             IF volume>125 THEN
                volume=125
             ENDIF
            ' noteon channel 1
            HSEROUT [$90, sounds[i] ,volume] 
            'reset impulsetoggle
            impulsetoggle[i]=1
            'set mutetoggle to trigger
            mutetoggle[i]=1
        ENDIF
    NEXT
    
    'Reset Toggles and Noteoff Loop
    FOR i=0 TO (numinput-1)
        'Send Noteoff signal and reset mutetoggle
        IF hold[i]<threshold AND mutetoggle[i]==1 THEN
             HSEROUT [$80, sounds[i], $00]
             mutetoggle[i]=0
        ENDIF
        'RESET impulsetoggle
        IF hold[i]<resetthreshold THEN
            impulsetoggle[i]=0
        ENDIF
    NEXT

GOTO main