Beginner code question
KeithJust
Posts: 18
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.
0
Comments
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]
In my example, FOUNTAIN_RELAY is a constant assigned the relay number. I'm sure the rest is self-explainitory.
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.
DEFINE_PROGRAM
wait 100 fnFountainUpkeep(); // runs this function every 10 sec
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.