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.
 

FIREMANJIM

New Member
ok...so I updated the code as you guys suggested and it work on simulation but when I load it on the chip and put it on the board it doesn't work in the project....I am at wits end. Any suggestions???
 

Aries

New Member
In what way does it not work?

It's usually worth adding some sertxd statements in your code so that you can see where the code is going. Add more statements as you identify where it is "going wrong".
 

FIREMANJIM

New Member
I am new to this and don't understand how the sertxd works. I just have figured out the basic stuff you see above...Maybe you guys can teach me how that works....Thanks for all your help.
 

BESQUEUT

Senior Member
ok...so I updated the code as you guys suggested and it work on simulation but when I load it on the chip and put it on the board it doesn't work in the project....I am at wits end. Any suggestions???
Rewrite your code to remove any GOTO command...
 

AllyCat

Senior Member
Hi,

A program "working" in the simulator but not in a real chip often indicates a hardware construction or design mistake, but it might be an operating speed issue (normally the simulator runs hundreds of times slower than a real chip). For example HIGH LED : LOW LED will show on the simulator but the real LED will blink too fast for it to be seen.

The #TERMINAL 4800 (or pressing the f8 key) should give a "pop-up" window in the Program Editor (it worked very reliably in PE5 but there seem to be a few ways it can get "hidden" in PE6). The program can then send "messages" to that window with the SERTXD(...) command.

The first recommendation is to put a SERTXD("Starting",cr,lf) very near the top of the program (perhaps preceded with a PAUSE 3000 to ensure that the terminal is ready). This will confirm that the program is running and in particular show if it gets unexpectedly reset. I sometimes use SERTXD("Battery = ",#w1," mVolts",cr,lf) but that needs rather more advanced programming (I have written a code snippet in that section of the forum).

Then put SERTXD commands throughout the program at "critical" points, particularly before IF commands. For example SERTXD("Waiting for the sensor to be triggered",cr,lf) etc.. In view of the title of this thread, you may need to include a number of SERTXD("Time= ",#time," secs.",cr,lf) throughout the program.

Cheers, Alan.
 

hippy

Technical Support
Staff member
My first recommendation would be to post the code you now have. Otherwise those here can only guess what may be wrong with it.

But don't worry; we'll help you get there :)
 

The bear

Senior Member
Hi FIREMANJIM,
Your code in post #10 looks very involved to me (A beginner).

However I did notice there is a conflict, as shown in 'Code Explorer'.

feedermodedelay (b4) w2, b4
lightlevel (w2) w2, b4, b5
counter (b5) w2, b5
 

Flenser

Senior Member
Firemanjim,

In the description of your project you said:
and timed it out with a Nap 3....I just thought the TIME is always running anyway
This assumption looks to be incorrect.

The manual entry for the SLEEP command describes the microcontroller being put into a low power mode in which all the internal timers are switched off and also that the 'time' variable will not increment.

The manual entry for the NAP command also describes the microcontroller being put into a low power mode in which all the internal timers are switched off but does not mention the 'time' variable.

This code that has a 250ms delay using the pause 250 command:
Code:
#PICAXE 08M2
#no_data
setfreq m4

#Terminal 4800
Do
  w0 = time
  SerTxd( #w0," " )
  Pause 250
Loop
Produces the expected output showing the TIME variable being updated every 4 times through the loop. i.e. updated every second.
Code:
0 0 0 0 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5 6 6 6 6 7 7 7 7 8 8 8 9 9 9 9 10 10 10 10
but this code that has a 288ms delay using the nap 4 command:
Code:
#PICAXE 08M2
#no_data
setfreq m4

#Terminal 4800
Do
  w0 = time
  SerTxd( #w0," " )
  nap 4
Loop
Produces the following output showing that the TIME variable is not being updated during the nap command, exactly as is described for the sleep command.
Code:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Flenser
 
Last edited:

Flenser

Senior Member
You can still use the nap command but you will need to measure time by counting your naps instead of using the time variable.

This code has a delay of about 1s using the nap 6 command:

Code:
#PICAXE 08M2
#no_data
setfreq m4

#Terminal 4800
w0=0
Do
  nap 6
  inc w0
  SerTxd( #w0," " )
Loop
and increments w0 about once per second:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
Flenser
 
Top