Program starting unreliability

Hello
I have a simple program on a Pic 18m2 that operates an electric door.
The program works correctly, except it does not always start instantly when the input is applied, rather it waits for it to be applied then removed. The trigger is from a keypad which is about 3 seconds. Entering the correct code the door does not start opening/closing until the keypad resets, which is a short delay but annoying. I can not see where I have gone wrong, in the simulator it operates straight away.
I can rewrite the code differently without the waiting for an input loop, but I can't see where I have gone wrong. If someone can shine some light on my problem I would be most grateful to know what I am doing wrong.
Code:
start0:
'picaxe 18m2
'Bay 3 door operator, mains motor switching, with meros trigger and contact monitoring on relay
'NO devices to open or close doors depending on contact position
'Safety beam stops motor either way


Symbol timepot        = c.0     ; on board pot for motor time
symbol trigger        = pinc.1    ; no to 5v
symbol safety_beam     = pinc.2    ;nc to 5v
symbol door_contact    = pinc.5    ;nc to 5v
 

symbol power_relay     = B.1       ; 1st relay switches live to relay 2
symbol direction_relay     = b.2     ; 2nd relay, NO is open output, NC is close contacts
symbol door_contact_relay = b.3        ; nc to operate meros
symbol safety_out        = b.4        ; output when beam broken relay to connect to other doors

symbol greenled         = b.7        ; on when safety closed off when beam blocked Panel LED

symbol timestore        = b1
symbol opentime        = b2
symbol closetime        = b3

pause 1000

Main:           
    pause 10



do while trigger = 0    ; waiting for an input to open or close door
    
        pause 50       
        
            do while safety_beam = 0 ;if beam blocked door will not trigger
                
                pause 50
                        
            loop
    
                    
    
loop


if door_contact = 1 then goto opening_time 'door opens

if door_contact = 0 then goto closing_time 'door closes
    
    
opening_time:    ; open door loop

readadc timepot, opentime    ;load running time

do while opentime > 0     ;timing loop
    
    high power_relay    ; door goes up

        if safety_beam = 0 then goto safety_Stop 'safety beam operation

            dec opentime 'counting down

                pause 150 ' gives about 30 secs max operating time

Loop

low power_relay        ; end of time relay off

goto main
        
    


closing_time:    ;door closing loop

readadc timepot, closetime


do while closetime > 0
    
    high power_relay, direction_relay ; with two relays on door coming down

        if safety_beam = 0 then goto safety_Stop

            dec closetime

                pause 150

Loop

low power_relay, direction_relay ; both relays off

goto main



Safety_stop:    'if beam is broken this stops motor and clears variables

Low power_relay, direction_relay, safety_out    ;everything off

    pause 1000

        opentime = 0    ; clears time
            closetime = 0    ;;clears time

                pause 50

goto main    ; go back to needing an input
    

start1: 'controls meros relay and safety output

if door_contact = 1 then high door_contact_relay else low door_contact_relay endif

    Pause 50

        if safety_beam = 1 then low safety_out else high safety_out endif

            Pause 50

                if safety_beam = 1 then high greenled else low greenled end if

                    pause 50

goto start1
 

lbenson

Senior Member
Do you have pulldowns (e.g., 10K to 0V) on pinC.1, pinC.2, pinC.5? A program which has switch inputs which works reliably in the simulator but not in the field is often an indicator that an input is "floating" and may be in an undefined state when the switch is not activated.

If that's not your issue, we may need to see your circuit layout.
 
I use Picaxe 18 project boards so all built in, I also fit an extra 0.1 cap across the PIC power pins as well. The relays are the transistorised Ebay type.
If you can't see a problem in the program then I will rewrite it without the loops and see how that goes.
Thank you
 

lbenson

Senior Member
I've reformatted in a more conventional style. I don't see anything obviously wrong.
Code:
#picaxe 18m2
Start0:
'Bay 3 door operator, mains motor switching, with meros trigger and contact monitoring on relay
'NO devices to open or close doors depending on contact position
'Safety beam stops motor either way

Symbol timepot        = c.0     ; on board pot for motor time
symbol trigger        = pinc.1    ; no to 5v
symbol safety_beam     = pinc.2    ;nc to 5v
symbol door_contact    = pinc.5    ;nc to 5v
 
symbol power_relay        = B.1     ; 1st relay switches live to relay 2
symbol direction_relay    = b.2     ; 2nd relay, NO is open output, NC is close contacts
symbol door_contact_relay = b.3     ; nc to operate meros
symbol safety_out         = b.4     ; output when beam broken relay to connect to other doors
symbol greenled           = b.7     ; on when safety closed off when beam blocked Panel LED
symbol timestore          = b1
symbol opentime           = b2
symbol closetime          = b3
pause 1000

Main:           
  pause 10
  do while trigger = 0    ; waiting for an input to open or close door
    pause 50       
    do while safety_beam = 0 ;if beam blocked door will not trigger
      pause 50
    loop
  loop

  if door_contact = 1 then goto opening_time 'door opens
  if door_contact = 0 then goto closing_time 'door closes
    
opening_time:    ; open door loop
  readadc timepot, opentime    ;load running time
  do while opentime > 0     ;timing loop
    high power_relay    ; door goes up
    if safety_beam = 0 then goto safety_Stop 'safety beam operation
    dec opentime 'counting down
    pause 150 ' gives about 30 secs max operating time
  Loop
  low power_relay        ; end of time relay off
  goto main
        
closing_time:    ;door closing loop
  readadc timepot, closetime
  do while closetime > 0
    high power_relay, direction_relay ; with two relays on door coming down
    if safety_beam = 0 then goto safety_Stop
    dec closetime
    pause 150
  Loop
  low power_relay, direction_relay ; both relays off
  goto main

Safety_stop:    'if beam is broken this stops motor and clears variables
  Low power_relay, direction_relay, safety_out    ;everything off
  pause 1000
  opentime = 0    ; clears time
  closetime = 0    ;;clears time
  pause 50
  goto main    ; go back to needing an input
    
start1: 'controls meros relay and safety output
  if door_contact = 1 then high door_contact_relay else low door_contact_relay endif
  Pause 50
  if safety_beam = 1 then low safety_out else high safety_out endif
  Pause 50
  if safety_beam = 1 then high greenled else low greenled end if
  pause 50
  goto start1
What is meant by "keypad resets" in this statement: "Entering the correct code the door does not start opening/closing until the keypad resets"?

How is that signified in the program?
 

PhilHornby

Senior Member
Could this be as simple as power impulses (caused by triggering the door opening mechanism) causing the Picaxe to reset?

(I've been there and got the T shirt for something similar, involving a Fridge Compressor ;) )
 
Thank you Ibenson and PhilHornby.
Both correct motor interferance causing the problem, fitted an X2 filter across the open/close contacts and reliability has returned.

I had two parts of the program operating "safety_out" B.4, this blip made me think it was the program.

Thanks for the prompt and correct help.

There is always something more....

Can someone show me a way to use a press switch in two modes: so a quick press it does function A, press and hold it does function B. It would need the long press to not activate the short press function?

Thank you
 

lbenson

Senior Member
With experimentation to find out what the cutoff is, something like this could work (adjust count variable):
Code:
do while pinC.whatever=0: loop: bXX=0: do while pinC.whatever=1: inc bXX: loop
if bXX > ?? then
...
else
...
endif
You might need a PAUSE of some duration before inc bXX

(Input must be debounced, perhaps with hardware.)
 
Top