fader bank and crow

This was my first attempt to write some code using crow. I have a f8r and I didn’t feel like I was making good use of it. I have it outputting midi to control the settings on a disting mk4 and now I have it talking to crow so I am feeling much better about it. The code just creates a melody using clock, some cv to modulate or attenuate the voltage values from the faders. I think it sounds neat. Hopefully some other users can share how they use faders with crow. thanks

-- fader sequencer
-- in1: sampling clock
-- in2: control voltage
-- out1: fader note with slew and cv
-- out2: fader note with slew and cv
-- out3: fader note with slew and cv
-- out4: fader note with slew and cv

function init()
    metro[1].time = .1

pitch = {0,0,0,0,0,0}
val = 1
next_fader = 1

ii.faders.event = function( e, value)
    if e.name == 1 then
        pitch[1] = value
    elseif e.name == 2 then
        pitch[2] = value
    elseif e.name == 3 then
        pitch[3] = value
    elseif e.name == 4 then
        pitch[4] = value

metro[1].event = function()
    if next_fader >= 4 then
        next_fader = 1
        next_fader = next_fader + 1

input[1].change = function(s)
    --cv = math.floor(input[2].volts/2 + 3.5)
    cv = input[2].volts
    if cv > 5 then cv = 5 elseif cv < -5 then cv = -5 end
    val = pitch[next_fader] 
    if val < .01 then val = 0 end
    mod = val - cv
    if val == 0 then mod = 0 end
    if s then
        for n=1,4 do
            output[n].volts = mod/n
            output[n].slew = n * .01



You can clean up your change function using math.clamp if you want :slight_smile:

edit: thanks trent it’s actually util.clamp on norns

Thanks I appreciate that. I am excited to learn more about lua and about using the super smart crow module. So many possibilities all in 2hp. A small miracle.

The other day I tried math.clamp and I get the following error
userscript:42: attempt to call a nil value (field ‘clamp’). Would you mind showing me how to implement it. thanks

math.clamp is not in the lua standard library (so not on crow). I think it might have been added to norns as util.clamp and got confused?

Here’s a quick implementation:

function math.clamp(in, min, max)
    return math.max(min, math.min(max, in))

Thanks I’ll try definitely util.clamp I appreciate it.

util.clamp is correct - thanks @Galapagoose , edited my post

To clarify further, util.clamp is not available on crow (utility libraries are fewer on crow due to limited storage space). You’ll need to declare it like above, or just inline the math.min & math.max in your script. (or using your if/else/then structure is fine too).