PDA

View Full Version : Delay problem



lloyd778
- 2nd September 2006, 17:04
Hi all, my first post on this great site, so be gentle with me...
I need to embed a program in the Olimex PIC I/O board. It has 4 Inputs and 4 relay Outputs. I am using a 16F628A. Using PBP. I use several of these boards in my little welding shop and always seem to have the same problem with Basic language programming. The relay response time when the input changes state is affected by the Pause command. I want to turn on a vacuum, pause, turn on a light, pause, turn on a power feed, pause, turn on a saw. I flip the switch on -vac comes on followed by the other relays OK. But there is that darned delay of up to 3 seconds at the beginning of each cycle. When the switch is turned off the saw MUST shut down immediately. But there is that darned delay again. The more Pause I include in the program the more delay I get at the beginning of each cycle. I can program the same hardware in Ladder Logic and not get the delay but I want to use PBP. Please tell me what the fix is. It's usually simple, I know, but 6 Saturdays have been invested with little result.
My latest (cleaned up) incantation:

pause 50
define OSC 20
CMCON = 7 ' digital I/O
TRISA = %00010000
TRISB = %00111001

counter var word

loop:
toggle portb.5 'Cycle on/off Power-On LED
if portb.0 = 1 then 'Switch is Off
gosub sub1
else
portb.0 = 0 'Switch is On
gosub sub2
endif
goto loop
sub1:
low 11 'Turn Off SAW immediately
gosub delay
low 10 'Turn Off power feed
gosub delay
low 9 'Turn Off Light
gosub delay
low 8 'Turn Off Vacuum
goto loop
sub2:
high 8 'Turn On Vacuum
gosub delay
high 9 'Turn On Light
gosub delay
high 10 'Turn On Power Feed
gosub delay
high 11 'Turn On SAW
goto loop

delay:
counter=32
repeat
pause 50
counter=counter-1
until counter=0
return

end

As I said, this works, but when I flip the switch off I really need the saw to stop immediately. Thanks to all of you. lloyd778

Melanie
- 2nd September 2006, 18:50
Well isn't the important part NOT to have any delays?



loop:
if portb.0 = 1 then ' Switch is OFF
gosub sub2
else ' otherwise it's ON
gosub sub1
endif
Goto Loop

sub1:
low 11 ' Turn Off SAW immediately
low 10 ' Turn Off power feed
low 9 ' Turn Off Light
low 8 ' Turn Off Vacuum
Return

sub2:
high 8 ' Turn On Vacuum
high 9 ' Turn On Light
high 10 ' Turn On Power Feed
high 11 ' Turn On SAW
Return

end


And if you want a blinky Light as well (which now introduces a maximum 10mS delay in this example)...



counter=0
loop:
if portb.0 = 1 then ' Switch is OFF
gosub sub2
else ' otherwise it's ON
gosub sub1
endif
counter=counter+1
If Counter=>100 then
' approx 1 second toggle
toggle portb.5
Counter=0
endif
Pause 10
Goto Loop

sub1:
low 11 ' Turn Off SAW immediately
low 10 ' Turn Off power feed
low 9 ' Turn Off Light
low 8 ' Turn Off Vacuum
Return

sub2:
high 8 ' Turn On Vacuum
high 9 ' Turn On Light
high 10 ' Turn On Power Feed
high 11 ' Turn On SAW
Return

end


Why not also ALIAS your Ports, so they're easier to program... at the start you declare your aliases...

LED var PortB.5

then in your program you can write Toggle LED instead of Toggle PortB.5

The bonus is, if you then decide to move the LED to a different port/pin, you make your change on ONE line of your program, and hey-presto, it doesn't matter how many times you've used LED, they all get reassigned.

Likewise alias, VACUUM, POWERFEED, LIGHT and SAW and you can then go...

Low SAW
Low POWERFEED
Low LIGHT
Low VACUUM

which makes fare more easier program understanding...

