Using the TIME feature on a 14M2

FIREMANJIM

New Member
Does the TIME function work in simulator mode? I am trying to use the Elapsed Time counter on a 14M2 and simply can not get it to work. Doing something like this:

W0 = TIME ' recalling the TIME to W0
If w0 >= 3600 Then ' if 60 minutes has passed then 60 sec x 60 min = 3600
High CamPwr ' turn on camera
Pause 500
Low CamPwr
Pause 6000
High CamPwr ' turn off camera
Pause 500
Low CamPwr
w0 = 0 ' clear w0
Time = 0 'reset timer to 0
EndIf


What am I doing wrong here? It will not read the TIME and function in simulation mode....
 

lbenson

Senior Member
This screen shot from PE6 proves that the time variable works (at least after a fashion) in the PE6 simulator:
23138
(Not counting the time taken up by DO, LOOP, and SERTXD, and perhaps the simulation delay time.)

How long do you think it should take in simulation for an hour to pass?
 

lbenson

Senior Member
This will run in the simulator. Your posted code must be in a DO LOOP so that it recurs. The PAUSE 100 would not be required on a real chip (though I think it wouldn't hurt), but without it in the simulator it takes well over a minute for TIME to increment (note that I also changed the comparison from W0>= 3600 to W0>=6, since I didn't want to wait an hour (or in fact many hours in simulation) to see something happen). I have the simulation delay time set to 30ms (the lowest which would show the cursor scrolling through the code), and the Code Explorer tab clicked so that I could see W0 incrementing.
Code:
#picaxe 08M2
symbol CamPwr=C.4
#terminal 4800
do
  W0 = TIME ' recalling the TIME to W0
  If w0 >= 6 Then ' if 60 minutes has passed then 60 sec x 60 min = 3600
    High CamPwr ' turn on camera
    Pause 500
    Low CamPwr
    Pause 6000
    High CamPwr ' turn off camera
    Pause 500
    Low CamPwr
    Time = 0 'reset timer to 0
  EndIf
  pause 100
loop
23139
I increased the PAUSE time to 1000 after HIGH CamPwr so that I would have a better chance of catching a screen shot of C.4 colored green in the Simulation image of the 08M2.

Note that you don't have to use W0--you can compare TIME directly: IF TIME => 3600 THEN
 
Last edited:

AllyCat

Senior Member
Hi FiremanJim
,
You don't appear to have put the instructions in a loop, so they will only execute once (and the program stop).

The following works fine for me in PE5, almost in "real time":
Code:
#terminal 4800
do
    if time > 4 then
        sertxd("tick ")
        pause 5000
        sertxd("tock ")
        time = 0
    endif
