Stopping/interrupting a command and going on to the next command

craftvfx

New Member
I'm trying to figure out how to stop/interrupt a command that is in progress. During the command I have a servo turn and some lights turn on and stay on for around 10 seconds. I'd like to set it up so that if I press the switch again any time while the lights are still on, it switches them off and goes to the next command.

Heres my code:
Code:
start:
    servo 2,50
	goto switch1
	
'--------------------------
	
close:
    servopos 2,50
    pause 50
    goto lights1

switch1:
		
	if pin3 = 1 then 
        pause 200
        goto open
		 
	else 
		goto switch1
	endif
	
'--------------------------	
   	
	
open:

    servopos 2,1280
  
    
switch2:

   		if pin3 = 1 then
        pause 600
        goto close

    else 
		goto switch2
	endif
	
'--------------------------	


lights1:
    high 0
    pause 400
    low 0
    pause 100
    high 0 
    pause 100
    low 0
    pause 100
    high 0
    pause 100
    low 0 
    pause 100
    high 0
    pause 10000
    low 0
	goto start
    
'--------------------------
 
Last edited by a moderator:

westaust55

Moderator
A simple scheme is to put the PAUSE command with a smaller value into a FOR . . . NEXT loop then test the input each pass and use the EXIT command to break out of the loop if switch is activated.

The basic interrupt scheme (see SETINT command) checks after each command and I recall continuously during a pause.
 

BESQUEUT

Senior Member
There is no simple way to stop a running command. IHMO, you have to rewrite your code.
I would suggest :
- to remove any GOTO command,
- to remove any PAUSE command.
(YES ; this is possible...)
What Picaxe are you using ?
 

erco

Senior Member
You must replace all of your long pauses with calibrated for/next loops which constantly look at your pushbutton switch and GOTO when necessary.
 

BESQUEUT

Senior Member
no GOTO, no PAUSE test program :
Code:
[color=Green]'2017-05-05 JYB
'tested with AXE049[/color]

[color=Navy]#picaxe [/color][color=Black]18m2[/color]
[color=Navy]#no_data[/color]

[color=Blue]symbol [/color][color=Purple]PinState[/color][color=DarkCyan]=[/color][color=Purple]bit0[/color]
[color=Blue]symbol [/color][color=Purple]ServoState[/color][color=DarkCyan]=[/color][color=Purple]bit1[/color]
[color=Blue]symbol Closed[/color][color=DarkCyan]=[/color][color=Navy]0[/color]
[color=Blue]symbol Opened[/color][color=DarkCyan]=[/color][color=Navy]1[/color]
[color=Blue]symbol Light[/color][color=DarkCyan]=[/color][color=Blue]B.5
symbol [/color][color=Purple]Tempo[/color][color=DarkCyan]=[/color][color=Purple]w2

dirC.1[/color][color=DarkCyan]=[/color][color=Navy]0[/color]
[color=Purple]Tempo[/color][color=DarkCyan]=[/color][color=Navy]0[/color]
[color=Purple]ServoState[/color][color=DarkCyan]=[/color][color=Blue]Closed[/color]
[color=Purple]PinState[/color][color=DarkCyan]=[/color][color=Purple]pinC.7[/color]


