Does serin block the "time" variable?

Jeremy Harris

Senior Member
A quick question that I can't find an answer to.

I'm trying to measure the time between serin events. I have an RF data link that transmits a handful of characters every ~ 30 seconds or so. I have a simple bit of code that has a serin, then a simple calc that uses the time variable to measure the time since the last data burst, then resets the time variable to zero.

The problem is that the time variable doesn't seem to increment every second when the conditional serin is waiting for data, but stays at zero.

I've used the same technique in the past, but not with a blocking instruction like serin.

I've trimmed the code right down to just a conditional serin, followed by a debug, then resetting time to zero and going back to the start of the loop to try and see why time doesn't seem to work. The loop runs and debug updates around once every 30 seconds, but always returns time as zero, even though around 30 secs has elapsed since time was reset to zero.


serin c.4, T1200_4, ("POW"), high_byte, low_byte


time = 0

goto main
What I was hoping was that in the ~30 secs between receiving valid serin data, time would carry on incrementing, so that I could determine roughly how long the elapsed time is between valid data bursts. Sadly it looks as if time doesn't increment, but is blocked by serin. debug shows the received data updating OK, but time stays stuck at zero.

This is running on a 14M2, but I'm not sure if that has any impact on the behaviour.

Can anyone confirm that serin does really block time from updating, please, or is it my finger trouble?

If serin does block time, then I'll just have to find a workaround.


Senior Member
In my experience with Picaxe, serin has blocked everything until it's properly completed.

Could you add in a do loop until time= whatever?

I did something similar where I used a timeout on my serin, so after 1 second timeout I increment a variable and flash an LED,and then when the variable reached 60 (1 minute) it exits that loop and continues with my program.

just an idea.


serin [1000],B.3,N4800,w1
inc b0

loop until b0 > 30


Senior Member

Yes, AFAIK serin (and other "timing" commands such as pulsin/out, count,etc) "stop" the time variable incrementing (or more precisely disable the 20 ms interrupt "ticks" of its prescaler). Note also that setting time to zero does not reset the prescaler (so it will next increment after between zero and one second later).

ravis may have pointed to a solution, but you'd need to specify (say) a one-second timeout and a timeout address. At that address you'd increment a (or the) "time" variable and then jump back to the serin again.

Alternatively, you could try to use HSERIN, but this raises many issues such as pin availability and the much more limited functionality of the command, particularly on M2s.

Cheers, Alan.

Jeremy Harris

Senior Member
Thanks folks, looks like I'll have to go to plan 2 and work around it. Glad to know it wasn't just finger trouble my end, as it had me scratching my head a bit yesterday.

I can do this using the RTC for this project; it just means a bit more code to read the time just before the serin instruction and again just after, plus some arithmetic to work out the elapsed time in seconds. With luck I should still have enough spare program memory, I just get a bit obsessed with using the least amount of code to do a specific task sometimes.


Senior Member
I have found the time variable to be highly inaccurate in most programs, as too many commands block it. It would be handy if it worked better, but an external rtc sounds like your best solution.

Jeremy Harris

Senior Member
I have found the time variable to be highly inaccurate in most programs, as too many commands block it. It would be handy if it worked better, but an external rtc sounds like your best solution.
Yes, it can be, but I've found it worked very well for calculating battery capacity used on my electric bike "fuel gauge", mainly because I didn't use any commands that needed the picaxe timers, I suspect.

The RTC solution works OK, but does add a few more lines of code to calculate elapsed time since the last received reading. The application is an energy meter, which receives power data (both import and export) from a 433 MHz transmitter hooked up to another Picaxe and an electricity meter chip, that's remotely mounted next to my external electricity meter cabinet. The idea is to have a remote display inside the house showing whether the house is exporting or importing electricity and keep tally of the approximate daily energy use/generation (most of the time the house generates a lot more power than it uses).