Integer maths problem negative numbers

Hitechbuzz

New Member
Hi Guys I was wanting to have a timer for 10 minutes. Here is part of the code.
With a loop inside another but you can't use negative numbers so how does one subtract 1 off the variable eg. b1 and replace it with the result?

timer10: '10minute timer
let b1 = 10

loop1:
b1 = b1 -1
Pause 500
if b1 >= 1 then loop1

return

Thanks for your help in advance.
Cheers hitechbuzz
 

hippy

Technical Support
Staff member
Welcome to the PICAXE forum.

Your code will work ( try it in the Programming Editor Simulator ). Because of your 'b1>=1' your condition will only 'fail' when b1 reaches zero. An Equivalent is -

b1 = 10
Do
b1 = b1-1
Pause 500
Loop While b1 <> 0

You may ( probably will ) have to alter the length of your PAUSE and/or initial value of 'b1=' to get the time delay you are wanting.
 

BCJKiwi

Senior Member
It's OK to subtract one value from another.

It's just that when the value goes below Zero, it "rolls over'' to 255 (for a byte value) instead of -1.

Simpler to count up in a loop until the result is over the one wanted rather than count down.

However if the requirement is to count down, then rather than test for <0, test for =0 or > say 250.
 
Top