; THIS TESTED ON 30/09/03
; AND WORKS

; AUTO STOP ADDED by MPH

; Day/Night swapped on 23-5-04 by MPH
; catch to stop motor at power-up added 23-5-04

;
;**************************
;*                        *
;*   PIC Curtain Winder   *
;*    By Stuart Brown     *
;*                        *
;**************************
; Input 0 is Everything			Variable b1 states:
; Input 1 id LDR (Anal.)		===================
; Input 6 is Close Stop			0: next action close
; Input 7 is Open Stop			1: next action open
;============================		
; Output 0 Day LED			Variable b2 states:
; Output 1 Night LED			===================
; Output 2 Down (To Motor Module)	0: It is Dark
; Output 3 Up (To Motor Module)		1: It is Light
;============================
; Variable b0 is LDR Analogue
; Variable b1 is Switch State
; Variable b2 is Light State
; variable b3 is to stop motor winding at power up


start:
	let b3 = 1			;stops winding at power up
	if input6=1 then closestart   	;If at bottom then goto closestart
	if input7=1 then openstart    	;If at top then goto openstart
	goto main

closestart:
	let b1=1			;Set next action as open
	let b2=0
	goto main			;Goto main loop

openstart:
	let b1=0			;Set next action as close
	let b2=1
	goto main			;Goto main loop
	
main:
	pause 300			;Pause
	if input0 = 1 then action	;If action button pressed goto action

	readadc 1,b0
	if b0 < 50 then dayact
	if b0 > 49 then nightact
	goto main			;catch for analogue failure

dayact:
	if b2=1 then main
	goto day

nightact:
	if b2=0 then main
	goto night

action:
	if b1=0 then down		;If state is 0 then down
	if b1=1 then open		;If state is 1 then open

day:
	high 0				;Set Day (Green) LED as high
	low 1				;Set Night (Red) LED as low
	let b2=1
	goto open
	

night:
	high 1				;Set Night (Red) LED as high
	low 0				;Set Day (Green) LED as low
	let b2=0
	goto down

down:
	let b1=1
	if b3 = 1 then restore		;stops motor at power up
	high 2				;Start motor going down
	pause 1000			;Pause
	goto downstop			;Goto stopping instructions

downstop:
	
	if input0 = 1 then stop		;If action button pressed, stop
	if input6 = 1 then stop		;If closeStop is pressed, stop
	
	readadc 2,b9
	if b9 > 100 then stop	
	goto downstop			;Loop

open:
	let b1=0
	if b3 = 1 then restore		;stops motor at power up
	high 3				;Start motor going up
	pause 1000			;Pause
	goto openstop			;Goto stopping instructions

openstop:

	if input0 = 1 then stop		;If action button pressed, stop
	if input7 = 1 then stop		;If openstop is pressed, stop
	
	readadc 2,b9
	if b9 > 100 then stop	
	goto openstop			;Loop

stop:
	low 2				; )_ Turn motor
	low 3				; )    Off
	goto main			;goto main loop

restore:
	let b3 = 0			;reset b3 now that b2 is set
	goto main	















