Trouble with Time variable

okasional

New Member
In a section of code that counts manual switch presses, I'm trying to use Time to exit if the switch remains idle for 5 seconds.

Code:
#Picaxe 08M2  			 			
 #No_Data      						
 Setfreq M4			 				
 		
 symbol SW1 = pinc.3	 					  					
 symbol UHF = pinc.4  	  
 low c.0
 low c.1
 low c.2
 
 IDLE:
     	enabletime
	time = 0
	do:loop until SW1 = on or time => 5 
		if SW1 = on then goto BLINK
		if time =>5 then END
	endif
	goto IDLE

  BLINK:
	do:loop until SW1 = off
	high c.0
	pause 2000
	low c.0
  goto IDLE
During simulation, the program waits forever in the do:loop, as if the time function is not counting seconds. The "BLINK" stuff was added just to watch it in simulation. No problem as long as I keep pressing and releasing SW1, but it will wait forever without timeout if I stop. HELP!

Tommy Tyler
 

SAborn

Senior Member
I think the problem is you keep resetting the timer on every loop due to where you have the " TIME = 0 "
 

inglewoodpete

Senior Member
In a section of code that counts manual switch presses, I'm trying to use Time to exit if the switch remains idle for 5 seconds.

During simulation, the program waits forever in the do:loop, as if the time function is not counting seconds. The "BLINK" stuff was added just to watch it in simulation. No problem as long as I keep pressing and releasing SW1, but it will wait forever without timeout if I stop. HELP!

Tommy Tyler
To summarise your code as posted, "If the switch is pressed and released, flash the LED on then off. If the switch is not pressed, wait until Time = 5 (seconds). However, on every loop, reset Time to 0." In other words, loop for ever if the switch is not pressed.

When developing code, it often helps to include #Terminal 4800 at the top of your code and some strategically placed SerTxd commands in your code:
Code:
[color=Navy]#Picaxe [/color][color=Black]08M2                  [/color]
[color=Navy]#No_Data
#Terminal 4800
      
 [/color][color=Blue]symbol [/color][color=Purple]SW1 [/color][color=DarkCyan]= [/color][color=Purple]pinc.3                               
 [/color][color=Blue]symbol [/color][color=Purple]UHF [/color][color=DarkCyan]= [/color][color=Purple]pinc.4      
 
 [/color][color=Blue]Setfreq M4                   
 low c.0
 low c.1
 low c.2
 
 [/color][color=Black]IDLE:
   [/color][color=Blue]enabletime
   [/color][color=Purple]time [/color][color=DarkCyan]= [/color][color=Navy]0
   [/color][color=Blue]SerTxd([/color][color=Red]"Time = "[/color][color=Black], #[/color][color=Purple]Time[/color][color=Black], [/color][color=Red]" SW1 = "[/color][color=Black], #[/color][color=Purple]SW1[/color][color=Black], [/color][color=Blue]CR[/color][color=Black], [/color][color=Blue]LF)[/color][color=Green]'Just for debugging
   [/color][color=Blue]do[/color][color=Black]:[/color][color=Blue]loop until [/color][color=Purple]SW1 [/color][color=DarkCyan]= [/color][color=Blue]on [/color][color=DarkCyan]or [/color][color=Purple]time [/color][color=DarkCyan]=> [/color][color=Navy]5 
   [/color][color=Blue]if [/color][color=Purple]SW1 [/color][color=DarkCyan]= [/color][color=Blue]on then goto [/color][color=Black]BLINK
   [/color][color=Blue]if [/color][color=Purple]time [/color][color=DarkCyan]=>[/color][color=Navy]5 [/color][color=Blue]then END[/color][color=Black]: [/color][color=Blue]endif
   goto [/color][color=Black]IDLE

  BLINK:
   [/color][color=Blue]do[/color][color=Black]:[/color][color=Blue]loop until [/color][color=Purple]SW1 [/color][color=DarkCyan]= [/color][color=Blue]off
   high c.0
   pause [/color][color=Navy]2000
   [/color][color=Blue]low c.0
  goto [/color][color=Black]IDLE[/color]
 

hippy

Technical Support
Staff member
The simulation runs in PE5 but hangs in PE6. Technical/hippy?
Depends what you mean by "hang". I suspect you mean that with PE6 'time' isn't reaching 5 after 5 seconds of simulating, but you will probably find that PE6 is running and as responsive as it usually is.

Time simulates vveerryy slowly, far far far far far slower than on a 'real' device?
That is correct. The 'time' variable is determined from a rough approximation of what is being executed.

In the following the "DO-LOOP" is considered to execute in mere microseconds, so there has to be thousands of executions of that before the simulator considers 5 seconds to have elapsed ...

Code:
SerTxd( "Started" )
time = 0
Do : Loop Until pinC.3 = 1 Or time >= 5
SerTxd( "Ended" )
The solution is to add a PAUSE which the simulator can use to move simulated time closer to real elapsed time ...

Code:
SerTxd( "Started" )
time = 0
Do : [b]Pause 500[/b] : Loop Until pinC.3 = 1 Or time >= 5
SerTxd( "Ended" )
To make that responsive in a real chip and to have simulated 'time' running at closer to elapsed time ...

Code:
SerTxd( "Started" )
time = 0
Do 
  [b]#IfDef SIMULATING
    Pause 500
  #EndIf[/b]
Loop Until pinC.3 = 1 Or time >= 5
SerTxd( "Ended" )
 
Top