Date
1 - 9 of 9
Conditionals challenge
Thanks for the all the replies everyone - it is much appreciated
If anyone has a brainwave on how to make two 1010 switches function like the Record and Play/Stop buttons of a cassette deck transport that's where I hoped to go with this... |
|
ossandust
I'll take it into consideration
|
|
chrisw_63
On Thu, May 5, 2022 at 08:36 PM, ossandust wrote:
Unfortunately the loop detection algorithm of the UnO2 compiler is not smart enough...Would it be possible to add a switch that turns loop detection off? |
|
ossandust
If you have the latest UnO2 version (v.1.3) the code above can be simplified, because with that version you can check on effect states, so you no longer need to store these in variables :
EFFECT_ON Record = SendMidi ...
EFFECT_OFF Record = SendMidi ...
EFFECT_ON Play = SendMidi ...
EFFECT_OFF Play = SendMidi ...
TRIGGER_CLICK togglePlay =
{
if(EFFECT_OFF Play) {
SwitchOn Play
}
else {
SwitchOff Play
if(EFFECT_OFF Record)
SwitchOn Record
}
}
TRIGGER_CLICK toggleRecord =
{
if(EFFECT_OFF Record) {
SwitchOn Record
}
else {
SwitchOff Record
if(EFFECT_OFF Play)
SwitchOn Play
}
}
|
|
ossandust
Hi Mike,
what you try is not possible. Actually it should be possible as follows : EFFECT_OFF Record =
{
//SendMidi ...
if(!$triggeredFromPlayButton ) {
$triggeredFromRecButton = true
SwitchOn Play
}
}
EFFECT_ON Play =
{
//SendMidi ...
if(!$triggeredFromRecButton ) {
$triggeredFromPlayButton = true
SwitchOff Record
}
} These conditions would make sure that the infinite loop actually never happens. Unfortunately the loop detection algorithm of the UnO2 compiler is not smart enough to interpret these conditions and decide that the code is safe. It just detects the "SwitchOff" and "SwitchOn" commands which call "each other" and decides there is a loop issue. If you have spare buttons, you could solve it by having 2 trigger buttons which you click for Play/Record, and 2 effect buttons which you use to show the Play/Record status (they can also be used to toggle Play / Record, but will not automatically start Play when stopping Record and vice versa) var $recording = false
var $playing = false
EFFECT_ON Record =
{
//SendMidi ...
$recording = true
}
EFFECT_OFF Record =
{
//SendMidi ...
$recording = false
}
EFFECT_ON Play =
{
//SendMidi ...
$playing = true
}
EFFECT_OFF Play =
{
//SendMidi ...
$playing = false
}
TRIGGER_CLICK togglePlay =
{
if(!$playing) {
SwitchOn Play
}
else {
SwitchOff Play
if(!$recording)
SwitchOn Record
}
}
TRIGGER_CLICK toggleRecord =
{
if(!$recording) {
SwitchOn Record
}
else {
SwitchOff Record
if(!$playing)
SwitchOn Play
}
}
|
|
Jack.taylor12@...
My response was not meant to be a comprehensive code written for his solution. Only a more correct direction and short example that will hopefully be a solution for them :)
|
|
Conditionals:
if ($Recording) (SwitchOn Play) The above statement says, "If the variable $Recording is True, do the command (SwitchOn Play)." If there's only one command to be run, it can go right after the 'If' condition in parentheses, as above. If you need more than one command to be executed, you need to put them in a 'block' right after the 'If'. A block is the same as in EFFECTs, PRESETs, TRIGGERs, etc. definitions, and is defined by the same curly braces - { }. EFFECT_OFF Record =
{
SendMidi ...
$recording = false if (!$Playing) { SwitchOn Play [any other commands] } [any commands to always be executed can go here] }
}So now when Record is activated, it sends the MIDI, sets $recording to false, then if $Playing is Not True (see below), Play is switched on. Anything after the closing brace of the 'if' statement is executed normally. The generic form is: if (condition) { [command(s) to run if 'condition' is True] else { [command(s) to run if 'condition' is False] } The last three lines starting with 'else' are optional - if you have no commands to execute on a false condition, just leave it out (blank 'else' statements just take up space). The opening brace '{' can be either on the same line as the 'if' (or 'else'), or on the next line as in the examples on this demo page: https://www.fcb1010.eu/uno2_demo.html Conditional Operators: The result of a condition can only be one of two things: True or False. The generic term for this is called Boolean. You use Conditional Operators to compare numbers, variables, (or strings, but that's a bit more complicated). You probably remember '>' and '<' from school, 'less than' and 'greater than'. The various comparisons you can use, from the manual: $var > nn // is bigger than
$var >= nn // is bigger than or equal to
$var == nn // is equal to
$var != nn // is not equal to
$var <= nn // is less than or equal to
$var < nn // is less than
Since Boolean values are already True or False, you don't need to compare them with anything (but you can!). "If ($myboolvar)..." is the same as "If ($myboolvar == True)..." You can use the '!' (not) to reverse the condition: "If (!$myboolvar)..." tests to see if $myboolvar is False, and is the same as "If ($myboolvar != True)" or "If ($myboolvar == False)". Conditionals like this are pretty much the same for every programming language, so if you want to learn more, you can search the internet or find a good book. |
|
Jack.taylor12@...
I think I see what is happening. I would add another If and Variable in Effect Off. That activates the SwitchOn Play only when the Record Off button is pressed, not when linked from the effect on Play. Hope that makes sense. Example......... EFFECT_OFF Record =
{
SendMidi ...
if ($Recording == 1) (SwitchOn Play) else { } $recording = false
} |
|
Mike Watkinson
Not for the first time I find myself slightly confused by conditionals and hope that someone can suggest a route to resolve the following requirement:
Two switches assigned as 'effects' - but I would like each one to also switch the other one in some cases (as in make the lights go on and off) Switch1 On Switch1 Off turns Switch2 On (or stays on if it is already on) Switch2 On turns Switch1 Off (if it is on) Switch 2 Off Switch 1 is Record, Switch2 is Play Seems simple enough but my attempt ends in an activation loop. Here's what I have: var $recording = false
var $playing = false
EFFECT_ON Record =
{
SendMidi ...
$recording = true
}
EFFECT_OFF Record =
{
SendMidi ...
SwitchOn Play $recording = false
}
EFFECT_ON Play =
{
if ($recording)
SwitchOff Looper 1 Record //(this is the line that causes the activation loop)
SendMidi ...
else if (!$recording)
SendMidi ...
$playing = true
}
EFFECT_OFF Play =
{
SendMidi ...
$playing = false
}
Apologies to those who know what they are doing - the above is probably complete garbage! Any help appreciated :) |
|