' Serial Data Buffers dim inputBuffer(1 To 13) As Byte '4-byte output buffer. dim outputBuffer(1 To 10) As Byte '1-byte output buffer. ' Global Vars dim inputByte As Byte dim outputByte As Byte dim photoCellOne As Integer dim photoCellTwo As Integer dim photoCellOneHigh As Integer dim photoCellTwoHigh As Integer dim photoCellOneLow As Integer dim photoCellTwoLow As Integer dim photoCellOneB As Byte dim photoCellTwoB As Byte dim startTime As Single dim currentTime As Single ' Board Setup Constants Private Const photoCellOnePin As Byte = 13 Private Const photoCellTwoPin As Byte = 14 Private Const serialTransmit As Byte = 12 Private Const serialReceive As Byte = 11 ' Programatic Constants Private Const CellLow As Integer = 1 Private Const CellHigh As Integer = 100 Private Const recalibrationWait As Single = 600.0 ' ASCII Constants Private Const AreYouThere As Byte = 205 Private Const ReadyForTransmission As Byte = 206 sub main () call delay(0.5) ' start program with a half-second delay ' Open the serial communication ports ' define which pins COM3 will be: call defineCom3(serialReceive,serialTransmit,bx1000_1000) ' set aside memory for input and output: call openQueue(inputBuffer, 13) call openQueue(outputBuffer, 10) ' open COM3: call openCom(3, 9600, inputBuffer, outputBuffer) photoCellOneHigh = 0 photoCellOneLow = 100 photoCellTwoHigh = 0 photoCellTwoLow = 100 ' Start the recalibration timer startTime = Timer do ' Get the Photocell Values photoCellOne = getADC(photoCellOnePin) photoCellTwo = getADC(photoCellTwoPin) ' Debugging 'debug.print "1:";cStr(photoCellOne) 'debug.print "2:";cStr(photoCellTwo) ' Calibration if (photoCellOne > photoCellOneHigh) then photoCellOneHigh = photoCellOne end if if (photoCellOne < photoCellOneLow) then photoCellOneLow = photoCellOne end if if (photoCellTwo > photoCellTwoHigh) then photoCellTwoHigh = photoCellTwo end if if (photoCellTwo < photoCellTwoLow) then photoCellTwoLow = photoCellTwo end if ' Range them photoCellOne = intRanger(photoCellOneLow, photoCellOneHigh, CellLow, CellHigh, photoCellOne) photoCellTwo = intRanger(photoCellTwoLow, photoCellTwoHigh, CellLow, CellHigh, photoCellTwo) ' Convert to Useable Bytes 'photoCellOne takes first 100, photoCellTwo takes second 100.... photoCellTwo = photoCellTwo + 100 ' Debugging 'debug.print "1:";cStr(photoCellOne) 'debug.print "2:";cStr(photoCellTwo) photoCellOneB = cByte(photoCellOne) photoCellTwoB = cByte(photoCellTwo) ' Debugging debug.print "1:";cStr(photoCellOneB) debug.print "2:";cStr(photoCellTwoB) ' Send them out to the serial port if (statusQueue(inputBuffer) = true) then call getQueue(inputBuffer, inputByte, 1) if (inputByte = ReadyForTransmission) then call putQueue(OutputBuffer, photoCellOneB, 1) call putQueue(OutputBuffer, photoCellTwoB, 1) end if else outputByte = AreYouThere call putQueue(OutputBuffer, outputByte, 1) end if currentTime = Timer if ((currentTime - startTime) > recalibrationWait) then photoCellOneHigh = 0 photoCellOneLow = 100 photoCellTwoHigh = 0 photoCellTwoLow = 100 startTime = Timer end if loop end sub Function intRanger(byVal originalMin as integer, byVal originalMax as integer, byVal newMin as integer, byVal newMax as integer, byVal currentValue as integer) as integer dim zeroRefOriginalMax as long dim zeroRefNewMax as long dim zeroRefCurVal as long ' Check for out of range currentValues if (currentValue < originalMin) then currentValue = originalMin end if if (currentValue > originalMax) then currentValue = originalMax end if ' Zero Refference the values zeroRefOriginalMax = clng(originalMax - originalMin) zeroRefNewMax = clng(newMax - newMin) zeroRefCurVal = clng(currentValue - originalMin) ' Check for negative values and 0 max ranges if ( (zeroRefNewMax < 1) OR (zeroRefOriginalMax < 1) OR (originalMin < 0) OR (originalMax < 1) OR (newMin < 0) OR (newMax < 1) OR (currentValue < 0) ) then intRanger = 0 Exit Function end if intRanger = cint( ( (zeroRefCurVal * zeroRefNewMax) \ (zeroRefOriginalMax) ) + clng(newMin) ) End Function