PDA

View Full Version : a simple boxe timer (code included)



hozone
- 19th June 2008, 15:58
=========
boxetimer
=========

Boxe timer is a countdown timer based on PIC 16F84A.
Through its 7-segments led display it alternates 2 different countdowns.
In the first mode, called "pause countdown", the display blinks while numbers are shown.
The second mode is named "working countdown" and shows numbers in a traditional way.
When the count reach 0, the devices emits a sound using the internal piezo.
The time is selected using an external switch.

Code is attached.
Code is released under the GNU Lesser General Public License.

mackrackit
- 19th June 2008, 16:12
COOL.
Do you have the Pic Basic code so folks around here can work with it?

skimask
- 19th June 2008, 16:20
COOL.
Do you have the Pic Basic code so folks around here can work with it?

It's in the zip file...and it looks a bit goofy to me (But that's just me. It doesn't have near enough colons in it :D )

mackrackit
- 19th June 2008, 16:47
It's in the zip file...and it looks a bit goofy to me (But that's just me. It doesn't have near enough colons in it :D )

Gotta get new glasses. Did not see it.

Colons-- Definitely needs a re-write :)

hozone, thanks for sharing.

T.Jackson
- 19th June 2008, 17:26
The project looks good :)

Avoid this coding style though ...


If (b1 <= 255 And b1 >= 243) Then
timerN = 1
timerAc = 600
timerBc = 180
End If
If (b1 <= 242 And b1 >= 219) Then
timerN = 2
timerAc = 180
timerBc = 60
End If
If (b1 <= 218 And b1 >= 193) Then
timerN = 3
timerAc = 120
timerBc = 60
End If


Very inefficient. Suggest rethinking your schema using a "Select Case" structure. No need for the logical AND operator at all. Put your thinking cap on and you should be able to reduce the size of the code by half!

Regards,

Trent Jackson

T.Jackson
- 19th June 2008, 18:03
Jeremy will correct it for you. If he does his IQ is 120+

skimask
- 19th June 2008, 18:05
Jeremy will correct it for you. If he does his IQ is 120+
Already did...down to 38 lines. But since nobody else can read it, decided not to post it...and didn't use select case. Those numbers aren't linear, but they're close enough to do some math on to get the desired results...thereby resulting in a simple single if/then/endif 3-line statement to handle all those if/then's.

T.Jackson
- 19th June 2008, 18:18
Can you post a readable example using either IF's or Select Case?

skimask
- 19th June 2008, 18:25
Can you post a readable example using either IF's or Select Case?

Nope... :D
It's a state secret now, top notch double-A rating, return postage included...
And it's doesn't have IF's, it has a single IF statement :D

mackrackit
- 19th June 2008, 18:31
What
is
so
hard
to
read
with
colons
?

I
think
they
help
make
the
code
MORE
readable
if
used
in
the
right
place.

It
makes
it
more
like
reading
a
book.
:D

T.Jackson
- 19th June 2008, 18:31
That's a shame Jeremy, I was going to expose you to the world as border line genius.

T.Jackson
- 19th June 2008, 18:44
I realize that I'm a bit drunk at the moment, now I'm saying 105+

T.Jackson
- 19th June 2008, 19:06
The usual suspects.

skimask
- 19th June 2008, 19:23
I realize that I'm a bit drunk at the moment, now I'm saying 105+
Then step away from the keyboard and go chase parked cars or something...

skimask
- 19th June 2008, 19:23
That's a shame Jeremy, I was going to expose you to the world as border line genius.

I've already exposed myself. I don't need any help.

T.Jackson
- 20th June 2008, 18:41
One possible "improved" solution is as follows ...



Select Case b1
Case Is < 219
timerN = 3
timerAc = 120
timerBc = 60

Case Is < 243
timerN = 2
timerAc = 180
timerBc = 60

Case Is <= 255
timerN = 1
timerAc = 600
timerBc = 180
End Select


That code will do exactly the same job as ...



If (b1 <= 255 And b1 >= 243) Then
timerN = 1
timerAc = 600
timerBc = 180
End If
If (b1 <= 242 And b1 >= 219) Then
timerN = 2
timerAc = 180
timerBc = 60
End If
If (b1 <= 218 And b1 >= 193) Then
timerN = 3
timerAc = 120
timerBc = 60
End If


BUT, it'll probably compile to half the size, run twice as fast, be more readable, easier to comprehend and definitely far easier to maintain.

Regards,

Trent Jackson

T.Jackson
- 20th June 2008, 18:53
I might add also that the same thing is possible using IF's, select case is always more effcient though however.



for (int i = 0; i < 12; i++)
{
sample = tempArray[i];

if (sample < 0)
rangeA++;
else if (sample < 10)
rangeB++;
else if (sample < 20)
rangeC++;
else if (sample < 30)
rangeD++;
else
rangeE++;
}


