LDR sensor code
wafa86
Posts: 44
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
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
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.
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???!!!!
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.
http://www.hosfelt.com/contents/en-us/d583.html 25 cents
http://www.radioshack.com/product/index.jsp?productId=2062590