lloyd778
- 2nd September 2006, 19:09
Thank you for your reply. I do need the delays. If I turn on all the tools at once I will blow a fuse. I need the vac to stay on a few seconds after the saw shuts down to collect the dust. The blinky light just lets me know that it's getting power and to remind me of the hot mains connected to the relays(and lately I've been using it to see the cycle time). Blinky light is not so important but a few one second pauses without the latency would be so nice. How can I get a pause in the main program without the delay at the beginning of the cycle?

lloyd778
- 2nd September 2006, 20:39
I organized it up a bit. The blinky no worky, so I went back to the simple toggle. I aliased everything that I could. Looks better but works the same. Any suggestions?


'* Name : relay4.BAS
'* Author : lloyd778
'* Notice : Copyleft
'* : No Rights Reserved
'* Date : 7/12/2006
'* Version : 1.0.25(+/-)
'* Notes : for Olimex 4 relay board (PIC I/O) 16F628A
'* Relay board incantation #25 at least
' Use of chicken bones and water buffalo entrails show no
' substantial improvement so far.

pause 50
define OSC 20
CMCON = 7 ' digital I/O
TRISA = %00010000
TRISB = %00111001
LED var portb.5
Saw var porta.3
Light var porta.1
Feed var porta.2
Vac var porta.0
Switch var portb.0
counter var byte
counter1 var byte

loop:
toggle led 'Power LED shows cycle time
if switch = 1 then ' Switch is OFF
gosub sub1
else 'otherwise it's ON
gosub sub2
endif

Goto Loop
sub1:
low saw
gosub delay
low feed
gosub delay
low light
gosub delay
low vac
goto loop
sub2:
high vac
gosub delay
high light
gosub delay
high feed
gosub delay
high saw
goto loop

delay:
counter=32
repeat
pause 50
counter=counter-1
until counter=0
return

end

Melanie
- 2nd September 2006, 23:35
So you put your delays in the turn-on and NOT in the turn-off routines. You won't blow a fuse switching OFF.

btw... your code is shot! If you call a GOSUB then you terminate the subroutine with a RETURN (see my example), you don't exit back with a GOTO.

All that happens is you fill up your stack with unpredictable results.

niels
- 8th September 2006, 13:20
I organized it up a bit. The blinky no worky, so I went back to the simple toggle. I aliased everything that I could. Looks better but works the same. Any suggestions?

Your problem is in Sub2. The delays you are using are preventing the switch from being read. What you want to do is check the status of the switch inside the delay routine. If you see the switch go off, jump to Sub1. You need to keep checking the status of the switch - and the easiest place to do this is inside the delay routine itself.

lloyd778
- 8th September 2006, 17:25
Yes, that was exactly my problem all along. Thank you niels.

Archangel
- 10th September 2006, 06:50
' Use of chicken bones and water buffalo entrails show no
' substantial improvement so far.


You needed BLACK CAT BONES, and some HIGH JOHN THE CONQUERS ROOT AS WELL! OH AND A CHICKEN FOOT TOO! In liew of actual working code.

As a woodworker I say; Good Idea ! :-)

Edit: in retrospect, I think this device should be configured as a slave to the power tools magnetic power switch, Darn those sharp cutters, I think I would not trust a PIC with my fingers!

niels
- 11th September 2006, 00:08
I would not trust a PIC with my fingers!
I'd trust a PIC- the real question is would you trust the code. A big red mechanical 'kill-power' button in series with the saw wouldn't be a bad idea.

Archangel
- 11th September 2006, 01:39
I'd trust a PIC- the real question is would you trust the code. A big red mechanical 'kill-power' button in series with the saw wouldn't be a bad idea.

It is not the pic or code per-se' I don't trust, it's power surges, stray capacitance, you name it . . . gremlins, I'm just saying when my fingers are near the moving parts, I want the isolation of an unpowered relay protecting them. I have more scars than Tim Allens Charactor Tim THE TOOLMAN, and still lucky enough to count to ten.

Now all that said I stayed up till 4:00 am making a program do exactly what he wanted as the whole thing intrigued the heck out of me! So the real question is :
Who's having fun out here?

Ioannis
- 27th September 2006, 06:34
You may change all the commands in the sub delay with a single pause 1600 (32x50).

