The general idea for taking an average is the same with the Maestro, except since you cannot store samples in an array, you store them on the stack. For example, a very simple way to do it would be to take 10 readings of channel 2 by calling
2 get_position ten times, then calling
plus 9 times to add them all together, and get an average with
10 divide. Of course this is not a very efficient way of doing it and will only get less efficient if you want to average more samples. I wrote a quick subroutine to take the average as an example of what it could look like:
dup rot #this saves the number of samples to the bottom of the stack
begin 1 pick while #check the number of times it has looped
dup get_position #read channel and save it lower on stack
rot 1 minus rot
drop drop #once the channel is sampled, we can clear the unecessary looping info
begin depth 2 greater_than while #add together the samples taken
swap divide #divide by the number of samples taken
return #return the average reading
To use this subroutine, you can add it to your script (outside of your BEGIN/REPEAT loop) and call it with the channel number and the number of samples you want to average (e.g.
2 20 average will take an average of 20 readings on channel 2). Please note that this subroutine uses the
depth command, so it will not work correctly if there are other values on the stack when it is called (other than the channel number and number of samples). Also, keep in mind that values on the stack cannot exceed 32767 or they will overflow (and loop back -32768), so taking too many samples can cause problems if the sum of all of the sampled readings exceeds this limit.
There are probably more efficient or faster ways to do this, and I would still encourage anyone wanting to learn more about the Maestro scripting language to try writing their own version of a subroutine that does this. It is a good way to get a better understanding of the stack commands and how they work.