PDA

View Full Version : Auto chicken door Pic 818 code oddity.



tasmod
- 7th January 2012, 16:24
Ref to recent post regarding the auto chicken door. Post (switch problems with 16f628) wrong title I know.

Sadly whilst I was away a problem occurred and the door was left closed, leaving 3 of the 4 chickens out. Needless to say Mr Fox had them.

This was not a fault of the circuit etc but a failure of the fishing line used to lift and lower the door. Somehow i think they jostled each other to get in and caused the line to snap, it wasn't a strong line as i didn't expect this. I've some much stronger braided line now.

I've revamped the system and code. The whole arrangement is on a removeable board for testing purposes.

This is where i came across a problem when i attempted to slim down the code.

There are two versions of the same code, although not exact. The hardware is the same.

Code A, chicklcd.bas uses an LCD for testing purposes and the motor 'run time' is set in code.
Code B, chickswit.bas version has the LCD stuff removed and limit switch code added.

The B code general operation is basically the same but just doesn't work without the LCD serout commands added as per code A. Why ??

Code A:-


'###############################################
'# Automatic chicken coop sliding door. #
'# #
'# 12 Volt Battery powered. #
'# Open when light, close when dark. #
'# #
'# Uses single microswitch as feed back. #
'# #
'# Pic 16f818 --- Rob Lane - Dec 2011 #
'###############################################


include "Modedefs.bas"

OSCCON=$60 'set 4mhz internal oscillator
'(Set INT_OSC in programmer fuses)

' Define ADCIN parameters
Define ADC_BITS 10 'Set number of bits in result
Define ADC_SAMPLEUS 50 'Set sampling time in uS

Photo var word 'Create photo to store result of ldr input
Counter1 var byte 'counter variable

'PIN ASSIGNMENTS
LDR var porta.0

MotorUp var portb.4
MotorDown var portb.5
Switch var portb.6
LCD var portb.7

SERout PortB.7,N9600,[254,1] 'lcd CLS
pause 10 ' needed after cls command
Counter1=1 'Set counter for power up,
' door must be closed in daylight for startup.

TRISA=000001 ' Set PORTA
TRISB=000000 ' Set PORTB

Main:
' serout PortB.7,N9600,[254,1] 'lcd CLS
' pause 10 ' needed after cls command
adcin ldr, photo
serout lcd,N9600,[254,128] 'lcd line 1 home
SERout lcd,N9600,["Photo Value ",#Photo ]
serout lcd,N9600,[254,192] 'lcd line 2 home
serout lcd,N9600, ["Counter = ",#counter1 ]
pause 100
IF Photo <10000 Then checkdown 'darkness
IF Photo >30000 Then checkup 'daylight
goto Main

checkup:
if counter1=1 then up
goto main
up:
serout lcd,N9600,[254,192] 'lcd line 2 home
SERout lcd,N9600,["Motor UP "]
High MotorUp 'start motor up
pause 3200 'runtime
low motorup
counter1=2
sleep 30
goto main


checkdown:
if counter1=2 then down
goto main

down:
serout LCD,N9600,[254,192] 'lcd line 2 home
SERout LCD,N9600,["Motor DOWN "]
high MotorDown 'start motor down
pause 4500
low motordown
counter1=1
sleep 30
goto main


end


Code B :-


'###############################################
'# #
'# Automatic chicken coop sliding door. #
'# #
'# 12 Volt Battery powered. #
'# Open when light, close when dark. #
'# #
'# Uses single microswitch as feed back. #
'# #
'# Pic 16f818 --- Rob Lane - Dec 2011 #
'# #
'###############################################


OSCCON=$60 'set 4mhz internal oscillator
'(Set INT_OSC in programmer fuses)

' Define ADCIN parameters
Define ADC_BITS 10 'Set number of bits in result
Define ADC_SAMPLEUS 50 'Set sampling time in uS

Photo var word 'Create 'photo' to store result of ldr input
Counter1 var byte 'flip flop counter variable

'PIN ASSIGNMENTS
LDR var porta.0

MotorUp var portb.4
MotorDown var portb.5
Switch var portb.6


pause 1000

Counter1=1 'Set counter for power up, door must
'be closed in daylight for startup.

TRISA=000001 ' Set PORTA
TRISB=000000 ' Set PORTB


'################################################# ########

Main:
ADCIN ldr, photo 'read ldr adc value
IF Photo <10000 Then checkdown 'darkness
IF Photo >30000 Then checkup 'daylight
goto Main

'################################################# #########

checkup:
if counter1=1 then up
goto main

up:
High MotorUp 'start motor up
pause 1000 'wait 1 sec so micro switch opens
stopup:
if switch = 1 then pressedup 'check microswitch state
goto stopup 'check again till true

pressedup:
low motorup 'stop motor
Counter1=2 'flip flop counter for up/down of door
sleep 30 'test mode - sleep 30 secs
goto main

'################################################# #########

checkdown:
if counter1=2 then down
goto main
down:
high MotorDown 'start motor down
pause 1000
stopdown:
if switch = 1 then presseddown 'check again
goto stopdown

presseddown:
low MotorDown
Counter1=1
sleep 30
goto main

end


Sorry, the code looked neatly formatted when I posted but forum seems to alter that.

Demon
- 8th January 2012, 22:48
... Somehow i think they jostled each other to get in and caused the line to snap...

I couldn't help but wonder if Mr. Fox wasn't right behind them. :D

Could the PAUSE 3200 and PAUSE 4500 between High and Low MotorUp make the difference? You only have 1000 in Code B.

(sorry, have yet to use ADC)

Robert

tasmod
- 9th January 2012, 13:00
No, the difference between the two codes is the limit switch inclusion. I ran out of development time before I went away for the New Year and so as a quick fix I used the motor run timer Code A.

The pause in Code A is the motor run time, in B it's to allow the limit switch to open before it's checked for position. That way I only use one switch, not two for up/down.. The High motorup starts the motor and the pause leaves it running then the program enters the Low phase. The Counter prevents cycling from one to another. Both conditions, LDR and Counter have to be correct for it to run. That was another anomaly, I tried 'AND' which just didn't work, i resorted to a small Loop. All most odd.

I tried the Code B with Pause inclusions where the Serout commands were in Code A but still it doesn't run correctly, the motor just runs in the Open phase non stop and ignores the limit switch.
The addition of the limit switch is to make the close and open positions identical. Using Code A and run time I found the door positions changed ever so slightly over time due to not refining the run times. The different run times is for motor torque, in the down run the additional weight of the door makes it close faster than the up cycle.

It all runs Ok at the moment with Serout commands in the Code B but I'm curious as to why it needs the Serout commands for it to work correctly. Bear in mind they are useless commands, there's no LCD connected, the N9600 is incorrect for the clock at 4Mhz anyway.

The Tris commands are incorrect above but correct in code, the copy and paste to forum seems to have removed the percent and a couple of 0's and 1's.

tasmod
- 11th January 2012, 19:59
OK, the system has been in use for a few days and behaves well.

So I took the opportunity to add the 4 hour SLEEP after the door is operated in order to save battery drain.

However, in sleep mode the current consumed is the same as when it is just left looping the LDR adc.

I know there's a standing current consumed by the 7805 reg but I expected a change when the chip is sleeping that would be measureable?