Delay problem


Closed Thread
Results 1 to 12 of 12

Thread: Delay problem

Hybrid View

  1. #1
    Join Date
    Sep 2006
    Posts
    5

    Default Delay problem

    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

  2. #2
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    Well isn't the important part NOT to have any delays?

    Code:
    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)...

    Code:
    	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...

  3. #3
    Join Date
    Sep 2006
    Posts
    5


    Did you find this post helpful? Yes | No

    Default need the pause

    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?

  4. #4
    Join Date
    Sep 2006
    Posts
    5


    Did you find this post helpful? Yes | No

    Default as per your suggestions...

    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

  5. #5
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    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.

  6. #6
    niels's Avatar
    niels Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by lloyd778
    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.

  7. #7
    Join Date
    Sep 2006
    Posts
    5


    Did you find this post helpful? Yes | No

    Default That's it...

    Yes, that was exactly my problem all along. Thank you niels.

  8. #8
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Wink Glad you got it working !

    Quote Originally Posted by lloyd778
    ' 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!
    Last edited by Archangel; - 10th September 2006 at 11:45.

  9. #9
    niels's Avatar
    niels Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Joe S.
    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.

Similar Threads

  1. 16F628A - Stops if release power switch.
    By dene12 in forum General
    Replies: 16
    Last Post: - 14th February 2009, 07:57
  2. Old and beyond help ?
    By DavidFMarks in forum mel PIC BASIC Pro
    Replies: 46
    Last Post: - 11th December 2008, 15:23
  3. need a project for PIC16F84
    By Danish in forum Schematics
    Replies: 6
    Last Post: - 3rd August 2005, 17:51
  4. Memory Space of the PIC16F84...
    By Tear in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 1st July 2005, 19:55
  5. Problem with saving to EEPROM...
    By Tear in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 1st July 2005, 00:10

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts