' rob@faludi.com ' This program reads analog sensors on a glove and shows a ' decimal number that corresponds to the binary represented ' by how many fingers the glove-wearer is holding up. ' Define pins 'set up the switches and LEDs finger1 var porta.0 finger2 var porta.1 finger3 var porta.2 switch1 var portb.0 LEDRed var portc.0 LEDYellow VAR portc.1 LEDGreen var portc.2 topBar var portd.5 middleBar var portd.0 bottomBar var portd.2 upperRight var portd.4 upperLeft var portd.1 lowerRight var portd.6 lowerLeft var portd.3 ' declare variables i Var word N var word fingerDown var word[4] fingerUp var word[4] fingerLimit var word[4] fingerBin var bit[4] fingerSum var byte powerNum var byte 'declare all the inputs and outputs: input switch1 output LEDRed output LEDYellow output LEDGreen output topBar output middleBar output bottomBar output upperRight output upperLeft output lowerRight output lowerLeft ' 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 50 ' Set sampling time in uS finger VAR word[3] ' Create variable to store result TRISA = %11111111 ' Set PORTA to all input ADCON1 = %10000010 ' Set PORTA analog and right justify result Pause 500 ' Wait .5 second '''''' CHECK LEDs FOR PROPER OPERATION ''''''' 'quick count to verify seven segment LED for n = 0 to 9 gosub displaySevenSeg pause 250 gosub douseSevenSeg next ' test Red, Yellow and Green LEDs high LEDred pause 250 low ledred high ledyellow pause 250 low ledyellow high ledgreen pause 250 low ledgreen ' display zero and wait for user to calibrate closed hand N=0 gosub displaysevenseg while switch1=0 for i = 0 to 2 ADCIN i, finger[i] ' Read channel to adval fingerdown[i]=finger[i] Next wend gosub dousesevenseg pause 500 while switch1=1 'wait for switch to be released wend ' display seven and wait for user to calibrate open hand N=7 gosub displaySevenSeg while switch1 = 0 for i = 0 to 2 ADCIN i, finger[i] ' Read channel to adval fingerUp[i]=finger[i] Next wend gosub dousesevenseg pause 500 while switch1=1 'wait for switch to be released wend ' calibration calculation for i = 0 to 2 fingerLimit[i] = ((fingerup[i] - fingerdown[i]) / 2) + fingerdown[i] next '''''' MAIN PROGRAM ''''''' main: gosub readhand N=fingerSum gosub dousesevenseg gosub displaysevenseg ' debugging output to serial port for i = 0 to 2 ADCIN i, finger[i] ' Read channel 0 to adval serout2 PORTC.6, 16468, [DEC finger[i],32] ' print it to serial out with space next serout2 PORTC.6, 16468, [13,10] ' with linefeed and carriage return (10, 13) serout2 PORTC.6, 16468, [bin fingerbin[0],32] serout2 PORTC.6, 16468, [13,10] ' with linefeed and carriage return (10, 13) serout2 PORTC.6, 16468, [dec fingerUp[i],32] serout2 PORTC.6, 16468, [13,10] serout2 PORTC.6, 16468, [dec fingerDown[i],32] serout2 PORTC.6, 16468, [13,10] serout2 PORTC.6, 16468, [dec fingerLimit[i],32] serout2 PORTC.6, 16468, [13,10] GoTo main '''''' SUBROUTINES ''''''' displaySevenSeg: ' shows a number on a seven segment LED display if n <> 5 and N <> 6 then high upperRight endif if N <> 2 then high lowerRight endif if n <> 1 AND n <> 4 then high topbar endif if n <> 1 AND N <> 0 and n<>7 then high middleBar endif if N <> 1 and N <> 4 and N<>7 and N<>9 then high bottombar endif if n=4 or n=5 or n=6 or n=8 or n=9 or n=0 then high upperleft endif if n=2 or n=6 or n=8 or n=0 then high lowerleft endif RETURN douseSevenSeg: ' turns off the seven segment LED display low topbar low middlebar low bottombar low upperleft low lowerleft low upperright low lowerright return readhand: 'calculates the binary number represented by finger position fingersum=0 powerNum=1 for i = 0 to 2 ADCIN i, finger[i] ' Read channel to adval if finger[i]>=fingerlimit[i] then fingerBin[i]=1 else fingerBin[i]=0 endif fingerSum=fingerSum + (fingerBin[i] * powerNum) powerNum=powerNum*2 next return