[color=Blue]do
      if [/color][color=Purple]pinC.7[/color][color=DarkCyan]<> [/color][color=Purple]pinState [/color][color=Blue]then     [/color][color=Green]' Detect pin state change
            [/color][color=Blue]if [/color][color=Purple]pinC.7[/color][color=DarkCyan]=[/color][color=Navy]1 [/color][color=Blue]then        [/color][color=Green]' pin has changed from 0 to 1
                  [/color][color=Blue]high b.1          [/color][color=Green]' only to show pin state
                  [/color][color=Blue]gosub [/color][color=Black]ToggleServo
            [/color][color=Blue]else
                  low b.1           [/color][color=Green]' only to show pin state
            [/color][color=Blue]endif
            [/color][color=Purple]pinState[/color][color=DarkCyan]=[/color][color=Purple]pinC.7
       [/color][color=Blue]endif
      

      select case [/color][color=Purple]Tempo                   [/color][color=Green]' if Tempo>0 deal with light flashing...
      [/color][color=Blue]case  [/color][color=Navy]999 [/color][color=Black]: [/color][color=Blue]high Light[/color][color=Black]: [/color][color=Blue]dec [/color][color=Purple]Tempo
      [/color][color=Blue]case  [/color][color=Navy]900 [/color][color=Black]: [/color][color=Blue]low Light[/color][color=Black]: [/color][color=Blue]dec [/color][color=Purple]Tempo
      [/color][color=Blue]case  [/color][color=Navy]880 [/color][color=Black]: [/color][color=Blue]high Light[/color][color=Black]: [/color][color=Blue]dec [/color][color=Purple]Tempo
      [/color][color=Blue]case  [/color][color=Navy]860 [/color][color=Black]: [/color][color=Blue]low Light[/color][color=Black]: [/color][color=Blue]dec [/color][color=Purple]Tempo
      [/color][color=Blue]case  [/color][color=Navy]840 [/color][color=Black]: [/color][color=Blue]high Light[/color][color=Black]: [/color][color=Blue]dec [/color][color=Purple]Tempo
      
      [/color][color=Blue]case [/color][color=Navy]1 [/color][color=Black]:[/color][color=Blue]low Light[/color][color=Black]: [/color][color=Blue]dec [/color][color=Purple]Tempo
      [/color][color=Blue]case [/color][color=Navy]0 [/color][color=Green]' do nothing...
      [/color][color=Blue]else [/color][color=Black]: [/color][color=Blue]dec [/color][color=Purple]Tempo
      [/color][color=Blue]endselect

loop[/color]


[color=Black]ToggleServo:
      [/color][color=Blue]if [/color][color=Purple]ServoState[/color][color=DarkCyan]=[/color][color=Blue]Closed then
            high b.2    [/color][color=Green]' only to simulate servo
            [/color][color=Blue]low b.3[/color]
[color=Green]'           servopos 2,1280
            [/color][color=Purple]ServoState[/color][color=DarkCyan]=[/color][color=Blue]Opened
      else
            high b.3    [/color][color=Green]' only to simulate servo
            [/color][color=Blue]low b.2[/color]
[color=Green]'           servopos 2,50
            [/color][color=Purple]ServoState[/color][color=DarkCyan]=[/color][color=Blue]Closed
            [/color][color=Purple]Tempo[/color][color=DarkCyan]=[/color][color=Navy]1000  [/color][color=Green]' Start light flashing
      [/color][color=Blue]endif
return[/color]
 
Last edited:

westaust55

Moderator
Still with PAUSE but no GOTO:

Code:
; we want a delay for 10 sec = 10,000ms ==> 1000 loops of 10 ms
; this will scan the input pin roughly every 11 or 12 msec
FOR w5 = 0 TO 1000
  PAUSE 10
  IF pinC.3 = 1 THEN
    LOW C.0  ; USE SYMBOL Lights = C.0
    EXIT
  ENDIF
NEXT w5
Alternatively as I mentioned at post 2, use the SETINT command for interrupts.
Interrupts are continuously scanned during the PAUSE command.
 

BESQUEUT

Senior Member
; we want a delay for 10 sec = 10,000ms ==> 1000 loops of 10 ms
; this will scan the input pin roughly every 11 or 12 msec
So we have a 12 s delay.
If we want a 10 s delay, we have to calibrate the main loop, says 890 loops.
AT this point, why using a PAUSE command ?
==> We can suppress the PAUSE and calibrate the main loop accordingly, maybe 4000 loops...
====> scan time will be even better (2.5 msec for 4000 loops)...

Alternatively as I mentioned at post 2, use the SETINT command for interrupts.
Interrupts are continuously scanned during the PAUSE command.
Alternatively with an M2 we can also use pseudo multitasking...
 
Last edited:

Pivrnec78

New Member
This post has been moved to its own thread - http://www.picaxeforum.co.uk/showthread.php?29998-Counting-impulses

Hi,
I do not know where to include this problem, so I write it here.
I need to create a PICAXE comparator. I need to load (count) impulses on the pin1. When pin1 appear first impulse then light up LED1. Then I need to load (count) impulses on the pin2.
I do not know the frequency - time will be different. The time between pulse1 and pulse2 will be different from the time between pulse2 and pulse3.
And when pin2 = pin1 then turn off LED1, turn on LED2 and reset pin1 and pin2.
The number of pulses per pin2 will not be greater than pin1.

The "COUNT" command I think can not be used. Because I do not want to count (measure) frequencies, but add and compare the number of impulses.
Now I work with 74HCT193 - 4-Bit Up / Down Counters, but I need to expand. 4-bit be few, I need 8-bit ...
 
Last edited by a moderator:
Top