Index all elements in loop

Is there a way to write a for loop over all elements, say in a PO16?

I’d like to write a for loop for the utility button which sends the values of all elements when pressed, but I don’t know how to address each element in turn when there is no “self” available.

You can reference any element with element[i] where i is the element number. Elements start at ‘0’.

So, on the ‘Utility’ tab on the ‘System’ element, you could do something like:

for i=0,15,1 do
     midi_send(element[i],176, cc, element[i]:potmeter_value()
end

The only thing is referencing ‘cc’ in the above as that would be a local variable programmed on each elemenet. Unless your ‘cc’ values are sequential then you could put the starting ‘cc’ value and use the iterator to increase it’s value:

for i=0,15,1 do
     cc = i + 70
     midi_send(element[i],176, cc, element[i]:potmeter_value()
end

So, if element 0 sends CC70 and element 1 sends CC 71 etc., this would work.

Note that functions that have a parameter that reference an element directly by its ID do not require the element[i] code to call them. One example is the led_value() function. Its first parameter is the element ID.

Quick and dirty answer as I’m off to work :slight_smile:

1 Like

Thanks so much for this, really appretiated. I will try this.

To help me make better use of the docs in future, could you tell me where the bit about element[i] might be in docs? I tried but couldn’t find it.

This worked perfectly on my PO16.

for i = 0, 15, 1 do
    cc = (32 + module_position_x() * 16 + i) % 128
    val = element[i]:potmeter_value()
    print("sending val:" .. val .. " to cc:" .. cc)
    midi_send(element[i], 176, cc, val)
end

In future I will experiment with several different units joined together, to see if I can send data from all of them with just one click.

1 Like

I would recommend using a non-module specific way for parameterizing your loop in this case. Using the element_count() function should do just that.

This function returns the number of actual elements on the module, for example 13 for the PBF4 and 9 for the EF44 (the physical elements + system element).

Utilizing this in a loop would look like this:

for i=0, element_count()-2 do 
-- we use "-2" here because we don't want address the system element (-1)
end

The above solution will work across all types of modules, regardless of the amount of elements that they have.

Information about elements could be found here: More about: Variables | Intech Studio Documentation

3 Likes

That addresses the itch I had about hard coding. Also the link to the documentation, which I’ll study carefully.

1 Like