Shop OBEX P1 Docs P2 Docs Learn Events
LDR sensor code — Parallax Forums

LDR sensor code

wafa86wafa86 Posts: 44
edited 2010-10-05 10:08 in Accessories
hi every body

I wrote a code for LDR sesor for my circuit connect it to BS2PX module, the LDR is conncted to a LED with 2 resistor. The circuit should turn on the led when the LDR sense no light (dark) and turn off when LDR sense light. So I tried my code on my circuit, some times it works but some times it do the opposite function that the program does " turn on LED when light appears and turn off LED when dark" or it does not work at all. I think the problem is in the code because i tried to program the circuit using PIC18 with C language and it works perfectly. So I will paste the code here and I need help pleaze to fix the code

code:

' {$STAMP BS2px}
' {$PBASIC 2.5}

LdrValue VAR Byte
pin3 PIN 3 'ldrsensor
pin1 PIN 1 'leds
Baud CON 396 '9600 Baud
'RX PIN 2 'Receive Pin
TX PIN 0 'Transmit Pin
OUTPUT pin1
LOW pin1
'the lightness has levels from 0 to 255 and I choose 150 and beyond to turn LED on
LdrValue = 0
DO
here: SERIN pin3, Baud, [LdrValue]
DEBUG CLS, DEC LdrValue, CR
IF LdrValue < = 150 THEN
HIGH pin1
GOSUB here
ELSE
LOW pin1
GOSUB here
ENDIF
LOOP


thank u

Comments

  • FranklinFranklin Posts: 4,747
    edited 2010-10-02 18:10
    First off you would not talk to a raw LDR with serial so you must have some form of module and in order to help you we need a link to that module.
    Second, if you have a gosub you need a return.
    Third, You should wrap your code with the code tags so the formatting stays in the code making it easier to read by others.
  • wafa86wafa86 Posts: 44
    edited 2010-10-03 10:44
    well, I do have a circuit "module" and I did not connect the LDR directly to BOE, this is the link to my circuit:
    http://img59.imageshack.us/f/ledldr.png/

    For the code I used before RETURN but it has the same result, the previos code as follows:

    ' {$STAMP BS2px}
    ' {$PBASIC 2.5}

    LdrValue VAR Byte
    pin3 PIN 3 'ldrsensor
    pin1 PIN 1 'leds
    Baud CON 396 '9600 Baud
    OUTPUT pin1
    LOW pin1
    LdrValue = 0
    DO DEBUG CLS, DEC LdrValue, CR
    Led_On:
    IF LdrValue < = 150 THEN
    HIGH pin1
    GOSUB Led_off
    Led_off:
    ELSEIF LdrValue > 150 THEN
    LOW pin1
    RETURN
    GOSUB Led_On
    ENDIF
    LOOP


    do you have any other sugestion to fix the code???!!!!
  • wafa86wafa86 Posts: 44
    edited 2010-10-03 12:36
    ???
  • FranklinFranklin Posts: 4,747
    edited 2010-10-03 13:09
    Download and read this. www.parallax.com/dl/docs/books/edu/wamv2_2.pdf
    Your code still is not even close to working, it's just wrong and you need to do some reading to figure out why.
    The input from the LDR is analog and you need to convert that to a digital representation. Try again after reading the pdf.
  • wafa86wafa86 Posts: 44
    edited 2010-10-03 13:39
    do u know any command that helps in converting from analog to digital representation? or it needs a kind of equation????
  • FranklinFranklin Posts: 4,747
    edited 2010-10-03 15:23
    RCTIME but read the book.
  • ercoerco Posts: 20,256
    edited 2010-10-04 17:32
    I agree with Franklin. Old-school photocells or photoresistors (pedantics call 'em LDRs) are not precision devices, not sure why your module uses a serial connection. Silk purse/sow's ear syndrome. Just RCtime a direct connection to a photocell and call it a day. They're cheap, even Radio Shack has a 5 pack for 3 bucks.

    http://www.hosfelt.com/contents/en-us/d583.html 25 cents
    http://www.radioshack.com/product/index.jsp?productId=2062590
  • wafa86wafa86 Posts: 44
    edited 2010-10-05 10:08
    thanks alot I ll try your advices and use RCTIME maybe it will works.
Sign In or Register to comment.