Shop OBEX P1 Docs P2 Docs Learn Events
"Newbie" Need's help with led code on bs2 with adc0831 and distance sensor! — Parallax Forums

"Newbie" Need's help with led code on bs2 with adc0831 and distance sensor!

paulhostpaulhost Posts: 6
edited 2011-08-14 13:46 in BASIC Stamp
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. I’m new to this but intrigued! I have tried for 2days can’t figure it out I’m 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

  • FranklinFranklin Posts: 4,747
    edited 2011-08-07 17:41
    It really helps if you wrap your code in code tags to maintain the indenting. This is available in the advanced options.
  • RDL2004RDL2004 Posts: 2,554
    edited 2011-08-07 18:41
    lightled1:
    HIGH LED1
    RETURN
    ?
  • paulhostpaulhost Posts: 6
    edited 2011-08-08 16:33
    Thanks for the advise
  • paulhostpaulhost Posts: 6
    edited 2011-08-08 16:38
    Thanks for your help Rick but I have tried this before the post and it didnt work. I think there may be an issue with my led code source not specifying how to interpret the distance to light the leds. If you have any other thoughts that would be great.
    Thanks again Paul
  • ercoerco Posts: 20,256
    edited 2011-08-08 17:19
    So exactly what's not working? Is the distance-measuring part working properly? Do you get the right sort of numbers in your DEBUG statements?

    I take it you've seen this : http://www.parallax.com/dl/docs/prod/acc/SharpGP2D12Snrs.pdf

    and have it hooked up as such?
  • paulhostpaulhost Posts: 6
    edited 2011-08-09 02:49
    Thanks for response erco, everything is hooked up correctly in my circuit and I do see the numbers in the debug sreen and the numers change with distance however when I run the program the led will contstantly flash rather than flash at a given distance. I need to flash the leds between a given distance if not then no flash. I think that there is something wrong in my led statements not sure what it is. If you have any other thoughts that would be great. Thanks Paul
  • ercoerco Posts: 20,256
    edited 2011-08-11 11:11
    Any progress? I'm assuming you are getting numbers from 100-250 that jibe with your goals. As noted, you need an extra return. Try swapping out your main program. Replace:

    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.
  • paulhostpaulhost Posts: 6
    edited 2011-08-13 05:44
    Erco....... That is great!!! I have tried several tweeks to get only one led to light between given distance but still having problems. The leds do light when they hit the given distnce but they all stay on. Im trying to get led 1 to light between 100 to 150 or led 2 to light between 151 to 200 or led 3 to light between 201 to 250. There will be only one light lit at any given distance.

    Thank you for sharing your wisdom!
    Paul
  • ercoerco Posts: 20,256
    edited 2011-08-13 22:21
    That's good news; try this quickie mod (there are better ways, but...):


    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
  • paulhostpaulhost Posts: 6
    edited 2011-08-14 02:57
    Erco............ Thank you!!! That solved my issue when I first tried it only two led lights would light I simply had to change the less than at the beginning of the statements to greater than and it works like a charm! I hope that there will be something I can do for you in the future probably unlikely but you never know.

    Thanks again Erco
    Paul
  • ercoerco Posts: 20,256
    edited 2011-08-14 13:46
    Oops, my bad on the < sign, but glad you figured it out. You have passed the final Jedi test. Everyone learns, everyone wins! :)
Sign In or Register to comment.