Go statements break the straight flow of sequence play to continue somewhere else. There are three kinds: Go to, Go to with chance, and Go between. The short description of each is here:

       Go To label with 25% chance  //one-quarter chance of going to label
                                    //three-quarters likely to go to the next statement
       Go Between label1 And label2 // 1/3 or 33% chance of going to each label. 
                                    //Never continues to the next statement


label1
      //code here
      go to ending      //this Go To occurs always
label
      //more code 
      go to ending      //this Go To occurs always
label2
      //and more code
      go to ending      //this Go To occurs always

ending                  //reaches this point and falls through to repeat

That’s all three in a tiny nutshell. Learn them well and you need go no further.

Back to Menu

If you want to know more, here they are in all their glory. The following covers

  • Labels
  • Go To
  • Go Between
  • Go To with Chance
    • Random Loop Counter
    • Number of Loops
      • Eyeball Estimates
  • Randomizing a non-random Sequence

Labels

Anything that is “legal” and is not a number or a keyword is considered a label. Upper/lowercase letters are treated alike. Underscores “_” separate words for readability, so does camelcase.

loopName            //camelcase makes words readable by humans
within              //oops! Illegal keyword is already used by Fade To statement
myTable@home        //bzzz. Try again
another_loop_name   //underscores make it readable by humans

                   GO TO LOOPname  //case need not match label

Typos can be interpreted as labels. They are usually easy to spot. But how about this: A programmer may accidentally type “goto” instead of “go to”. It looks alright but since goto isn’t a keyword, it will become a label. It will cause other errors.

loop1
     //code here
     goto loop1     //oops, "goto" typo is now a label, "loop1" becomes a duplicate label
     //other code here
     goto loop1     //oops, "goto" typo is now a duplicate label too.

Limits:

A label can be any length (well, billions of characters).

A sequence may contain up to 250 labels. The number of Go statements to those labels is not limited.

Go To Label

Go To Label statements always reach their destination. They take the following form:

//Two Go To examples
//one forward, one back

    //code here would play once on startup

loop    //this is a label

    //code here would play repeatedly

@+5ms

    go to skip   //goes forward in the sequence

    //code here would be skipped, would never play

skip    //this is a label

    go to loop   //goes back to an earlier point in the sequence

There is one strict rule: The Go To Loop, which is a backward reference, must never go to a label that shares the same time stamp as the Go statement that went to it.  This would cause an untimed loop, which is too fast to be seen, it would hog CPU time and be halted by the Morsel. If power is cycled, the Morsel will try to run it again, most likely with the same result.

Go Between label1 And label2

A Go Between statement always goes somewhere, but you don’t know where, it’s random. Label1 and label2 specify a range of labels inclusive that it can go to, and all are equally likely:

//Go Between example
//LED brightness is set randomly and held for 0.5s

full
        chn1 = 100%
        go between full and ninth
third
        chn1 = 33%
@+0.5s  go between full and thirtieth

ninth
        chn1 = 11%
@+0.5s    go between full and thirtieth

altThirtieth
thirtieth
        chn1 = 3.3%
@+0.5s  go between full and thirtieth

        //never falls through

Notice the middle labels never get mentioned by the Go Between statement. That’s okay, because the statement uses them too, each with an equal chance of setting a different brightness.

Notice too that the last brightness has two labels, thirtieth and altThirtieth. The presence of two labels means this code has twice as much chance of occurring as the others. Since there are 5 labels, each one has a 1/5 or 20% chance. The 3.3% brightness choice has a 40% chance.

Here’s another game we can play with the choice of labels.  We made the first Go Between different from the others. When the LED hits 100% brightness, we decided not to let it fall all the way to a thirtieth next time, only allowing down to 1/9 brightness on the following change.

Sometimes the code you want to go between has other go to’s and labels contained within it. These are fair play for the Go Between statement. In many cases, it may look okay to go to these inner labels, but if it is not, a Go To table can be used.

//Go Between with table

//go to table
chooseFull              Go to full
chooseHalf              Go To half
altChooseQtr chooseQtr  Go To qtr    //twice as likely

//the code
full
           chn1 = 100%
waitFull
@+50ms
           Go To waitFull with 90% chance
           Go Between chooseFull and chooseHalf  //don't transition to dimmest choice
half
           chn1 = 50%
waitHalf
@+50ms
           Go To waitHalf with 90% chance
           Go Between chooseFull and chooseQtr
qtr
           chn1 = 25%
waitQtr
@+50ms
           Go To waitQtr with 90% chance
           Go Between chooseFull and chooseQtr