Notice how we start with the smallest number first? Always start with the smallest value first and work downwards with the next biggest value.

Hope it helps

Trent Jackson

hozone
- 20th June 2008, 20:51
i'm new to picbasic.

i didn't know that the "select" statment is so faster and smaller than many "if then" compiled in picbasic [realy i did not look at the select statment :)... i've just used the first think i know in picbasic]. it was my mistake that i does't look at the beutiness of my code... even in functions where performance are not needed (like the selection of the time).
i've just share my code.
anyway, thank for suggestions.
any other suggestion will be apriciated.

p.s. is this code more faster? (no IF needed)

LOOKDOWN2 W0,>[255,242,218,192,166,144,124,105,80,59,49,40,0],B0
LOOKUP2 B0,[60,600,180,120,60,60,40,30,20,15,10,45,30],timerAc
LOOKUP2 B0,[60,180,60,60,60,30,40,30,20,15,10,15,15],timerBc
LOOKUP2 B0,[6,1,2,3,4,5,6,7,8,9,10,11,12],timerN

T.Jackson
- 21st June 2008, 05:45
p.s. is this code more faster? (no IF needed)

LOOKDOWN2 W0,>[255,242,218,192,166,144,124,105,80,59,49,40,0],B0
LOOKUP2 B0,[60,600,180,120,60,60,40,30,20,15,10,45,30],timerAc
LOOKUP2 B0,[60,180,60,60,60,30,40,30,20,15,10,15,15],timerBc
LOOKUP2 B0,[6,1,2,3,4,5,6,7,8,9,10,11,12],timerN

At a guess I'm going to say much slower. LOOKUP / DOWN is a function, a group of operations, probably containing many IFs inside it. I think the protocol behind that function would be lengthy, well protected and complex.

I place no bets though, never implemented it.

Trent Jackson

T.Jackson
- 21st June 2008, 06:28
The main reason "Select Case" is faster is because it allows you to structure your code. As with an IF statement there's still a "compare" operation being preformed, but it's usually far better optimized.

The biggest problem with your original code is that every IF will be executed, which consequently wastes time.

Say for example that b1 = 245, the first IF is tested, the result returns true because b1 is within range of (255 - 243), so the block of code associated with that IF is executed, timerN is set to 1 etc ...

Now here's the problem -- all subsequent IFs are executed even though b1 is clearly out of range and the result will always return false while ever b = 245.



If (b1 <= 255 And b1 >= 243) Then ' Comparison always performed
timerN = 1
timerAc = 600
timerBc = 180
End If
If (b1 <= 242 And b1 >= 219) Then ' Comparison always performed
timerN = 2
timerAc = 180
timerBc = 60
End If


Select Case can be configured in a way so that only one comparison is ever performed, only one block of code is ever executed.

Trent Jackson

precision
- 21st June 2008, 08:59
Great,... Yes.

IF AND THEN --- compile to 159 words.
Select case --- compile to 58 words.

I will have to compere all statements. eg. FOR/NEXT loops. while/Wend, GOTO, GOSUB/RETURN, HI/LOW, TOGGLE.
For time of Execution speed and code space.


.

T.Jackson
- 21st June 2008, 11:36
I will have to compere all statements. eg. FOR/NEXT loops. while/Wend, GOTO, GOSUB/RETURN, HI/LOW, TOGGLE.
For time of Execution speed and code space.


while / wend (same as "do / loop" but without a condition) -- will always be faster than "for next loops" Main reason is because there is no counter, there's no constantly incrementing or decrementing after each iteration.

But I encourage you to conduct your tests and post the results here.

Trent Jackson

T.Jackson
- 21st June 2008, 15:20
Another thing also, the "AND" operator -- try to avoid it at all costs.



if b1 = 100 then
if b2 = 200 then
Thank Mr Jackson for these invaluable tips
end if
end if


is much better than ...



if b1 = 100 and b2 = 200 then
do something ...
end if


You might save a few bytes of code space, it'll run faster too.

Can anyone tell me why?

Trent Jackson

skimask
- 21st June 2008, 22:14
IF you're actually asking a question vs. just talking out your.............................
then...


if b1 = 100 then
if b2 = 200 then
Thank Mr Jackson for these invaluable tips
end if
end if

2 comparisons, 3 jumps



if b1 = 100 and b2 = 200 then
do something ...
end if

3 'nested' comparisons (2 + 1), couple of extra 'state' bits to save in the middle of it all, plus 4 jumps

IF you're posing a question for the class from the podium, then nevermind...

hozone
- 13th October 2008, 13:57
here the 0.3 version.

contains some bug fixes, the most important:
in 0.2 timer values are contained in BYTE VAR, big values could not be showed (in 0.3 WORD type is used).

regards,
hOZONE