loop
But PE6 seems to run horribly slowly (at least on my ancient netbook) and appears to need a sertxd(#time," ") after the "do" to see what's happening.

Cheers, Alan.
 
Last edited:

AllyCat

Senior Member
Hi again,

Hmm, it seems to be an "oddity" with PE6. If you use the following code, it runs "slowly", i.e taking a few times longer than "real time". But if you comment out the first sertxd, then it takes literally many minutes (I needed to wait more than 5 minutes) before a "tick " appears.
Code:
#terminal 4800
do
    sertxd(#time," ")   ; Try commenting this out.
    if time > 4 then
        sertxd("tick ")
        pause 5000
        sertxd("tock ")
        time = 0
    endif
loop
Cheers, Alan.
 

lbenson

Senior Member
Hmm, it seems to be an "oddity" with PE6. If you use the following code, it runs "slowly", i.e taking a few times longer than "real time". But if you comment out the first sertxd, then it takes literally many minutes (I needed to wait more than 5 minutes) before a "tick " appears.
I saw that also. It seems as though PE6 tries to simulate how many instructions would have been processed on a real chip for a given TIME tick, rather than how long it takes in real time with the simulator.
 

hippy

Technical Support
Staff member
The 'time' variable when simulated tracks elapsed time based on the execution times of instructions being simulated, so a loop with no PAUSE will have an elapsed time of very little, and will take many many loops to indicate an elapsed second.

This is how it is on a real chip, and when simulating, but it will take an awful long time to complete those loops on the screen.

In order to simulate with 'time' more closely tracking real time, using a reasonably large value PAUSE will help. Those will kick the internal elapsed time value by the amount specified and cause the 'time' variable to follow suit.

One can write the code so the PAUSE is only executed when simulated as below -
Code:
#Picaxe 14M2
#Terminal 4800
w0 = -1
Do
  If w0 <> time Then
    w0 = time
    SerTxd( #w0," " )
  End If
  #IfDef SIMULATING
    Pause 250
  #EndIf
Loop
 

hippy

Technical Support
Staff member
This is the program structure I would use for something which took a photo every hour ...
Code:
#Picaxe 14M2
#Terminal 4800

MainLoop:
  Do
    Do
      #IfDef SIMULATING
        Pause 500
      #EndIf
    Loop Until time >= 3600
    time = 0
    Gosub TakePhoto
  Loop

TakePhoto:
  SerTxd("Smile ... " )
  Pause 5000
  SerTxd( "Click!", CR, LF )
  Return
Note where 'time=0' has been placed; before taking the photo. That more ensures a photo is taken every hour. Otherwise, if it's after taking the photo, it becomes every hour plus how long it takes to take a photo. But that might not matter,
 

FIREMANJIM

New Member
It's in a loop on the whole code. I will post the code in a few. Its a program that runs a trail camera with a pir sensor. We have always just used the Inc command and timed it out with a Nap 3....I just thought the TIME is always running anyway so I wanted to use that instead.
 

FIREMANJIM

New Member
Here is the complete code:

Code:
#Picaxe 18M2
Symbol DIP3 = pinB.0
Symbol DIP2 = pinB.1
Symbol DIP1 = pinB.2
Symbol DayLens = B.3
Symbol CamSht = B.4
Symbol CamPwr = B.5
Symbol Acc3 = B.6
Symbol NotUsed = B.7
Symbol CDSPwr = C.0
Symbol CDSRead = C.1
Symbol PIRInput = pinC.2
Symbol NightLens = C.3
Symbol DIP4 = pinC.5
Symbol NotUsed1 = C.6
Symbol LED = C.7
'list inputs and outputs
Low B.3
Low B.4
Low B.5
Low B.6
Low B.7
Low C.0
Low C.3
Low C.6
Low C.7
Input B.0
Input B.1
Input B.2
Input C.2
Input C.5
'assign variables
Symbol DayStatus = bit0
Symbol CameraOn = bit1
Symbol ExcPos = bit2
Symbol TrailMode = bit3
Symbol DaySetting = b2
Symbol NightSetting = b3
Symbol FeederModeDelay = b4
Symbol Counter = b5
Symbol LightLevel = w2
Symbol CameraShutterDelay = w3
Symbol WalktestTimer = w4
Symbol TrailModeTimer = w5
Symbol CDSTimer = w6
'set default variable settings
CameraOn = 0
DayStatus = 1
ExcPos = 1
DaySetting = 50
NightSetting = 30
Nap 3
If DIP1 = 1 and DIP2 = 1 Then
    FeederModeDelay = 7    '15 second delay
ElseIf DIP1 = 1 and DIP2 = 0 Then
    FeederModeDelay = 14    '30 second delay
ElseIf DIP1 = 0 and DIP2 = 1 Then
    FeederModeDelay = 27    '1 minute delay
ElseIf DIP1 = 0 and DIP2 = 0 Then
    FeederModeDelay = 79    '3 minute delay
EndIf
Nap 3
If DIP3 = 1 Then        'dip 3 is off
    TrailMode = 1    'run in trailmaster moode
ElseIf DIP3 = 0 Then
    Trailmode = 0     'run if feeder mode
EndIf
Nap 3
If DIP4 = 1 Then    'dip 4 is off
    CameraShutterDelay = 2000
ElseIf DIP4 = 0 Then     'dip 4 is on
    CameraShutterDelay = 2300
EndIf
Nap 3
High LED
Pause 1000
High CamPwr
Pause 500
Low CamPwr
CameraOn = 1
High NightLens
Pause 250
Low NightLens
ExcPos = 0
Pause 1000
High DayLens
Pause 250
Low DayLens
ExcPos = 1
Pause 1000
High NightLens
Pause 250
Low NightLens
ExcPos = 0
Pause 1000
High DayLens
Pause 250
Low DayLens
ExcPos = 1
Pause 10000
High CamPwr
Pause 500
Low CamPwr
CameraOn = 0
Pause 1000
Low LED
Nap 3
        High    CdsPwr        'apply power to CDS sensor
        Nap 5            'allow power to settle
        Readadc    CDSRead, LightLevel
        Low CDSPwr
        If LightLevel >= DaySetting then
          DayStatus = 1
          CDSTimer = 0
        If ExcPos = 0 Then
            High DayLens
            Pause 250
            Low DayLens
            ExcPos = 1
        EndIf
        ElseIf LightLevel < NightSetting then
            DayStatus = 0
            CDSTimer = 0
        If ExcPos = 1 Then
            High NightLens
            Pause 250
            Low NightLens
            ExcPos = 0
        EndIf
    EndIf
Nap 3
GoTo Walktest

Walktest:

Inc WalktestTimer
IF PIRInput = 1 Then
    High LED
    Nap 6        'pause 1.1 seconds
    Low LED
    Nap 8     'low power pause for 4 seconds for sensor to settle
    WalktestTimer = 0
EndIF
IF WalktestTimer >= 208 Then
    For Counter = 1 to 10
    High LED
    Pause 300
    Low LED
    Pause 300
    Next Counter
    Nap 3
    GoTo MainProgramLoop
EndIF
Nap 3
GoTo Walktest

MainProgramLoop:

If PIRInput = 1 Then
    
    If TrailMode = 0 Then    'run in feeder mode
        If CameraOn = 0 Then
            Pause 10   
            High CamPwr    ' Turn Camera ON
            Pause 500
            Low CamPwr
            CameraOn = 1
            Pause CameraShutterDelay
        EndIf
        High CamSht            ' Take a Picture
        Pause 3000
        Low CamSht
        Sleep 5
        High CamPwr        ' Turn Camera OFF
        Pause   500
        Low CamPwr
        CameraOn = 0
        TrailModeTimer = 0
        Time = 0
        Sleep FeederModeDelay
    EndIf

    If TrailMode = 1 Then    'run in trailmaster mode
        If CameraOn = 0 Then
            Pause 10   
            High CamPwr    ' Turn Camera ON
            Pause   500
            Low CamPwr
            CameraOn = 1
            Pause CameraShutterDelay
        EndIf
        High CamSht            ' Take a Picture
        Pause 3000
        Low CamSht
        Pause   1000
        TrailmodeTimer = 0
        If DayStatus = 0 Then
            Pause 8000
        ElseIf DayStatus = 1 Then
            Pause 1000
        EndIf
    EndIF
EndIf

If CameraOn = 1 Then
    Inc TrailmodeTimer
    If TrailmodeTimer >= 300 Then 'Keep Camera on for 30 seconds for Activity Mode
        High CamPwr        ' Turn Camera ON
        Pause 1000
        Low CamPwr
        CameraOn = 0
        TrailmodeTimer = 0
        Time = 0
        Sleep 2
    EndIf
EndIF

If Time >= 3600 Then
    If DayStatus = 0 Then
        High CamPwr                ' Turn Camera ON
        Pause 500
        Low CamPwr
        Sleep 5
        High CamPwr                ' Turn Camera ON
        Pause 500
        Low CamPwr
        Nap 3
    EndIf
    Time = 0
    Pause 1000
EndIF

Inc CDSTimer
If CDSTimer >= 4680 Then    'check CDS approximately ever 10 minutes
    Nap 3
    High CDSPwr
        Nap 5    ' allow power to settle
        Readadc    CDSRead, LightLevel
        Low CDSPwr
        If LightLevel >= DaySetting Then
            DayStatus = 1
            CDSTimer = 0
        If ExcPos = 0 Then
            High DayLens
            Pause 250
            Low DayLens
            ExcPos = 1
        EndIf
        Pause 1000
        ElseIf  LightLevel < NightSetting Then
            DayStatus = 0
            CDSTimer = 0
        If ExcPos = 1 Then
            High NightLens
            Pause 250
            Low NightLens
            ExcPos = 0
        EndIf
        Pause 1000
    EndIf
EndIf
NAP 3   
GoTo MainProgramLoop                ' Do it forever
End
 

FIREMANJIM

New Member
Basically the TIME is needed for refreshing the flash on the camera. If no critter walks in front of the sensor for and hour if its dark outside it will turn on the camera for a few seconds to refresh the flash capacitor inside the camera. Hope this makes sense. I am new to all this and learning. What does the #Terminal 4800 I see you guys using...what does that do??
 

lbenson

Senior Member
#Terminal 4800 sets the baud rate for the serial terminal on which output produced by SERTXD is displayed--4800 baud for running at the normal 4mHz, 9600 for 8mHz operation, etc.
 
Top