PDA

View Full Version : IF...THEN won't let numbers to be summed



flotulopex
- 12th April 2007, 20:34
Hello,

I can't find out why this doesn't work:
if mem_l = 2 then speed = speed + 1 : gosub game_speed
while this does:
if mem_l = 2 then speed = 1 : gosub game_speed
According to the PBP Compiler's manual, both syntax look okay.

When I run my prog with the second example, my Speed VAR will go crazy and show up numbers between 50 and 240(?). Speed is declared as VAR Byte and set to "0" at beginning of prog.

I've attached my code (the concerned lines are labeled 'Set game speed according to level).

Any help is appreciated.

skimask
- 12th April 2007, 22:05
Hello,
I can't find out why this doesn't work:
if mem_l = 2 then speed = speed + 1 : gosub game_speed
while this does:
if mem_l = 2 then speed = 1 : gosub game_speed
According to the PBP Compiler's manual, both syntax look okay.
When I run my prog with the second example, my Speed VAR will go crazy and show up numbers between 50 and 240(?). Speed is declared as VAR Byte and set to "0" at beginning of prog.
I've attached my code (the concerned lines are labeled 'Set game speed according to level).
Any help is appreciated.

You can't have a 'colon' in a single line if/then statement. You have to split it up, like this:

If mem_1 = 2 then
speed = speed + 1
gosub game_speed
EndIf

or

If mem_1 = 2 then
speed = speed + 1 : gosub game_speed
EndIf

I don't think the manual says it specifically, but that's the way it is.

flotulopex
- 13th April 2007, 06:44
Hi skimask,

Thanks for the help. I'm in the office now and I'm not sure about this anymore but I think I have tried what you suggest already and didn't work too.

Nevertheless, I'll try again tonight (I'm in the office now).

BTW, this is an example one can find in the COMPILER's manual (confusing):

<img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=1533&stc=1&d=1176442788">
I've used this syntax many times before and never had trouble.

mackrackit
- 13th April 2007, 07:05
That non-optional ENDIF to complete the structure might have something to do with it?

I do not see it in your code.

skimask
- 13th April 2007, 13:30
Don't know why one works and the other doesn't.
I just stick with what does work... :)
I know, no help, but it's the facts...


Also, I just looked thru your code.
There's an END statement in the middle before GAME_SPEED.
You probably don't want that in there.

And a couple of your for/next statements, you don't specifically tell PBP which variable you are 'next'ing with your 'for'.
Might help, might not...

mackrackit
- 13th April 2007, 13:54
The second example in the first post does not work correctly.

When I run my prog with the second example, my Speed VAR will go crazy and show up numbers between 50 and 240(?). Speed is declared as VAR Byte and set to "0" at beginning of prog.

Look at this from the posted code.


'Set game speed according to level
if mem_l = 2 then speed = 1 : gosub game_speed
if mem_l = 3 then speed = 2 : gosub game_speed
if mem_l = 4 then speed = 3 : gosub game_speed
if mem_l = 5 then speed = 4 : gosub game_speed
'' if mem_l = 2 then speed = speed + 1 : gosub game_speed
'' if mem_l = 3 then speed = speed + 1 : gosub game_speed
'' if mem_l = 4 then speed = speed + 1 : gosub game_speed
'' if mem_l = 5 then speed = speed + 1 : gosub game_speed

value = value + 1 'To change the "seed" for random #
Goto MAIN


Do you see a problem?

sougata
- 13th April 2007, 14:34
Hi,





IF MEM_1 = 2 THEN SPEED = 1 : GOSUB SOMETHING

is equivalent to


IF MEM_1 = 2 THEN SPEED = 1
GOSUB SOMETHING

or


IF MEM_1 = 2 THEN
SPEED = 1
ENDIF
GOSUB SOMETHING

mackrackit
- 13th April 2007, 14:43
This is weird. The email message when sougata posted said
################################
Hi,

A colon separated mulitple statement will always compile so


Code:
---------
IF MEM_1 = 2 THEN SPEED = 1 : GOSUB SOMETHING
---------
is equivalent to

Code:
---------
IF MEM_1 = 2 THEN SPEED = 1
GOSUB SOMETHING
---------
or

Code:
---------
IF MEM_1 = 2 THEN
SPEED = 1
ENDIF
GOSUB SOMETHING
---------
So if you really want to execute multiple command based on a conditions then *ENDIF*
is the way.
##########################
But the forum does not have the whole message. I am on a windows machine today so maybe...

sougata
- 13th April 2007, 15:03
Hi,

I wrote the original thread counting on my past experience but then again I didn't try out flotul's structure. May be I will trying peeping the compiled hex and find out if it does carry out a conditional check on ":"So did not want to confuse readers and edited. Your windows is working fine as the doors :D

sayzer
- 13th April 2007, 18:52
A little suggestion:

What about a Pause or a kind of bounce killing?
Something like,


IF MEM_l = 2 THEN
WHILE MEM_l = 2 : WEND
speed = speed + 1
ENDIF

OR
IF MEM_l = 2 THEN
PAUSE 100
speed = speed + 1
ENDIF




Speed increases so fast that it goes crazy!
---------------

paul borgmeier
- 13th April 2007, 20:47
The manual and Flotu imply that:


if mem_l = 2 then speed = speed + 1 : gosub game_speed

is equivalent to


if mem_l = 2 then
speed = speed + 1
gosub game_speed
endif


Flotu,

does pbtn = 9 if no button is pushed? If it does then your code is doing what you told it to do - update speed each time through the loop while waiting for the button to be pushed whenever mem_1 is 2,3,4, or 5. See below



MAIN:
gosub READ_BTNS
if pbtn < 4 and Ctr_L <= mem_l then
'lots of stuff here removed for readability (see his post #1)
endif

if mem_l = 2 then speed = speed + 1 : gosub game_speed
if mem_l = 3 then speed = speed + 1 : gosub game_speed
if mem_l = 4 then speed = speed + 1 : gosub game_speed
if mem_l = 5 then speed = speed + 1 : gosub game_speed

value = value + 1 'To change the "seed" for random #
Goto MAIN
end

flotulopex
- 13th April 2007, 22:19
Thank you for all your remarks.

The END statement was there only for testing purpose; I'm going to remove it.

Yes, Pbtn=9 if no button is pressed.

Well Paul, I think you have found the problem.

Regarding the "IF xxxx THEN yyyy : zzzz", I thought that the "zzzz" would be executed ONLY when the IF statement is true. This is why I wrote it after each IF statement (didn't check this out now - will do this in a few minutes).

I'll come back asap.

flotulopex
- 13th April 2007, 22:49
I moved my four IF statements to the "correct place" in my loop and everything goes fine now.

I admit, while searching for hours in the wrong direction, I've lost the overview of the program's cycle.

So let me thank all of you one more time.

I'm finishing some cleanups and port it to a PIC12F683 (it's my first SMD project...).

Finally, I'll end this thread by attaching the ultimate code.

flotulopex
- 14th April 2007, 17:45
Hello,

I've had lots of help from many of you so I share my code for this SIMON game.

I wanted to make a hand-held SMD version of this game with 3 lifes and a level counter.

Start the game on speed-level 1 by pressing button n°1 (most left) and, respectively, press button n°4 to start on speed-level 4.

When you miss, the leds flash for the numbers of remaining lifes.

After you have used your 3 lifes, all leds flash 3 times and then you can read your score: most-right led counts the Units, second led starting from right counts Tens.

<img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=1534&stc=1&d=1176568745">

mister_e
- 14th April 2007, 17:49
Neat PCB job! well done.