//never falls through

The table ensures that the labels we specifically choose have equal probability of occurring. Once again, we played our old tricks: The Go Between following the “full” label omits the dimmest choice and the last Go Between was made twice as likely with the alternate label.

If we had not used the table, the wait labels would also be targets for the Go Between statements. That might be okay for your purposes, but it works differently:  When going to a wait loop, the brightness setting instruction is skipped. In that case, the prior brightness lasts around twice as long.

Go To Label With Chance

Go To With Chance uses a probability that a Go will be taken. It might not.  Chance is a percentage from 0.0% (will never happen) to 100.0% (will always happen, well, this one comes to 99.4% likely). This percent scale was also seen for LED brightness (chn1 = 50%), but LED brightness is definite, not random.

The previous Go Between example can be written with Go to with chance statements instead

//Go To with Chance

//the code
full
 chn1 = 100%
waitFull
@+50ms
 Go To waitFull with 90% chance

 Go To full with 50% chance    //Either Full or Half, not dimmest choice
half
 chn1 = 50%
waitHalf
@+50ms
 Go To waitHalf with 90% chance

 Go To full with 33% chance   //maybe full
 Go To half with 50% chance   //maybe half or falls through to qtr
qtr
 chn1 = 25%
waitQtr
@+50ms
 Go To waitQtr with 90% chance

 Go To half with 33% chance   //maybe half
 Go To qtr with 50% chance    //maybe qtr
 Go To full                   //or maybe falls through to full (optional Go To)

Not as compact but it works.

Random Loop Counter

Here is an example of a random loop counter.

//Go To with Chance
//Randomized loop count

          chn1 = fade from 0% to 100% for 5s  //slooow fade
@+5s

blinkLoop
          chn1 = 100%
@+100ms   chn1 = 0%
@+100ms   go to blinkLoop with 90% chance  //eyeball estimate 10 loops typical

         //it could fall through and replay the slooow fade

The statements following blinkLoop are eyeball-estimated at occurring around 9 times before falling through, but that can be as little as once to more than 100, just by chance. But there is an average range we call eyeball estimates.

Number of loops and

Eyeball Estimates

We can calculate the chance of a loop happening a certain number times. That number is not obvious, because in loops, the probabilities are multiplied.

The next table may be a bit much to take in.  That is why we follow it with eyeball estimates.

                       Chance of looping at least...
Go To Chance   Never   Once    Twice    3 Times   10 Times  100 times

    1%          99%     1%      .01%     0.*       0.*       0.*
   33%          66%    33%      11%      3.6%      0.*       0.*
   50%          50%    50%      25%     12.5%      .1%       0.*
   80%          20%    80%      64%     51%        11%       0.*
   90%          10%    90%      81%     73%        35%       0.*
   95%           5%    95%      90%     86%        60%       0.6% 
   97%           3%    97%      94%     91%        73%       5%
   98%           2%    98%      96%     94%        82%      13%
   99%           1%    99%      98%     97%        90%      37%
   
*very small chance, but possible

But that doesn’t tell us how many times it will loop. For that, we can quickly arrive at an eyeball estimate that is pretty good.

                Eyeball Estimates: How many times will it loop?
                        Go To label With NN Chance
Go To Chance
    NN        In words           Eyeball Estimate       Actual at

    1%     rarely loops again    rare exceptions        1%
   33%     unlikely to loop      occasionally           33%
   50%     1 in 2 chance         1 loop, half the time  50% 
   66%     2 in 3 chance         loops about twice      44% chance to loop twice or more
   80%     4 in 5 chance         loops around 4 times   41% chance of 4 loops or more
   90%     9 in 10 chance        loops around 9 times   39% chance of 9 loops or more
   95%     19 in 20 chance       loops around 19 times  38% chance of 19 loops or more
   97%     32 in 33 chance       loops around 32 times  38% chance of 32 loops or more
   98%     49 in 50 chance       loops around 49 times  37% chance of 49 loops or more
   99%     99 in 100 chance      loops around 99 times  37% chance of 99 loops or more

Keep in mind, the loop always executes once before reaching the Go To With Chance. So add one.

//Eyeball Estimate Loop Example:

         chn1=100%
@+100ms
         chn1=0%               //How long will it stay turned off?
loop
@+100ms
         go to loop with 90% chance //about how many loops? Try it!

//falls through and repeats

 

 

Now you possess massive amounts of knowledge on Go statements. Knowledge is power. And power can be used for both good and evil. Effects, that is.

 

Back to Menu