week5

Analog-to-Digital Conversion (ADC) Input

 

Jeff's in-class example with a potentiometer and a force sensor.

 

code 1

 

-----get ADC value code-----------------

dim potVar as integer

Sub main()
call delay(0.5) ' start program with a half-second delay

do

potVar = getADC(13)
debug.print "potVar = " ; cstr(potVar)

loop


end sub

 

dim potVar as integer

Sub main()
call delay(0.5) ' start program with a half-second delay

do

potVar = getADC(13)
debug.print "potVar = " ; cstr(potVar)

if (potVar >= 0) And (potVar < 300) then
call putPin(10, 1)
end if

if (potVar >= 300) And (potVar < 600) then
call putPin(11, 1)
end if

if (potVar >= 600) And (potVar < 900) then
call putPin(12, 1)
end if

if (potVar >= 900) then
call putPin(10, 0)
call putPin(11, 0)
call putPin(12, 0)
end if

loop


end sub

 

 

refer to a photo cell ranges

Look at the deffrence between the photo cell range itself and the range when it is connected with a potentiometer.

 

Then I wanted to make several LEDs lit followed as the Potentiometer's value. Because the languages and syntaxes are different from Lingo, sometimes I get confused. The most tricky thing was the syntax which uses 'if-then' statement. So I just repeated 'if', 'then' and 'end if'. It doesn't look elegant now. So I felt have to study more about the language of Basic X. After writing the code, I learned about how to use 'if' and 'else if' statement. In Basic X, if-then statement composed like this. the difference between Lingo and BasicX is in else-if statement. Lingo uses 'else if'(there is a space beween 'else' and 'if' ) , but BasicX uses 'elseif' (there is no space).

If (boolean_expression) then
mm[statement]
End if

If (boolean_expression) then
mm[statement]
Else
mm[statement]
End if

If (boolean_expression) then
mm[statement]
Elseif (boolean_expression) then
mm[statement]
End
if

 

code 3

dim potVar as integer

Sub main()
call delay(0.5) ' start program with a half-second delay

do

potVar = getADC(13)
debug.print "potVar = " ; cstr(potVar)

if (potVar >= 0) And (potVar < 150) then ' to lit on LED 5
call putPin(5, 1)
end if

if (potVar >= 150) And (potVar < 300) then ' to lit on LED 5,6
call putPin(5, 1)
call putPin(6, 1)
end if

if (potVar >= 300) And (potVar < 450) then ' to lit on LED 5,6,7
call putPin(5, 1)
call putPin(6, 1)
call putPin(7, 1)
end if

if (potVar >= 450) And (potVar < 600) then ' to lit on LED 5,6,7,8
call putPin(5, 1)
call putPin(6, 1)
call putPin(7, 1)
call putPin(8, 1)
end if

if (potVar >= 600) And (potVar < 750) then ' to lit on LED 5,6,7,8,9
call putPin(5, 1)
call putPin(6, 1)
call putPin(7, 1)
call putPin(8, 1)
call putPin(9, 1)
end if

if (potVar >= 750) And (potVar < 900) then ' to lit on LED 5,6,7,8,9,10
call putPin(5, 1)
call putPin(6, 1)
call putPin(7, 1)
call putPin(8, 1)
call putPin(9, 1)
call putPin(10, 1)
end if

if (potVar >= 900) And (potVar < 1000) then ' to lit on LED 5,6,7,8,9,10,11
call putPin(5, 1)
call putPin(6, 1)
call putPin(7, 1)
call putPin(8, 1)
call putPin(9, 1)
call putPin(10, 1)
call putPin(11, 1)
end if

if (potVar >= 1000) then ' to lit on LED 5,6,7,8,9,10,11,12
call putPin(5, 1)
call putPin(6, 1)
call putPin(7, 1)
call putPin(8, 1)
call putPin(9, 1)
call putPin(10, 1)
call putPin(11, 1)
call putPin(12, 1)
end if

loop

end sub

 

Journal Index