Shop OBEX P1 Docs P2 Docs Learn Events
Light sensor (LDR) — Parallax Forums

Light sensor (LDR)

EduEdu Posts: 12
edited 2008-12-04 07:50 in Robotics
Hello, I have just started to run my Boe-Bot, I am novice. I would like to do a detector of light. The idea is when the LDR is by below a concrete value (shadow), the Led works. I have made the following circuit:

dibujocq6.jpg
I know that I have to use RCTIME but I don't know how to do it.

Anybody can help me?

Thank you very much

Comments

  • PJAllenPJAllen Banned Posts: 5,065
    edited 2008-11-30 00:56
    RCTIME is covered in PBASIC Help, an example circuit is there, too, about 1/2 way down the page.


    Post Edit -- dwg attached

    Experiment for values of "C"

    Post Edited (PJ Allen) : 11/30/2008 1:49:39 AM GMT
    319 x 210 - 8K
  • GWJaxGWJax Posts: 267
    edited 2008-11-30 03:43
    Edu, Use this program to determine what value you want and then incorporate that value into your program also use this routine as a sub routine to keep checking the value but leave off the debug command and instead of using the DO-LOOP command just remove them and add a·RETURN at the end of the routine assuming your using the GOSUB command to access the routine also you can take out the PAUSE command as well when its in your program. Hope this helps you out.

    Jax

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}

    RC············· PIN···· 1

    result········· VAR···· Word


    Main:
    · DO
    ··· HIGH RC···························· ' charge the cap
    ··· PAUSE 1···························· '·· for 1 ms
    ··· RCTIME RC, 1, result··············· ' measure RC discharge time
    ··· DEBUG HOME, DEC result············· ' display value
    ··· PAUSE 50
    · LOOP
    · END

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    If a robot has a screw then it must be romoved and hacked into..
  • EduEdu Posts: 12
    edited 2008-11-30 10:09
    Thank you GWJax, I have used this code:

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}

    RC PIN 1

    result VAR Word

    Led PIN 10


    Main:
    HIGH RC ' charge the cap
    PAUSE 1 ' for 1 ms
    RCTIME RC, 1, result ' measure RC discharge time
    IF (result<200) THEN Main
    HIGH Led
    GOTO Main

    but, It has not just worked well, seems that only it obeys the first order.

    What is the problem?


    Thank you
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2008-11-30 10:59
    Edu -

    The PAUSE 50 is an integral and important part of the example program. It's missing from your program.

    Regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When all else fails, try inserting a new battery.
  • EduEdu Posts: 12
    edited 2008-11-30 13:12
    Bruce Bates-

    I have just changed the pause to 50, but It does the same. The LED lights when there is little light but when I return to bring light it does not shut down.

    why it happens?

    Thank you very much

    -Edu-
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2008-11-30 14:02
    EDU -

    Simply put, your program is in an infinte loop. If you want to get out of the loop, you must change IF (result<200) THEN Main to branch to another target label.

    Regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When all else fails, try inserting a new battery.
  • GWJaxGWJax Posts: 267
    edited 2008-11-30 19:28
    Edu, change your program to this one, You are not shutting down the LED in your program and also the RC line does not need to be turned on all the time, This program will correct this problem you are having.

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}

    RC PIN 1

    result VAR Word
    LEDon VAR Byte
    Led PIN 10

    'set Var.
    LEDon = 0
    LOW Led


    Main:
    LOW RC
    HIGH RC ' charge the cap
    PAUSE 1 ' for 1 ms
    RCTIME RC, 1, result ' measure RC discharge time
    IF result < 200 AND LEDon = 0 THEN GOTO Main ' return to Main only if results is less than 200
    IF LEDon = 1 THEN GOTO LEDoff ' and LED is off if LED is on turn it off
    LEDon = 1
    HIGH Led
    GOTO Main

    LEDoff:
    LEDon = 0
    LOW Led
    GOTO Main

    let me know if this doe not work but I do believe it will. If you have any more questions let me know.

    Jax

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    If a robot has a screw then it must be romoved and hacked into..
  • EduEdu Posts: 12
    edited 2008-12-01 13:49
    GWJax, I had done another code that it works correctly. I supose that your code works correctly, too.

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}

    RC PIN 1

    result VAR Word

    Led PIN 10


    Main:
    HIGH RC
    PAUSE 50
    RCTIME RC,1, result
    IF (result<200) THEN Ledoff
    IF (result>200) THEN Ledon
    Ledoff:
    LOW Led
    GOTO Main
    Ledon:
    HIGH Led
    GOTO Main

    On the other hand I have a problem, I'm doing a tracking color object with boe bot and I have to put the led code with the other code. How can I do?
    The other code is:

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}

    x VAR Word
    y VAR Word
    tilt VAR Word

    main:
    SERIN 0,84,[noparse][[/noparse]WAIT ("!"),DEC4 x,DEC4 y,DEC4 tilt]
    PULSOUT 14, x
    PULSOUT 15, y
    PULSOUT 13, tilt
    PAUSE 5
    GOTO main

    So, I need to put the two codes in basic stamp.

    Anybody can help me?


    Thank you
  • Mike GreenMike Green Posts: 23,101
    edited 2008-12-01 14:42
    You can set this up so the Stamp does first one thing, then the other. Make each piece a subroutine like this, then call them in turn:
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    RC PIN 1
    
    result VAR Word
    
    Led PIN 10
    
    x VAR Word
    y VAR Word
    tilt VAR Word
    
    main:
    GOSUB FirstThing
    GOSUB SecondThing
    GOTO main
    
    FirstThing:
    HIGH RC 
    PAUSE 50 
    RCTIME RC,1, result 
    IF (result<200) THEN Ledoff
    IF (result>200) THEN Ledon
    Ledoff:
    LOW Led
    RETURN
    Ledon:
    HIGH Led
    RETURN
    
    SecondThing:
    SERIN 0,84,[noparse][[/noparse]WAIT ("!"),DEC4 x,DEC4 y,DEC4 tilt]
    PULSOUT 14, x
    PULSOUT 15, y
    PULSOUT 13, tilt
    PAUSE 5
    RETURN
    


    It is not possible to have the Stamp do both things at the same time although sometimes you can interleave two separate tasks if the timing works out. In this case, the SERIN prevents you from doing this because it waits indefinitely for the "!" and that cannot be controlled from your program. A timeout on the SERIN would not help because of the high Baud. There's not enough time after the "!" is received to set up to receive the numbers.
  • GWJaxGWJax Posts: 267
    edited 2008-12-01 17:18
    Edu, look at this code that I have done with your code. What I did is speed up your routines and cut down some of you current flow from the stamp. Run this code and let me know how it works..

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}

    ' set pins
    RC PIN 1
    LED PIN 10

    'set var
    result VAR Word
    x VAR Word
    y VAR Word
    tilt VAR Word



    Main:
    GOSUB LightCheck ' check light conditions
    GOSUB ColorCheck ' get color x,y,tilt varables
    'continue code here
    GOTO main

    LightCheck:

    HIGH RC
    PAUSE 1 ' charge cap for 1ms
    RCTIME RC,1, result
    IF result < 200 THEN LOW LED
    IF result >= 200 THEN HIGH LED
    LOW RC ' discharge cap to reduce current flow from stamp
    return

    ColorCheck:

    SERIN 0,84,[noparse][[/noparse]WAIT ("!"),DEC4 x,DEC4 y,DEC4 tilt]
    PULSOUT 14, x
    PULSOUT 15, y
    PULSOUT 13, tilt
    return

    end

    Jax

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    If a robot has a screw then it must be romoved and hacked into..
  • EduEdu Posts: 12
    edited 2008-12-01 20:27
    Thank you Mike and GWJax,

    GWJax your code works very well. I would like to do you a question, The led only works when I click run in Roborealm. If I want that the led works without running roborealm, What could I do?

    Thank you again
  • GWJaxGWJax Posts: 267
    edited 2008-12-01 21:02
    What is happening is that roborealm needs the color check routine. If you want to just have the LED work then remark out GOSUB ColoCheck with the ' infront of this code or just remove the GOSUB ColorCheck. The program is locking up when the roborealm is not running in the background. Hope that makes sense to you.

    Jax

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    If a robot has a screw then it must be romoved and hacked into..
  • EduEdu Posts: 12
    edited 2008-12-01 21:43
    Ok, the explanation very clear. An doubt more, Could I make diferents sounds depending of the movement of Boe? If the Boe goes straight I make a frequency(with a speaker), if the boe turns left another frequency and finally, If the boe turns right another frequency. The idea would be for Each movement,a determinate sound.


    Thank you for your time
  • GWJaxGWJax Posts: 267
    edited 2008-12-01 22:30
    Edu, Yes you can do what your talking about. Post your turn routine and the speaker PIN your using and I'll show you how to do this..

    Jax

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    If a robot has a screw then it must be romoved and hacked into..
  • EduEdu Posts: 12
    edited 2008-12-02 19:13
    Hello, This is the idea:

    IF resultat < 200 THEN LOW LED .............
    (Now, I would like to work the speaker with this frequency)

    FREQOUT 5,250,3000
    FREQOUT 5,250,6000
    FREQOUT 5,250,3000
    FREQOUT 5,250,6000

    and

    IF resultat >= 200 THEN HIGH LED ............

    (another frequency when the LED turn on)

    FREQOUT 5,250,8000
    FREQOUT 5,250,6000
    FREQOUT 5,250,8000
    FREQOUT 5,250,6000

    One question more, Is it possible to save a short missage voice and later play in the boe bot ?

    Thank you very much
  • GWJaxGWJax Posts: 267
    edited 2008-12-02 23:55
    EDU, Please post your turning routine so I can inbed the code in one of those turns then you can just repeat the same code for the other turns with the diffrent sounds. I see your speaker is on PIN 5 so I have at least that to go by but I still need your turning routines. I do not have a boe bot but programming this will be simple.
    If you want voice then you need the Emic-Text-To-Speach module and I can show you how to embed this into your code as well.

    Jax

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    If a robot has a screw then it must be romoved and hacked into..
  • EduEdu Posts: 12
    edited 2008-12-03 17:20
    Dear GWJax,

    This is the code in BS2:

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}

    ' set pins
    RC PIN 1
    LED PIN 10

    'set var
    result VAR Word
    x VAR Word
    y VAR Word
    tilt VAR Word



    Main:
    GOSUB LightCheck ' check light conditions
    GOSUB ColorCheck ' get color x,y,tilt varables
    'continue code here
    GOTO main

    LightCheck:

    HIGH RC
    PAUSE 1 ' charge cap for 1ms
    RCTIME RC,1, result
    IF result < 200 THEN LOW LED
    IF result >= 200 THEN HIGH LED
    LOW RC ' discharge cap to reduce current flow from stamp
    return

    ColorCheck:

    SERIN 0,84,[noparse][[/noparse]WAIT ("!"),DEC4 x,DEC4 y,DEC4 tilt]
    PULSOUT 14, x
    PULSOUT 15, y
    PULSOUT 13, tilt
    return

    end
    ...................

    I have a another code in Roborealm program·too. This code is in Vbscript. You can find it in a post of www.roboreal.com
    This here:

    · ' initialize our start motor values to neutral
    left_base = 750
    right_base = 750
    tilt = 700··

    · ' get current image size
    · width = GetVariable("IMAGE_WIDTH")
    · center = width / 2

    · ' get the size (width or height) of the current
    · ' bounding box
    · size = GetVariable("COG_BOX_SIZE")

    · ' if it is equal to "" then no object was detected
    · if size <> "" then

    ··· ' get the horizontal center of gravity found by the COG
    ··· ' module
    ··· cogX = GetVariable("COG_X")
    ··· ' if it is less than 75 the blob is on the left side
    ··· ' of the screen so move that way
    ··· if cogX < center-5 then
    · left_base = 750
    ····· right_base = 750-(cogX-center)/2··
    ··· ' otherwise move to the right if above 85 pixels (and no
    ··· ' movement if 75 < cogX < 85 )
    ··· elseif cogX > center+5 then
    left_base = 750+(center-cogX)/2
    ····· right_base = 750
    ······
    ··· end if
    ··· ' if the ball is too close then we have to move backwards
    size = GetVariable("COG_BOX_SIZE")
    · if· (size > 60)· then
    left_motor = left_base+((60-size)*2)
    right_motor = right_base-((60-size)*2)
    ··· else
    left_motor = left_base-((size-60)*3)
    ···· right_motor = right_base+((size-60)*3)
    ·· end if
    ··· ' set the final motor values to be picked up by
    ··· ' the SSC module
    end if
    if (size<10) then
    left_motor=750
    right_motor=750
    end if
    SetVariable "LEFT_MOTOR", CInt(left_motor)
    SetVariable "RIGHT_MOTOR", CInt(right_motor)
    ' now lets work on the tilt ... grab the current
    ··· ' tilt value
    ··· tilt = GetVariable("TILT")
    ··· ' if it was not set then default to 700 (in our case
    ··· ' this is horizontal to the floor)
    ··· if tilt = "" then
    ······· tilt = 700
    ··· end if

    ··· ' if the blob is below 55 then tilt down up a bit more
    ··· ' otherwise tilt up a little more
    ··· cogY = GetVariable("COG_Y")
    ··· if cogY < 55 then
    ······· tilt = tilt - (60-cogY)/5
    ··· elseif cogY > 65 then
    ······· tilt = tilt + (cogY-60)/5
    ··· end if
    ··· ' we don't want to look up more than horizontal to the
    ··· ' floor as we don't expect the
    ··· ' ball to be on the ceiling
    ·· if (cogY<5)or(cogY>105) then
    tilt =700
    end if

    ··· ' and set that value for the bs2
    ··· SetVariable "TILT", CInt(tilt)
    .........................................................

    I have seen the Emic-Text-To-Speach module in parallax website.·The problem is that I'm using the eb500 (bluetooth) and I can't connect in slot Boe-Bot but I supose that· I can connect to pins. (you have the image below). If I have the eb-500 with Emic-Text, Will I have problems?

    boebothf3.jpg

    If it is not possible to connect I have found a Synthesizer voice·http://www.electronickits.com/kit/complete/audi/a63.htm· I can·record a message and later playing when the pin is HIGH(1). What is your opinion?

    Adding a·my school project, I think that I can put a temperature sensor, when the temperature is·up to a concrete value then the speaker says "warning the temperature is hot".

    In conclusion, I would like·add·3·missatges. 1-When the led·is going to off ..........." the level of light is·good"

    2-When the les is going to on ...................." the level of light is bad,·led activated"

    3-When the temperature is up to a normal level·...................."warning the temperature is hot"

    I had said you that I would like add voice with the movements of robot but I have thought that it is better.



    I appreciate your help much

    Thank you· ; )
  • GWJaxGWJax Posts: 267
    edited 2008-12-04 07:50
    Edu, Let me ponder over this for a while since your using roborealm inconjuction with the boe-bot. I'm sure you can use both blue-tooth and emic-text-to-speach with this set up but this will take me a little bit longer to program it for what you want. If you were not using roborealm then I could do this in a matter of mins.

    Jax

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    If a robot has a screw then it must be romoved and hacked into..
Sign In or Register to comment.