There is some overkill for robustness in this code. The pulse in command alone could read the sensors but if it didn't see anything it would be stuck for some time just doing the read. have fun. 'For basic stamp 1 'By bob gross '7/15/97 'Symbol Probability = B5 'Sensor to read = B6 'Frequency = B3 at end of code, used for loop counter in ReadPulseIn 'B6 is used to pass the IR sensor number one desires to look at Top: Serin 0,N2400,#B6 'Wait for a command from external computer 'Pause 10 'Reset to 0 before calling sub(s) B5 = 0: B9 = 0: B2 = 0: B3 = 0: W4 = 0: W5 = 0 If B6 < 1 OR B6 > 6 Then SkipAll 'Skip if not a valid sensor number ' Pause 10 'pause 100 B6 = B6 + 1 'Set B6 to the correct pin numbers before calling sub 'If jumper is x then Goto SerialMode 'If jumper is x then Goto ParMode 'If jumper is x then Goto ControlerMode 'LookForPulse: 'This next rutine sorts out which pin to look at for activity. 'It then looks at the proper pin for activity. 'When it is the proper pin then skips the rest of the rutine ' to "DoneLookingAtPin" 'Then it is measured for pulse width. 'Toggle 0 If B6 > 2 Then SkipPin2 'Use for forward facing sensor For B3 = 1 to 100 'Sensor on pin 2 B5 = B5 + Pin2 'Remember pins are base 0 Next B3 Goto DoneLookingAtPin SkipPin2: If B6 > 3 Then SkipPin3 'Use for forward facing sensor For B3 = 1 to 100 'Sensor on pin 3 B5 = B5 + Pin3 Next B3 Goto DoneLookingAtPin SkipPin3: If B6 > 4 Then SkipPin4 For B3 = 1 to 100 'Sensor on pin 4 B5 = B5 + Pin4 Next B3 Goto DoneLookingAtPin SkipPin4: If B6 > 5 Then SkipPin5 For B3 = 1 to 100 'Sensor on pin 5 B5 = B5 + Pin5 Next B3 Goto DoneLookingAtPin SkipPin5: If B6 > 6 Then SkipPin6 For B3 = 1 to 100 'Sensor on pin 6 B5 = B5 + Pin6 Next B3 Goto DoneLookingAtPin SkipPin6: For B3 = 1 to 100 'If I got this far must be pin 7 B5 = B5 + Pin7 'Remember pins are base 0 Next B3 DoneLookingAtPin: 'Toggle 0 B5 = 100 - B5 'Convert to 0 = no probability, 100 = max ' GoSub ReadPulseIn ' Return 'ReadPulseIn: 'This rutine first checks if there was activity on pin. 'If so it then looks for valid pulse signal for measurement. 'First the negitive width is measured. Then if the negitive width 'was valid, it then measures the positive width. It continues this 'until the pulse is measured twice for negitive and twice for positive. 'It then compares the pusles for consistancy. If not consistance it 'then discards them (filter). If consistant it averages them. It then 'returns with a valid measurement for both period and probability. B3 = 0 'Reset B3 to zero If B5 < 40 Then SkipRPI '(active low sensor) Skip If not low 'At least two out of nine counts saw something Pulsin B6, 0, B4 'Read the negitive pulse in If B4 < 30 Then SkipRPI 'If the negitive wasn't their then skip, else Pause 1 Pulsin B6, 1, W4 'Read the positive pulse width If B4 < 30 Then SkipRPI 'If the positive wasn't their then skip, else W4 = W4 + B4 'Combine the negitive and positive widths Pause 2 Pulsin B6, 0, B4 'Read the negitive pulse width a 2nd time If B4 < 30 Then SkipRPI 'If the negitive wasn't their then skip, else Pause 3 Pulsin B6, 1, W5 'Read the positive pulse width a 2nd time If B4 < 30 Then SkipRPI 'If the positive wasn't their then skip, else W5 = W5 + B4 'Combine the 2nd negative and positive widths 'for later filter and average If W5 > W4 Then FirstIsHigher ' B3 = W4 - W5 'Look at the variance in data. If B3 <15 Then PulseGood 'Closer than 15 counts is valid data W4 = 0 'Reset words if data isn't valid W5 = 0 'This filters out bad data FirstIsHigher: B3 = W5 - W4 'Find out if data is valid 'NOTE: If W4 & W5 were set to 0 by the first they remain 0. If B3 <15 Then PulseGood 'Closer than 15 counts is valid data SkipRPI: 'Skip Read Pulse In W4 = 0 'Reset words if data isn't valid W5 = 0 PulseGood: 'The data at this point is valid W5 = W4+W5/2 'Average the readings for accuracy If W5 > 80 Then Valid 'If more 80 the data is valid else W5 = 0 'Else reset W5 Valid: B3 = W5 'Need number in byte form to send out RS-232 'Return 'GoSub LookForPulse SkipAll: 'debug b3 'Debug b5 SerOut 1, N2400,(B3) 'Send out frequency's period in counts of 10 uS SerOut 1, N2400,(B5) 'Send out duty cycle in counts of 1 to 99 Goto Top