"Newbie" Need's help with led code on bs2 with adc0831 and distance sensor!
paulhost
Posts: 6
Hi All,
I am trying to get a group of led's to light when the individually led1 when distance is from 100 to 150cm led2 from 151 to 200cm and led3 from 201 to 250cm. I have pasted the code that I have so far below. Im new to this but intrigued! I have tried for 2days cant figure it out Im missing something. Any help is greatly appreciated.
P.S. I did not write this code its a Parallax Demo and a start point for my project.
' File...... GP2D12 Demo.bs2
' Purpose... Demonstrate GP2D12
' Author.... Parallax, Inc.
' E-mail.... support@parallax.com
' {$STAMP BS2}
' {$PBASIC 2.5}
' =========================================================================
'
[ Program Description ]
' This program demonstrates reading the distance in centimeters from the
' Sharp GP2D12 Analog Distance Sensor.
'
[ I/O Definitions ]
Adc0831 PIN 0 ' ADC0831 Chip Select (ADC0831.1)
AdcClock PIN 1 ' ADC0831 Clock (ADC0831.7)
AdcData PIN 2 ' ADC0831 Data (ADC0831.6)LED1 PIN 5
LED1 PIN 5 ' LED 1
LED2 PIN 6 ' LED 2
LED3 PIN 7 ' LED 3
'
[ Constants ]
span CON 5 ' 5 cm Per Data Point
'
[ Variables ]
result VAR Byte ' ADC8031 Result
volts VAR Word ' Volts (0.01 Increments)
cm VAR Byte ' centimeters
index VAR Nib
test1 VAR Byte ' Values For
test2 VAR Byte ' Interpolation
slope VAR Word ' mV/cm between test points
range VAR Byte
'
[ EEPROM Data ]
Vout DATA 251, 179, 139, 114, 97
DATA 85, 76, 67, 62, 57
DATA 53, 50, 48, 46, 43
DATA 0
'
[ Initialization ]
HIGH Adc0831 ' Disable ADC0831
'
[ Program Code ]
DO
GOSUB Read_GP2D12 ' Read Sensor Value
GOSUB Calculate_Distance ' Convert Value To cm
DEBUG HOME, "Distance = ", DEC cm, " cm "
PAUSE 100
IF (cm >100)THEN
GOSUB lightled1
ELSEIF (cm <150)THEN
GOSUB nolightled1
ENDIF
LOOP
END
'
[ Subroutines ]
Read_GP2D12:
volts = 0 ' Reset Sensor Value
FOR index = 0 TO 2 ' Read 3 Times
LOW Adc0831 ' Enable ADC0831
SHIFTIN AdcData, AdcClock, MSBPOST, [result\9] ' Read The Voltage
HIGH Adc0831 ' Disable ADC0831
volts = volts + result ' Add The Values
PAUSE 30
NEXT
volts = volts / 3 ' Average The Readings
RETURN
Calculate_Distance:
FOR index = 0 TO 15 ' Search DATA Table For Value
READ (Vout + index), test2 ' Get Value From DATA Table
IF (test2 <= volts) THEN EXIT ' Found Value
NEXT
SELECT index
CASE 0
cm = 100 ' Set To Minimum Distance
CASE 1 TO 14 ' Calculate Distance
cm = 100 + (5 * index)
IF (test2 < volts) THEN ' Estimate Using Interpolation
READ (Vout + index - 1), test1
slope = (test1 - test2) * 10 / span ' Calculate Slope
cm = cm - ((volts - test2) * 10 / slope)
ENDIF
CASE 15
cm = 500 ' Set To Maximum Distance
ENDSELECT
RETURN
lightled1:
HIGH LED1
nolightled1:
LOW LED1
RETURN
I am trying to get a group of led's to light when the individually led1 when distance is from 100 to 150cm led2 from 151 to 200cm and led3 from 201 to 250cm. I have pasted the code that I have so far below. Im new to this but intrigued! I have tried for 2days cant figure it out Im missing something. Any help is greatly appreciated.
P.S. I did not write this code its a Parallax Demo and a start point for my project.
' File...... GP2D12 Demo.bs2
' Purpose... Demonstrate GP2D12
' Author.... Parallax, Inc.
' E-mail.... support@parallax.com
' {$STAMP BS2}
' {$PBASIC 2.5}
' =========================================================================
'
[ Program Description ]
' This program demonstrates reading the distance in centimeters from the
' Sharp GP2D12 Analog Distance Sensor.
'
[ I/O Definitions ]
Adc0831 PIN 0 ' ADC0831 Chip Select (ADC0831.1)
AdcClock PIN 1 ' ADC0831 Clock (ADC0831.7)
AdcData PIN 2 ' ADC0831 Data (ADC0831.6)LED1 PIN 5
LED1 PIN 5 ' LED 1
LED2 PIN 6 ' LED 2
LED3 PIN 7 ' LED 3
'
[ Constants ]
span CON 5 ' 5 cm Per Data Point
'
[ Variables ]
result VAR Byte ' ADC8031 Result
volts VAR Word ' Volts (0.01 Increments)
cm VAR Byte ' centimeters
index VAR Nib
test1 VAR Byte ' Values For
test2 VAR Byte ' Interpolation
slope VAR Word ' mV/cm between test points
range VAR Byte
'
[ EEPROM Data ]
Vout DATA 251, 179, 139, 114, 97
DATA 85, 76, 67, 62, 57
DATA 53, 50, 48, 46, 43
DATA 0
'
[ Initialization ]
HIGH Adc0831 ' Disable ADC0831
'
[ Program Code ]
DO
GOSUB Read_GP2D12 ' Read Sensor Value
GOSUB Calculate_Distance ' Convert Value To cm
DEBUG HOME, "Distance = ", DEC cm, " cm "
PAUSE 100
IF (cm >100)THEN
GOSUB lightled1
ELSEIF (cm <150)THEN
GOSUB nolightled1
ENDIF
LOOP
END
'
[ Subroutines ]
Read_GP2D12:
volts = 0 ' Reset Sensor Value
FOR index = 0 TO 2 ' Read 3 Times
LOW Adc0831 ' Enable ADC0831
SHIFTIN AdcData, AdcClock, MSBPOST, [result\9] ' Read The Voltage
HIGH Adc0831 ' Disable ADC0831
volts = volts + result ' Add The Values
PAUSE 30
NEXT
volts = volts / 3 ' Average The Readings
RETURN
Calculate_Distance:
FOR index = 0 TO 15 ' Search DATA Table For Value
READ (Vout + index), test2 ' Get Value From DATA Table
IF (test2 <= volts) THEN EXIT ' Found Value
NEXT
SELECT index
CASE 0
cm = 100 ' Set To Minimum Distance
CASE 1 TO 14 ' Calculate Distance
cm = 100 + (5 * index)
IF (test2 < volts) THEN ' Estimate Using Interpolation
READ (Vout + index - 1), test1
slope = (test1 - test2) * 10 / span ' Calculate Slope
cm = cm - ((volts - test2) * 10 / slope)
ENDIF
CASE 15
cm = 500 ' Set To Maximum Distance
ENDSELECT
RETURN
lightled1:
HIGH LED1
nolightled1:
LOW LED1
RETURN
Comments
HIGH LED1
RETURN
?
Thanks again Paul
I take it you've seen this : http://www.parallax.com/dl/docs/prod/acc/SharpGP2D12Snrs.pdf
and have it hooked up as such?
DO
GOSUB Read_GP2D12 ' Read Sensor Value
GOSUB Calculate_Distance ' Convert Value To cm
DEBUG HOME, "Distance = ", DEC cm, " cm "
PAUSE 100
IF (cm >100)THEN
GOSUB lightled1
ELSEIF (cm <150)THEN
GOSUB nolightled1
ENDIF
LOOP
END
And replace with this, just to see if your LEDs are working properly:
main:
GOSUB Read_GP2D12 ' Read Sensor Value
GOSUB Calculate_Distance ' Convert Value To cm
DEBUG HOME, "Distance = ", DEC cm, " cm "
PAUSE 100
IF cm<100 or if cm>250 then main' no leds if cm <100 or cm >250
high 5' led 1
if cm<151 then main
high 6' led 2
if cm<201 then main
high 7
goto main
This should light 0-3 leds based on distance. If it works, then you can tweak the code to flash one LED at a time if you like.
Thank you for sharing your wisdom!
Paul
main:
GOSUB Read_GP2D12 ' Read Sensor Value
GOSUB Calculate_Distance ' Convert Value To cm
DEBUG HOME, "Distance = ", DEC cm, " cm "
PAUSE 100
IF cm<100 or cm>250 then low 5: low 6: low 7: goto main' turn off all leds if cm <100 or cm >250
if cm<=100 and cm<151 then high 5:low 6:low 7: goto main' led 1 only
if cm<=151 and cm<201 then low 5:high 6:low 7: goto main' led 2 only
if cm<=201 and cm<=250 then low 5:low 6:high 7' led 3 only
goto main
Thanks again Erco
Paul