One Count Short

JPB33

Senior Member
This looped timer is part of a conroller I am constructing. Eventually it will be working in minutes. I want to read the variable T into the display as it loops, however it reads 1 count short and I'm going round in circles trying to make progress!

Also are there any shortcuts I could use to "streamline" the code and reduce the byte count?

Many Thanks. Peter



View attachment Reads 1 low.bas
 

techElder

Well-known member
JPB33, I sure wish you had put your program within code blocks here rather than provided the download.
 

JPB33

Senior Member
Has that got it!



Code:
                  'Part of the SBR 

symbol varT = b19
symbol varTEMPBYTE1 = b22
symbol varTEMPBYTE2 = b23
symbol varTEMPBYTE3 = b24


let b19 = 0		'reset variable B2 to 0
do
if pinC.2 = 1 then exit	'exit the loop early if C.1 is high
if pinB.1 = 1 then exit	'exit the loop early if B.1 is high
if pinB.2 = 1 then exit	'exit the loop early if B.2 is high
high C.1,B.0       ' C.1 high B.1 high
pause 1000		 'wait 1 second
serout C.3, N2400, (254, 128, "Aerate pre pump  ")
bintoascii varT, varTEMPBYTE1, varTEMPBYTE2, varTEMPBYTE3
serout C.3, N2400, (254, 192, "After ",varTEMPBYTE2,varTEMPBYTE3," x mins")
inc b19		   'increment the loop counter B2
loop while b19 < 7   'otherwise loop
low b.1,c.1          ' C.1 B.2 low 
stop
 
Last edited by a moderator:

hippy

Technical Support
Staff member
When you say the count is one short, what value do you get and what value were you expecting ?

For the code given the last value shown should be 6. Your b19 is then incremented, that becomes 7, is no longer less than 7 so the loop terminates, the program ends.

You possibly need to do the increment before putting the value to the display.
 

JPB33

Senior Member
Thanks everyone, thats it! I was getting 6 when set to 7 but I was getting in a tangle with it, spent ages with it.

Is there any way to reduce/combine anything else? ie on the exit part or reading the variable T? Dont understand (but looking it up) what the DO by itself is for?

Code:
                   'Part of the SBR 

symbol varT = b19
symbol varTEMPBYTE1 = b22
symbol varTEMPBYTE2 = b23
symbol varTEMPBYTE3 = b24


let b19 = 0		'reset variable B2 to 0
do
if pinC.2 = 1 then exit	'exit the loop early if C.1 is high
if pinB.1 = 1 then exit	'exit the loop early if B.1 is high
if pinB.2 = 1 then exit	'exit the loop early if B.2 is high
high C.1,B.0       ' C.1 high B.1 high
pause 1000		 'wait 1 second
inc b19		   'increment the loop counter B2
serout C.3, N2400, (254, 128, "Aerate pre pump  ")
bintoascii varT, varTEMPBYTE1, varTEMPBYTE2, varTEMPBYTE3
serout C.3, N2400, (254, 192, "After ",varTEMPBYTE2,varTEMPBYTE3," x mins")
loop while b19 < 5   'otherwise loop
low b.1,c.1          ' C.1 B.2 low   
stop
 

srnet

Senior Member
what the DO by itself is for?
Its not by itself, as you will see when you look it up, there is a matching 'loop' somewhere below.

If the code is indented, its much clearer whats going on;

Code:
do
             if pinC.2 = 1 then exit	'exit the loop early if C.1 is high
             if pinB.1 = 1 then exit	'exit the loop early if B.1 is high
             if pinB.2 = 1 then exit	'exit the loop early if B.2 is high
             high C.1,B.0       ' C.1 high B.1 high
             pause 1000		 'wait 1 second
             inc b19		   'increment the loop counter B2
             serout C.3, N2400, (254, 128, "Aerate pre pump  ")
             bintoascii varT, varTEMPBYTE1, varTEMPBYTE2, varTEMPBYTE3
             serout C.3, N2400, (254, 192, "After ",varTEMPBYTE2,varTEMPBYTE3," x mins")

loop while b19 < 5   'otherwise loop
 

hippy

Technical Support
Staff member
Some people may find it easier to understand what value a DO-LOOP UNTIL ends with better than a DO-LOOP WHILE ...

Do
b19 = b19 + 1
:
Loop While b19 < 5

Do
b19 = b19 + 1
:
Loop Until b19 = 5

And it could be even clearer with a FOR-NEXT -

For b19 = 1 To 5
:
Next
 
Top