how to programm this: on/off switch newbie needs help

Dawo

2011-11-15 23:00:32

Hello,

i`ve got a simple problem but i get stuck with this.
let`s say i have got a midicontroller with a toggle button. it sends "90 34 7f" when pressed and "80 34 7f" when released. i want MT to control the built in led of this button. the message to turn it on is 90 32 7f and 80 32 7f will turn it off. MT should remember the status off the led. by default it is on. when i press the button the first time the led should stop glowing. the next time i press the button it should glow again and so on.
here is what i tried:

Code: Select all

translator#1:
Incoming: MIDI "90 34 7f" -- port=controller 
Rules
 if g0==0 then exit rules, execute outgoing action
Outgoing: MIDI "80 32 7f" -- port=controller

translator#2: 
Incoming: MIDI "90 34 7f" -- port=controller
Rules
 if g0==1 then exit rules, execute outgoing action 
Outgoing: MIDI "90 32 7f" -- port=controller

translator#3:
Incoming: MIDI "90 34 7f" -- port=controller
Rules
 if g0==0 then assign g0=1
 if g0==1 then assign g0=0
Outgoing: none
this is not working. where am i wrong?
thanks in advance!

florian

2011-11-17 12:56:59

Hi,

you try to use g0 as the global variable to remember the toggle state. If it has value 0 it means that the LED is ON (the default state), and if it is 1 it means it is off.
The main problem is this:

Code: Select all

if g0==0 then assign g0=1
if g0==1 then assign g0=0
In the first line, if g0 is 0 you will set it to 1. In the second line, you will then immediately set it to 0, as it is set to 1 in the first line! You can follow that behavior when watching your preset in action in the Log Window. The Log Window is your best friend for "debugging" your translators and rules.

A trick to toggle a variable from 0 to 1 and from 1 to 0 is to use this code:

Code: Select all

g0 = 1 - g0
Another problem is that unless explicitly "demanded" by the rules, the outgoing action is always executed! So you should reverse the condition and do "exit rules and skip outgoing action".

Here is your "fixed" preset:

Code: Select all

translator#1: "Toggle LED off"
Incoming: MIDI "90 34 7f" -- port=controller
Rules:
 if g0!=0 then exit rules, skip outgoing action
Outgoing: MIDI "80 32 7f" -- port=controller

translator#2: "Toggle LED on"
Incoming: MIDI "90 34 7f" -- port=controller
Rules:
 if g0!=1 then exit rules, skip outgoing action
Outgoing: MIDI "90 32 7f" -- port=controller

translator#3: "Toggle g0 variable"
Incoming: MIDI "90 34 7f" -- port=controller
Rules:
 g0 = 1 - g0
Outgoing: none
Hope that helps!
Florian

PS: you can copy multiple translator entries in Midi Translator and paste them directly into this forum!

PPS: I took the liberty to format your original post a bit.

Dawo

2011-11-17 23:57:25

Hello,

thanks a lot for your help!

over the weekend i tried another solution and it worked. i discoverd the log-window and i noticed the problems you discribed.
your solution is much more elegant than mine:

Code: Select all

Translator 735: Solo/Cue 8 led an
Options: stop=false
Incoming: MIDI 97 31 7F 
Rules: 
  if gp==0 then exit rules, skip Outgoing Action
Outgoing: MIDI 97 31 7f 

Translator 736: Solo/Cue 8 led aus
Options: stop=false
Incoming: MIDI 97 31 7F 
Rules: 
  if gp==1 then exit rules, skip Outgoing Action
Outgoing: MIDI 87 31 7f 

Translator 737: Solo/Cue 8 timer 1
Options: stop=false
Incoming: MIDI 97 31 7F 
Rules: 
  if gp==0 then exit rules, skip Outgoing Action
Outgoing: One-shot timer "gp auf 0": 1 ms delay

Translator 738: Solo/Cue 8 timer 2
Options: stop=false
Incoming: MIDI 97 31 7F 
Rules: 
  if gp==1 then exit rules, skip Outgoing Action
Outgoing: One-shot timer "gp auf 1": 1 ms delay

Translator 739: Solo/Cue 8 gp 1
Options: stop=false
Incoming: On timer "gp auf 1"
Rules: 
  gp=1
Outgoing: (none)

Translator 740: Solo/cue 8 gp 0
Options: stop=false
Incoming: On timer "gp auf 0"
Rules: 
  gp=0
Outgoing: (none)
at the moment i am not sure if i change my code because there were 36 (yeah, i needed all global variables) buttons that i programmed like this. do you think that my more complicated code will cause noticable performance issues?

thanks again a lot and best wishes!

florian

2011-11-18 00:35:57

using hundreds of translators can degrade performance somewhat. But rest assured, there are presets with thousands of translators and sometimes hundreds of rule lines per translator in productive use and they work with no noticeable delay.
One performance tip is to put translators that you don't always need into a separate preset. Then when you don't need them, disable that preset from an outgoing action. Like that, the processing time for all those translators is removed.

There are actually hundreds of global variables, there is g0...g9 and ga...gz, but also h0....hz, i0....iz, and so on. Freely named variables are planned for version 2...

Florian

Dawo

2011-11-18 13:24:16

hello,

it is good to know!

i thought that only the "g" variables were global.

i had a hard time finding sleep tonight and so i changed my code to the version you suggested.

less code is better in my oppinion.

i still do not understand why it is working the right way (how can a abstract value like "ga" be substracted from a real value like 1? :? ). but i will try to get involved in this. :idea:

the sign "!=" means "ungleich" in german, doesn`t it?

thanks for your time and best wishes.

florian

2011-11-18 20:58:27

yes, "!=" is used for "ungleich" (not equal).
Hope you're getting better sleep tonight!
Florian