-adc.bas
Option Explicit
' define constants
const PotPin as byte = 16
const FSRPin as byte = 17
const switchPin as byte = 20
' define variables
dim switchState as byte
dim ADCValue as integer
dim count as byte
Public Sub Main()
delay(0.5)
' say hello
dim i as byte
for i = 1 to 3
flashyFlashy
delay(0.05)
next
do
' use switch to toggle between which sensors to read and display
switchState = getPin(switchPin)
if switchState = 1 then
putpin 26, 0
' read the FSR:
ADCValue = getADC(FSRPin)
' only debug periodically
if count = 0 then
debug.print "FSR = " ; cstr(ADCValue)
end if
else
putpin 26, 1
' read the potentiometer
ADCValue = getADC(PotPin)
' only debug periodically
if count = 0 then
debug.print "Pot = " ; cstr(ADCValue)
end if
end if
setDisplay ADCValue
count = (count + 1) mod 25
delay 0.0
loop
End Sub
' setDisplay takes an integer and sets the appropriate pins to
' reflect the value in the LED bar graph.
' The number is assumed to be the 10-bit (0-1023) result of an ADC.
sub setDisplay(byVal x as integer)
dim topPin as byte
dim i as byte
dim pinState as byte
topPin = cbyte(cSng(x)/1024.0 * 10.0) + 5
for i = 5 to 14
if i < topPin then
pinState = 1
else
pinState = 0
end if
if (i=11) then 'pin 11 is broken
call putpin(15, pinState)
else
call putpin(i, pinState)
end if
next
end sub
sub flashyFlashy()
dim pinState as byte
dim i as byte
' light the graph up...
pinState = 1
for i = 5 to 14
if (i=11) then 'pin 11 is broken
call putpin(15, pinstate)
else
call putpin(i, pinState)
end if
delay(0.005)
next
delay(0.125)
' ...and down
pinState = 0
for i = 14 to 5 step -1
if (i=11) then 'pin 11 is broken
call putpin(15, pinstate)
else
call putpin(i, pinState)
end if
delay(0.01)
next
end sub
|