Datasheet
Chapter 3: EEPROM Tricks and Program Tips ยท Page 113
Incorporating the Pulse-Decay Trick into Another Program
Here is how to incorporate the test code from QtiPulseTrickLeft.bs2 into
TestFrontQtiLineSensors.bs2 from Activity #2. The first step is to declare a dummy
PIN
name for P6.
DummyPin PIN 6
Also declare a couple of extra bit variables to store the states of qtiSigLeft (P9) and
qtiSigRight (P8). Remember, they need a snapshot immediately after the PULSOUT
command that passes the threshold amount of time:
qtiStateLeft VAR Bit
qtiStateRight VAR Bit
Next, move the entire contents of the QTI calibration code block from the initialization
routine into a single subroutine and name it
Calibrate_Qtis:
Calibrate_Qtis:
HIGH qtiPwrLeft ' Turn left QTI on
HIGH qtiSigLeft ' Discharge capacitor
PAUSE 1
RCTIME qtiSigLeft, 1, qtiLeft ' Measure charge time
LOW qtiPwrLeft ' Turn left QTI off
HIGH qtiPwrRight ' Turn right QTI on
HIGH qtiSigRight ' Discharge capacitor
PAUSE 1
RCTIME qtiSigRight, 1, qtiRight ' Measure charge time
qtiThreshold = (qtiLeft + qtiRight) / 2 ' Calculate average
qtiThreshold = qtiThreshold / 4 ' Take 1/4 average
IF threshold > 220 THEN ' For code overhead
threshold = threshold - 220
ELSE
threshold = 0
ENDIF
RETURN
After the Calibrate_Qtis subroutine is called, the program can use a modified version
of the
Read_Line_Sensors subroutine that uses the pulse-decay trick. Here it is, and it's
essentially the same as the code block from the previous Your Turn.