Home AMX User Forum NetLinx Studio

Problem whit for loop

I'm trying to atualize every second on my TP screen a timer ... and I'm trying this:
DEFINE_FUNCTION fnTIMER()
{
    FOR(nATUALIZE = 1; nATUALIZE <= 60; nATUALIZE++)
    {
	IF(!bRUNNING)
	{
	   ON[bRUNNING]
	   nTIME = nTIME-1
	   SEND_COMMAND dvTP,"'^TXT-33,0,',ITOA(nTIME)"
	   WAIT 10
	   {
		OFF[bRUNNING]
	   }
	}
    }
}

The variable nTIME starts with number 60


But nothing happens ... so I checked on debugging and the nTIME just decrease once and the variable nATUALIZE didn't increase ... Please, where is dumb mistake?

Thank you!

Comments

  • DHawthorneDHawthorne Posts: 4,584
    Your FOR loop isn't suspended when bRunning == TRUE. It goes right through the entire sequence and just skips the part inside your IF statement. You can't pause a FOR loop with a WAIT, the WAIT will just get thrown on the stack and the loop continues. That's why nTime isn't changing more than once. nATUALIZE should be incrementing though, based on that code snippet; anything else have access to it? It's possible, however, that it is just changing too fast to see in debug; though I would expect it to be at 61 after exiting the loop.

    You would be better served by doing the whole thing in a timeline. Let fnTimer start up a repeating timeline that counts down from 60 to 0 once a second. Put your send_command to the panel in the timeline event, and have the timeline kill itself when it hits zero.
  • 2Fast2Fast Posts: 90
    All right! Thank you DHawthorne!
  • 2Fast2Fast Posts: 90
    Exists one way to create a timeline with one event for each second .... but this same timeline can be used to count seconds, minutes or hours? I don't know if I explained correctly ... to use a timeline event I need to create a variable like: LONG lTIMES1[]={625,750,1000,1500,1625,1750,1850,2000}, but with this variable I have 8 positions with different values. The question is if I want to count 90 seconds and send a string to panel each second I need to create a variable with 90 positions? And if I want to count 3000 seconds?

    Thanks!
  • I* would just do a repeating timeline with 1 value for time 1000. This represents 1 second. Use a variable for the countdown. This way the count down can work for any amount of time.

    timeline_event[onesecondTimeline]
    {
    if (countdown>0)
    {
    countdown--
    if (countdown) send_command TP,"'TXT1-',itoa(countdown)"
    else send_command TP,'TXT1- ' //to clear the countdown
    }
    }

    Set Countdown to what ever you want.
    You could even pause and resume the timeline with the value of countdown. If CountDown = 0 pause the timeline.
  • JeffJeff Posts: 374
    what kbeattyAMX suggested would work fine, I just wanted to show another example of how to do the same thing.
    timeline_event[OneSecondTL]
    {
    	send_command dvTP,"'^TXT-1,0,',itoa(nCountdown-timeline.repetition)
    	if (nCountdown=timeline.repetition) timeline_kill(OneSecondTL)
    }
    
    button_event[dvTP,1]
    {
    	push:
    	{
    		nCountdown=60
    		timeline_create(OneSecondTL,lOneSecond,1,timeline_relative,timeline_repeat)
    	}
    }
    

    This code has the advantage of ending the timeline when your countdown is over. You could then use the timeline again for 90 seconds, or 400 seconds, or whatever, just set nCountdown to whatevery ou want, and create the timeline.

    And if you're going to use it a lot, just create this function
    define_function start_countdown(integer i)
    {
    	nCountdown=i
    	timeline_create(OneSecondTL,lOneSecond,1,timeline_relative,timeline_repeat)
    }
    

    and then you just call start_countdown(60) or whatever.

    I prefer killing the timeline when you're not using it to letting it run all the time, but there are many ways to do it and I just wanted to show another example.

    J
  • timeline.repetition

    Good to know! I didn't think to look for this.
  • 2Fast2Fast Posts: 90
    Thank you all!

    I'll do this right now thanks again!
Sign In or Register to comment.