From: Jan Panteltje on
As I am working on YAOLC (Yet An Other LED Controller),
one feature is light dark detection with an LDR.

In a previous one I just detect if it is dark, and if it is, allow the LEDs on.
Else they stay off, unless the automatic feature is disabled by the user.
Sort of manual you could say.

I would not have bothered to look into it, if I had not noticed the strange
network traffic in the multi-PIC RGB controller, constantly sending
values of zero if it was light....

So I wanted the network to be quiet if no external light CHANGE happened.

I thought, oops, no idea how to program that, and wrote some code:

; test light level:
; select ADC
movlw D'6'
call select_adc
; read ADC
BSF ADCON0, GO ; Start conversion bit 1
BTFSC ADCON0, GO ; Is conversion done?
; ADC result is left justified, shift right 6 x 1111 1111 1100 0000, high byte in ADRESH

; compare high byte to light sensitivity level (0-255)

bsf flags2, NIGHT_FLAG
movfw light_sensitivity
cpfsgt ADRESH
bcf flags2, NIGHT_FLAG


; test for light dark change

btfss flags2, NIGHT_FLAG
goto is_light
; is dark
btfss flags2, OLD_NIGHT_FLAG
; and was light
goto it_became_dark
; and was dark
goto no_light_change

is_light:
btfss flags2, OLD_NIGHT_FLAG
; and was light
goto no_light_change
; and was dark
goto it_became_light



it_became_light:
light_off:
; RS232 red zero command to red slave
movlw 'R'
call tx_w
movlw '0'
call tx_w
call tx_crlf

; RS232 green zero command to green slave
movlw 'G'
call tx_w
movlw '0'
call tx_w
call tx_crlf

; blue to zero, master
clrf CCPR1L
goto test_udp_in


it_became_dark:
light_on:
; restore red
movlw 'R'
call tx_w
movfw red
call print_w_ascii_dec
call tx_crlf
; restore green
movlw 'G'
call tx_w
movfw green
call print_w_ascii_dec
call tx_crlf
; restore blue
movfw blue
movwf CCPR1L
goto test_udp_in


no_light_change:
test_udp_in:
; update OLD_NIGHT_FLAG
bcf flags2, OLD_NIGHT_FLAG
btfsc flags2, NIGHT_FLAG
bsf flags2, OLD_NIGHT_FLAG


I tested it with a trimpot on the ADC input, as I had no LDRs anymore.
Got some new LDRs today , and now the circuit is working.
It is a very interesting behaviour, a bit like those light switches
where you can turn the light on and off from 2 different places.

If it is light, and the LEDs are also on, you can wave over the LDR and it will see
the light - dark - light sequence, and decide it is light (last change), and switch the LEDs off.
If it is dark, and the LEDs are off, and you shine a flashlight on it for a moment,
it sees dark - light - dark, and switches the LEDs on.
If you send any command to it via the network it will set the light level to what you specify.
The light 'on' is always the last light level you specified, also you can disable the light sensing system
remotely by setting a high or low threshold.

I think this system is better then the normal light dark detector.
Who would have thought that such a simple problem could turn out to be so complicated?

From: JosephKK on
On Thu, 17 Jun 2010 21:37:11 GMT, Jan Panteltje
<pNaonStpealmtje(a)yahoo.com> wrote:

>As I am working on YAOLC (Yet An Other LED Controller),
>one feature is light dark detection with an LDR.
>
>In a previous one I just detect if it is dark, and if it is, allow the LEDs on.
>Else they stay off, unless the automatic feature is disabled by the user.
>Sort of manual you could say.
>
>I would not have bothered to look into it, if I had not noticed the strange
>network traffic in the multi-PIC RGB controller, constantly sending
>values of zero if it was light....
>
>So I wanted the network to be quiet if no external light CHANGE happened.
>
>I thought, oops, no idea how to program that, and wrote some code:
>
>; test light level:
>; select ADC
> movlw D'6'
> call select_adc
>; read ADC
> BSF ADCON0, GO ; Start conversion bit 1
> BTFSC ADCON0, GO ; Is conversion done?
>; ADC result is left justified, shift right 6 x 1111 1111 1100 0000, high byte in ADRESH
>
>; compare high byte to light sensitivity level (0-255)
>
> bsf flags2, NIGHT_FLAG
> movfw light_sensitivity
> cpfsgt ADRESH
> bcf flags2, NIGHT_FLAG
>
>
>; test for light dark change
>
> btfss flags2, NIGHT_FLAG
> goto is_light
>; is dark
> btfss flags2, OLD_NIGHT_FLAG
>; and was light
> goto it_became_dark
>; and was dark
> goto no_light_change
>
>is_light:
> btfss flags2, OLD_NIGHT_FLAG
>; and was light
> goto no_light_change
>; and was dark
> goto it_became_light
>
>
>
>it_became_light:
>light_off:
>; RS232 red zero command to red slave
> movlw 'R'
> call tx_w
> movlw '0'
> call tx_w
> call tx_crlf
>
>; RS232 green zero command to green slave
> movlw 'G'
> call tx_w
> movlw '0'
> call tx_w
> call tx_crlf
>
>; blue to zero, master
> clrf CCPR1L
> goto test_udp_in
>
>
>it_became_dark:
>light_on:
>; restore red
> movlw 'R'
> call tx_w
> movfw red
> call print_w_ascii_dec
> call tx_crlf
>; restore green
> movlw 'G'
> call tx_w
> movfw green
> call print_w_ascii_dec
> call tx_crlf
>; restore blue
> movfw blue
> movwf CCPR1L
> goto test_udp_in
>
>
>no_light_change:
>test_udp_in:
>; update OLD_NIGHT_FLAG
> bcf flags2, OLD_NIGHT_FLAG
> btfsc flags2, NIGHT_FLAG
> bsf flags2, OLD_NIGHT_FLAG
>
>
>I tested it with a trimpot on the ADC input, as I had no LDRs anymore.
>Got some new LDRs today , and now the circuit is working.
>It is a very interesting behaviour, a bit like those light switches
>where you can turn the light on and off from 2 different places.
>
>If it is light, and the LEDs are also on, you can wave over the LDR and it will see
>the light - dark - light sequence, and decide it is light (last change), and switch the LEDs off.
>If it is dark, and the LEDs are off, and you shine a flashlight on it for a moment,
>it sees dark - light - dark, and switches the LEDs on.
>If you send any command to it via the network it will set the light level to what you specify.
>The light 'on' is always the last light level you specified, also you can disable the light sensing system
>remotely by setting a high or low threshold.
>
>I think this system is better then the normal light dark detector.
>Who would have thought that such a simple problem could turn out to be so complicated?

I might. I have had enough of them "blow up in my face". About all i
have to do now is remember to ask myself how might this fail (do proper
engineering)?