Home AMX User Forum AMX General Discussion

Beginner code question

Would on of you guys mind giving me a code example to control a relay with a single button? My device is defined as Fountain = 5001:4:0 and I would like to use a single button one push for on and the next push for off. I would also like to have on off status for the button. Any help would be appreciated! Thanks.

Comments

  • jazzwyldjazzwyld Posts: 199
    Try this

    I believe one of these will work for ya.


    DEFINE_DEVICE


    dvFountain = 5001:4:0 // Water Thingy
    dvTP = 10001:1:0 // Touch Panel Thingy

    DEFINE_VARIABLE

    WATER


    DEFINE_EVENT

    BUTTON_EVENT[dvTP,101] // Single button event for water fountain
    {
    PUSH:
    {
    SELECT
    {
    ACTIVE (WATER=0): // if water fountain is off turn it on
    {
    ON[dvFountain,1]
    WATER =1 //setting variable for the toggle
    }
    ACTIVE(WATER=1): //if water fountain is on turn it off
    {
    OFF[dvFountain,1]
    WATER =0 // setting variable for the toggle
    }
    }
    }
    }
    BUTTON_EVENT[dvTP,101] // here's another way...just depends on what you're going for.
    {
    PUSH:
    {
    [dvFountain,1]=![dvFountain,1]
    }
    }


    DEFINE_PROGRAM

    [DVTP,101]=[dvFountain,1]
  • Thanks Jeff for the information it really helps! Question though. In the first example what part of the code controls button feedback?
  • jazzwyldjazzwyld Posts: 199
    The relay is controlling the feedback. You could make the variable WATER also, but that's not real feedback. At the DEFINE_PROGRAM. You see that the touchpanel will be on/off when the relay is on/off.
  • DHawthorneDHawthorne Posts: 4,584
    Keep it simple.
    BUTTON_EVENT[dvTP, nFoutainButton]
    {
        PUSH : [dvRelays, FOUNTAIN_RELAY] = ![dvRelays, FOUNTAIN_RELAY] // toggles relay state
    }
    
    // In your feedback section, timeline or DEFINE_PROGRAM
    
    [dvTP, nFountainButton] = [dvRelays, FOUNTAIN_RELAY]
    

    In my example, FOUNTAIN_RELAY is a constant assigned the relay number. I'm sure the rest is self-explainitory.
  • DHawthorne wrote:

    // In your feedback section, timeline or DEFINE_PROGRAM

    [dvTP, nFountainButton] = [dvRelays, FOUNTAIN_RELAY]

    Do you use a timeline for feedback as opposed to define program?
  • DHawthorneDHawthorne Posts: 4,584
    TonyAngelo wrote:
    Do you use a timeline for feedback as opposed to define program?

    Usually, but if it's really basic (no send_commands, just channels and levels), I'll put in in DEFINE_PROGRAM. You absolutely do not want it in DEFINE_PROGRAM if there are any send_commands, or send_strings ... you'll bog the system down with all the spam. My normal procedure is to create a function call for feedback. I'll call it in DEFINE_PROGAM for the simple systems, but once they reach a level where that isn't feasible (my jobs all have a tendency to evolve - the company just loves selling upgrades), I'll move it to a timeline.

    I really hate even mentioning puting feedback in DEFINE_PROGRAM, because if you aren't extremely careful with it, it can and will blow up your system. It is probably a good general rule not to play fast and loose with it like I do until you are very familiar with what is happening in your system and you are absolutely positive of what you can get away with.
  • So when you're using a timeline do you just set it up repeat every 10ms or something like that? Do you create it in DEFINE_START? I've been trying to migrate more and more stuff out of define program and this is an interesting option, I'm just curious about it's implementation.
  • mpullinmpullin Posts: 949
    If you just want a function to run at regular intervals, another option is just to put it in DEFINE_PROGRAM with a wait. e.g.

    DEFINE_PROGRAM
    wait 100 fnFountainUpkeep(); // runs this function every 10 sec
  • DHawthorneDHawthorne Posts: 4,584
    TonyAngelo wrote:
    So when you're using a timeline do you just set it up repeat every 10ms or something like that? Do you create it in DEFINE_START? I've been trying to migrate more and more stuff out of define program and this is an interesting option, I'm just curious about it's implementation.

    Yes, create it in DEFINE_START. Depending on the project, I set it to repeat every 500ms. 10 is kinda fast if there is a lot going on. But I also try write my UI's so that button presses and reactions update immediately, and the feedback routine is strictly for non-interactive updates. If you are pressing a button, even a half second is noticeable (though, it can be argued, still not a huge delay). But for states updated by the device, it's more than acceptable.
Sign In or Register to comment.