Home AMX User Forum AMX Technical Discussion

Is there something like 'SP' for Relay or IO ports?

Trying to avoid SET_PULSE_TIME all over the place.

Comments

  • Spire_JeffSpire_Jeff Posts: 1,917
    Have you considered:
    define_function pulseRelay1(){
      on[dvRelay,1]
      wait 8 off[dvRelay,1]
    }
    .
    .
    .
    pulseRelay1();
    
    //If you want to be fancier...
    define_function pulseRelay(integer myRelay){
    switch(myRelay){
      case 1:{
        on[dvRelay,1]
        wait 8 off[dvRelay,1]
      }
      case 2:{
        on[dvRelay,2]
        wait 8 off[dvRelay,2]
      }
    }
    }
    .
    .
    .
    pulseRelay(1);
    pulseRelay(2);
    

    It does require a few lines of code, but it should be easy enough to change the "pulse" time on a per device basis.

    Jeff
  • travistravis Posts: 180
    how about
    define_function pulseRelay(dev myRelay, integer myChannel, integer myWait){
     ON[myRelay,myChannel]
     wait(myWait){
        OFF[myRelay,myChannel]
     }
    }
    

    or setup a timeline and all the necessary bits inside a the function... more accurate?
  • Spire_JeffSpire_Jeff Posts: 1,917
    You need to be careful with the waits if there is any chance you will need to relays to pulse at the same time. Only the first wait is likely to execute if you are using the same wait statement for all relays.

    You could do the timeline thing and have it trigger every .1 seconds and count down to the OFF command if you think you need to change pulse times on the fly for the relays. If you need the pulse time for each relay is constant, then the timeline approach seems like too much work for no real gain (that I can think of :) ). I would opt for the KISS method :)

    Jeff
  • jjamesjjames Posts: 2,908
    How about setting the pulse time in just one place?
    define_function pulseRelay(dev myRelay, integer myChannel, integer myPulse)
    {
    	// Store the old pulse time
    	stack_var integer oldPulse;
    	oldPulse = get_pulse_time;
    	
    	// Set the new pulse time
    	set_pulse_time(myPulse)
    	
    	// Pulse your relay
    	pulse[myRelay,myChannel];
    	
    	// Restore old pulse time
    	set_pulse_time(oldPulse);
    }
    

    I was about to go the wait route - but realized, if you had 10 REL10s . . . you'd need 100 different cases for a switch I was going to propose (10 for the device, 10 for each relay.) Timelines are terribly inefficient (as far as creating them - you have to code in three different places just to create them), so I wouldn't go that route at all.
  • viningvining Posts: 4,368
    Another option could be using Do_Push_Timed. Although there are many Do_Push haters out there using Do_Push_Timed could do what you want too.
    DEFINE_VARIABLE //testing
    
    VOLATILE DEV dvMyRelays = 5001:8:0 ;
    VOLATILE LONG nPushTime = 30 ;
    VOLATILE INTEGER nPushChnl = 1 ;
    VOLATILE INTEGER nBtnTest = 0 ;
    
    
    BUTTON_EVENT[dvRelays,0]
    
         {
         PUSH:
    	  {
    	  SEND_STRING 0, "'RELAY BTN-[ ',itoa(BUTTON.INPUT.CHANNEL),' ] PUSHED'" ;
    	  ON[dvRelays,BUTTON.INPUT.CHANNEL] ;
    	  SEND_STRING 0, "'RELAY CHNL-[ ',itoa(BUTTON.INPUT.CHANNEL),' ] ON'" ;
    	  }
         RELEASE:
    	  {
    	  SEND_STRING 0, "'RELAY BTN-[ ',itoa(BUTTON.INPUT.CHANNEL),' ] RELEASED'" ;
    	  OFF[dvRelays,BUTTON.INPUT.CHANNEL] ;
    	  SEND_STRING 0, "'RELAY CHNL-[ ',itoa(BUTTON.INPUT.CHANNEL),' ] OFF'" ;
    	  }
         }
    
    DEFINE_PROGRAM  //TESTING ROUTINE
    
         {
         WAIT 10
    	  {
    	  if(nBtnTest)
    	       {
    	       DO_PUSH_TIMED(dvMyRelays,nPushChnl,nPushTime) ;
    	       nBtnTest = 0 ;
    	       }
    	  }
         }
    
    for testing I just put this in define program and toggle the nBtnTest var in debug to make it run. Just put the Do_Push_Timed that I have in DEF_PROG where you need it, adjust the vars in it as needed for your various relays and you're good to go.
  • jjamesjjames Posts: 2,908
    The problem with the above solution is it requires a constant evaluation in define_program. Take full of advantage of the event driven programming model of NetLinx, and I suggest removing define_program all together.
  • viningvining Posts: 4,368
    jjames wrote: »
    The problem with the above solution is it requires a constant evaluation in define_program. Take full of advantage of the event driven programming model of NetLinx, and I suggest removing define_program all together.
    The code in def program is just for testing and it is not intended to be used at all. As I stated in my post you need to take the do push timed out of def program and put it where you want use it. I would never suggest putting anything in def program except to run some quick tests or as aquick fix.
Sign In or Register to comment.