Also you really need to change goto loop in the subs with return as Melanie said.

Ioannis

Archangel
- 26th October 2006, 06:42
Hi Lloyd,
here is my version as promised to you in private email.
If anyone uses this code to control machinery they do so at their own risk, I strongly recommend a hard wired power switch.
'************************************************* *****************
'* Name : Saw.BAS ***
'* Author : [Joe Stokes] ***
'* Notice : Copyright (c) 2006 Joe Stokes ***
'* : All Rights Reserved ***
'* Date : 9/9/06 ***
'* Version : 1.0 ***
'* Notes :This program delays vacuum, lights and power feed ***
'* :For a wood working tool. ***
'* :You may use this code for your personal devices ***
'* :without assigning any liability to me, and for non ***
'* :commercial purposes only. I recommend a positive ***
'* :mechanical power interrupting device for safety! ***
'* :Power tools are dangerous, and may inflict grave ***
'* :injury and or death. Your safety is your personal ***
'* :responsibility. Please enjoy the use of this code. ***
'* :It is my gift to you. ***
'************************************************* *****************

start:
' Change as applicable to your needs - a faster osc may make saw off happen even
' faster, certainly be worth a try. There are ports left over for ???

@ DEVICE pic16F84A, XT_OSC
@ DEVICE pic16F84A, WDT_OFF
@ DEVICE pic16F84A, PWRT_OFF
@ DEVICE pic16F84A, PROTECT_OFF

DEFINE OSC 4
PORTA = %00000001
PORTB = %00000000
TRISA = %00000000
TRISB = %00000000

symbol Power = PortA.0
symbol Saw = PortB.0
symbol Light = PortB.1
symbol Vacuum = PortB.2
symbol Feed = PortB.3
symbol Radio = PortB.4
symbol Sign = PortB.5
Flash var byte
main:

loop: if portA.0 = 0 then 'Switch is off - stop motors if running keep off if off.
gosub sub1
else
if portA.0 = 1 then ' Switch is On
gosub sub2 ' Begin power on sequence
else
goto loop ' endlessly check for power on condition
endif
endif


sub1: ' This routine stops the motors, lights and returns power
' to the shop's radio.


low Saw ' Turn Off SAW immediately
low Feed ' Turn Off power feed too
pause 5000 ' Five seconds for vacuum to remove dust from saw internals

low saw
low Light ' Turn Off Light
low Vacuum ' Turn Off Vacuum
high Radio ' Turn radio back on
low Sign ' Turn off Danger sign

goto loop ' Check for a power on request.



sub2:

for Flash = 5 to 0 step -1 ' Change the 5 to any # of flashes you like!
High Sign ' Turn on Danger Sign reminds family not to distract you!
pause 250 ' For Next loop flashes sign prior to saw starting
low Sign
pause 250
next
goto sub3 ' This is how I stopped the flashing light!

sub3: high sign ' Sign goes on continuously
low Radio ' Turn off radio, no distractions while cutting.
if portA.0 = 0 then
gosub sub1 'check off switch for off request
else

high Saw ' Turn On SAW, shaper, other power tool.
if portA.0 = 0 then
gosub sub1 ' Check off switch for off request
else
pause 1000 ' Time for saw to get up to speed and mains to settle down

if portA.0 = 0 then
gosub sub1 ' Check off switch for off request during delay.
else
pause 1000 ' Total 2 seconds delay for mains to settle.
high Vacuum ' Turn On Vacuum

if portA.0 = 0 then
gosub sub1 ' Check off switch for off request, keeping it safe
else
pause 1000 ' Time for vacuum motor to speed up and mains to settle
high Light ' Turn On Light
' Time for light to to come up to heat and mains to settle


if portA.0 = 0 then
gosub sub1 ' Check off switch for off request, still keeping safe.
else
pause 500
high Feed ' Turn On Power Feed, time to do some work!
endif
endif
endif
endif
endif
goto sub3 ' Loop back to sub3 where off command is located.

end ' I gotta keep the compiler happy!