Home AMX User Forum NetLinx Studio

Bigginer's Question .. Will this work?

Hi,

I'm trying to program for single button to toggle for discrete power function. I would appreciate if you take a look at the code and see if this works.

DEFINE_CONSTANT

INTEGER Proj_Off = 0
INTEGER Proj_On = 1


DEFINE_VARIABLE

INTEGER nProjPwr // Track Projector Status


DEFINE_FUNCTION ProjPwrOn()
{
PULSE[dvProj,27]
WAIT 50
{
nProjPwr = Proj_On
}
}
DEFINE_FUNCTION ProjPwrOff()
{
PULSE[dvProj,28]
WAIT 50
{
nProjPwr = Proj_Off
}
}


DEFINE_EVENT

BUTTON_EVENT[dvTP_Proj,9] // The projector power toggle button
{
PUSH:
{
IF(![dvTP_Proj,9])
{
ProjPwrOn()
}
ELSE
{
ProjPwrOff()
}
}
}

DEFINE_PROGRAM


[dvTP_Proj,9] = ![dvTP_Proj,9] // Toggle Projector Power button

Comments

  • In general the code would work. Personally I would write this different, but I don't want to start a discussion about "What's the best/shortest/easiet/most complicate way to do".

    The only thing I really would change is the way to use the projector's state
    BUTTON_EVENT[dvTP_Proj,9] // The projector power toggle button
    {
    PUSH:
    {
    IF(nProjPwr = Proj_Off) // <--
    {
    ProjPwrOn()
    }
    ELSE
    {
    ProjPwrOff()
    }
    }
    }
    
    DEFINE_PROGRAM
    [dvTP_Proj,9] = (nProjPwr = Proj_Off) // <---
    
    It is the better way to use the projector state variable instead of the channel of the Touchpanel button, because if the projector is tracked as ON, and the panel goes offline and online again later, the channel is off, and so the toggle will fail.
  • Without rewriting your code, how about this instead:
    DEFINE_CONSTANT
    
    INTEGER Proj_Off   =  0
    INTEGER Proj_On    =  1
    
    
    DEFINE_VARIABLE
    
    INTEGER nProjPwr				//	Track Projector Status
    
    
    DEFINE_FUNCTION ProjPwrOn()
    {
    	PULSE[dvProj,27]
    	WAIT 50
    	{
    		nProjPwr = Proj_On
    	}
    }
    DEFINE_FUNCTION ProjPwrOff()
    {
    	PULSE[dvProj,28]
    	WAIT 50
    	{
    		nProjPwr	=	Proj_Off
    	}
    }
    
    
    DEFINE_EVENT
    
    BUTTON_EVENT[dvTP_Proj,9]			//	The projector power toggle button
    {
      PUSH:
      {
    [b]    IF(nProjPwr == Proj_Off)[/b]
        {
          ProjPwrOn()
        }
        ELSE
        {
          ProjPwrOff()
        }
      }
    }
    
    DEFINE_PROGRAM
    
    
    [b][dvTP_Proj,9] = nProjPwr == Proj_On	//	Toggle Projector Power button[/b]
    

    In your code, nProjPwr didn't get used, and the last line would have caused the power button to change states every run through mainline.

    There are lots of reasons not to code it this way too by the way. Your status tracking can get screwed up for any number of reasons, then you'll be out of sync and your control system won't look very intelligent anymore. Also, most projectors have warmup/cooldown periods where it doesn't accept commands. You'll need to try to account for those too.

    --John
  • Without rewriting your code, how about this instead:

    In your code, nProjPwr didn't get used, and the last line would have caused the power button to change states every run through mainline.

    --John

    Ups..... you are right, not seen (1 big cup of coffee still did not bring me fully online this morning :))
  • viningvining Posts: 4,368
    As long as nProjPwr is either 1 or 0 for on and off which is the way this code is written you can drop the comparison in the feedback line and just write.
    DEFINE_PROGRAM
    
    [dvTP_Proj,9] = nProjPwr ;
    
  • Thanks,

    Thnaks all for your help.
    I've attended the prog.I class and this was part of the final project assignment for us to try out.
    I don't agree with this way of programming either, but it just serves the practice purpose, I guess.

    John
  • DHawthorneDHawthorne Posts: 4,584
    I always set my tracking variable to show warming or cooling states as well., even if it's a TV and not a projector. Nine of ten won't accept a source change command immediately after powering on, and you have to be able to watch for that. You could even take it a step further and make your power button a multi-state bargraph that reflects those conditions as well (though I prefer a modal popup myself, to remind them to cool their jets while it's powering up).
Sign In or Register to comment.