2014년 12월 25일 목요일

Need help understanding why this timer example works this way

I am creating a count up timer to be added to my larger program. So I thought I'd start with this basic program first to iron it out and try to understand how it works.

The way it works is that when the START button is pressed the label appears, enables the timer and starts counting up from 0. The number in the label is black for numbers 0 to 30 and if it goes past 30 it turns red. When the STOP button is pressed it disables the timer, makes the label not visible and resets the global variable timer count value to 0.

Here's the code:


When I press the START button the label appears and the numbers start counting up in black then turn red after 30. I press the STOP button and the numbers disappear all as expected. Now is where the problem comes. I then press the START button again and the numbers appear but it is the last number where it stopped counting then it goes to 1 and counts on up. For example if the very first time I pressed START and let it count from 0 to 40 then pressed STOP and then pressed START again the label will show a red 40 then go back to a black 1 and start counting up again.

So I added some more blocks as shown in the STOP blocks method:

So my question is why doesn't it recognize the "set global timer_clk to 0" and start out counting from at least 1? I realize that where I have my label block in the timer block that it won't show 0 the second time. If I use the 2 additional sets of blocks in the STOP blocks method then it works. Seems to me I should have to use those 2 block lines I added.

Can someone explain? Thanks and Happy Holidays to everyone!



You problem must be that you make the label visible before resetting its value. It can take a while until the timer fires and then sets the value right. So, set a value in the label before you make it visible will solve your problem.



You problem must be that you make the label visible before resetting its value.

Actually if you look at this set of blocks:

You can see that when I press the STOP button I do reset the timer to 0 after making the label invisible and before making it visible again. Sorry but your comment doesn't make sense to me if you refer to the blocks.
 

On Thursday, December 25, 2014 9:19:01 AM UTC-6, Ghica wrote:
It can take a while until the timer fires and then sets the value right.

So are you saying that even though I use the "set global timer_clk to 0" that doesn't take effect until the timer fires again???


I did get it to work using this:

But it seems to me that I shouldn't have to and that is why I am questioning this. I would think that in the chain of events within a "blocks method" would be acted upon immediately such as setting a variable to a specific value. 

Please don't misunderstand my tone here in the reply as I'm trying to understand. I know it can sound rather forward but it's not intended.

Thanks for your help. 



Look at your blocks again. Of course your global gets its value at the moment you set it. I was talking about what you see. If you set the label to visible, you see what it has as value at that moment, not what you have in your global. You should set the text of the label anytime you set the global value. Actually, you do not need the global at all, you could just read the text of the label, increment it and write it back again. 


댓글 없음:

댓글 쓰기