'jdl+cdk 11.7.05: play+menu
'place a toggle switch at D2 and a Red LED at D1
'and a Green LED at D0. Green is in play mode. Red is
'in menu mode.
'PLAY:
'Controls 8 FSRs (A0-A7) to MIDI Drum sounds. Modifies volume
'Looks for crossing of threshold and then 6 times more.
'MENU:
'All fingers begin locked. To unlock a finger hold down for
'One second. Then tapping the finger will cycle through the
'drum sounds. Once you find the one you like hold the finger
'down again for one second and the finger locks
'************************************************************************
'Setup
'************************************************************************
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
'************************************************************************
'END Setup
'************************************************************************
'************************************************************************
'Declare Variables
'************************************************************************
'Declare CONSTANTS AND COUNTER VARIABLEs
numinput CON 8
numlibrary CON 12
locktime CON 300
menuth CON 300
threshold CON 125
resetthreshold CON threshold*3/4
volumemin CON 25
i VAR BYTE
timecount VAR WORD
'Declare VARIABLES
ADCVar VAR WORD
volume VAR BYTE
'Declare ARRAYS
hold VAR WORD[numinput]
impulsetoggle VAR BYTE[numinput]
sounds VAR BYTE[numinput]
mutetoggle VAR BYTE[numinput]
soundlib VAR BYTE[numinput]
lock VAR BIT[numinput] '1 is open to change, 0 is locked
'initialize sounds ARRAY
FOR i=0 TO (numinput-1)
sounds[i]=i
NEXT
'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
'initialize soundlib ARRAY
soundlib[0]=35
soundlib[1]=38
soundlib[2]=42
soundlib[3]=46
soundlib[4]=49
soundlib[5]=50'35
soundlib[6]=51'38
soundlib[7]=52'42
soundlib[8]=24'from here down new and added
soundlib[9]=25
soundlib[10]=27
soundlib[11]=31
FOR i=0 TO (numlibrary-1)
soundlib[i]=soundlib[i]+11
NEXT
'intialize lock as locked
FOR i=0 TO (numinput-1)
lock[i]=0
NEXT
'*************************
'Declare Play/Menu toggle variable
playmenu VAR BIT ' 0 play, 1 menu
lastplaymenu VAR BIT
' initialize playmenu
playmenu=0
'************************************************************************
'END Declare Variables
'************************************************************************
'************************************************************************
'MAIN
'************************************************************************
MAIN:
playmenu = PORTD.2
IF lastplaymenu ==1 AND playmenu ==0 THEN
'lock down all locked
FOR i=0 TO (numinput-1)
lock[i]=0
NEXT
ENDIF
IF playmenu == 0 THEN
GOTO PLAY
ELSE
GOTO MENU
ENDIF
lastplaymenu=playmenu
GOTO MAIN
'************************************************************************
'END MAIN
'************************************************************************
'************************************************************************
'PLAY
'************************************************************************
PLAY:
'SET STATUS LEDS
HIGH PORTD.0 'Turn on green LED indicate play mode on
LOW PORTD.1 'Turn off red LED
'code:
FOR i=0 TO (numinput-1)
'Get reading from FSR (assume 0-1000):
ADCIN i, ADCVar
hold[i]=ADCVar'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, ADCVar
IF ADCVar > hold[i] THEN
hold[i]=ADCVar
ENDIF
ADCIN i, ADCVar
IF ADCVar > hold[i] THEN
hold[i]=ADCVar
ENDIF
ADCIN i, ADCVar
IF ADCVar > hold[i] THEN
hold[i]=ADCVar
ENDIF
ADCIN i, ADCVar
IF ADCVar > hold[i] THEN
hold[i]=ADCVar
ENDIF
ADCIN i, ADCVar
IF ADCVar > hold[i] THEN
hold[i]=ADCVar
ENDIF
ADCIN i, ADCVar
IF ADCVar > hold[i] THEN
hold[i]=ADCVar
ENDIF
'Convert hold to volume
volume=hold[i]/8
volume=volume+volumemin
IF volume>125 THEN
volume=125
ENDIF
' noteon channel 1
HSEROUT [$90, soundlib[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, soundlib[sounds[i]], $00]
mutetoggle[i]=0
ENDIF
'RESET impulsetoggle
IF hold[i]<resetthreshold THEN
impulsetoggle[i]=0
ENDIF
NEXT
GOTO MAIN
'************************************************************************
'END PLAY
'************************************************************************
'************************************************************************
'MENU
'************************************************************************
MENU:
'SET STATUS LEDS
LOW PORTD.0 'Turn off green LED
HIGH PORTD.1 'Turn on red LED indicate menu mode on
'code:
FOR i = 0 TO (numinput-1)
timecount=0
'read input
ADCIN i, ADCVar
hold[i]=ADCVar
IF hold[i] > menuth THEN
WHILE hold[i] > menuth
timecount=timecount+1
'pause
PAUSE 2
ADCIN i, ADCVar
hold[i]= ADCVar
WEND
IF timecount > locktime THEN
lock[i]=lock[i] +1 'lock is a bit so this flips it
ELSE
IF lock[i]==1 THEN 'system open: change note
sounds[i]=(sounds[i] + 1) // (numlibrary-1)
ENDIF
HSEROUT [$90, soundlib[sounds[i]] ,70] 'use constant volume
ENDIF
ENDIF
NEXT
PAUSE 100 'Helps get rid of noise
'***!!! THERE IS NO SOUNDOFF CALL ANYWHERE IN MENU!!!
GOTO MAIN
'************************************************************************
'END MENU
'************************************************************************