Ramp increment depending on incoming value

Hello,

I managed to connect PS3 controler using custom module, here is the result:

What I would like to do next is ramp up/down the increments for example of x axis depending on incoming value from joystick.
instead of “if else”, I tried using switch, from this example: https://www.w3schools.com/js/js_switch.asp in script widget, but I think syntax is a bit different there. I ended up with this in script widget:

var increment_1 = 0.001,
increment_2 = 0.01,
timeBetweenIncrements = 50 // ms

switch (value) {
case 0:
(value > 0.62) {
setInterval(()=>{
set(“fader_x”, get(“fader_x”) + increment_1)
}, timeBetweenIncrements)
}
break;
case 1:
(value > 0.82) {
setInterval(()=>{
set(“fader_x”, get(“fader_x”) + increment_2)
}, timeBetweenIncrements)
}
break;
case 2:
(value < 0.38) {
setInterval(()=>{
set(“fader_x”, get(“fader_x”) - increment_1)
}, timeBetweenIncrements)
}
break;
case 3:
(value < 0.18) {
setInterval(()=>{
set(“fader_x”, get(“fader_x”) - increment_2)
}, timeBetweenIncrements)
}
break;
case 4:
(Value == 0.5) {
clearInterval()
}
}

Console complains about syntax error: Unexpected token ‘{’

So far I am trying to get fader to do this, but ideally I’d like to do it with xy pad.
Tried incrementing x and y separately like this:
set(“xy_1[0]”, get(“xy_1[0]”) + increment)
But no luck…

Any help, much appriciated!

Thanks!

hi, might be of no help but find it interesting how exactly did you manage to get a controller working as input for OSC?

maybe somerhing like this… could help you

here it seems to the reaction has an offset (0.15 to 0.3)

Console complains about syntax error: Unexpected token ‘{’

You're mixing up if/else and switch syntaxes: switch - JavaScript | MDN

set(“xy_1[0]”, get(“xy_1[0]”) + increment)

set() and get() both take a widget id as first argument, this can't work. You could however do:

var v = get("xy_1")
v[0] = v[0] + increment // or shorter: v[0] += increment 
set("xy_1", v)

Hello!

I used SCP Toolkit for PS3 Controller to work on Windows (not the best solution as it locks Bluetooth to work only with controller)

Then I ended up using Fergo JoystickMIDI and loopMIDI to convert gamepad data to midi. Also can’t say it’s the best solution as there is bit of delay and it tends to crash over time.
But for R&D purposes it does the job :slight_smile:

Thanks for the links, I’ll look into it at some point.

Hi @jean-emmanuel

Thanks for this! I ended up using ton of “if” instead of switch. I bet it’s not clean way to do, but it works at least for sliders.
I tried your proposed method for xy pad, it works on a button click once, but if I try to make put it in interval, it doesn’t work.
I ended up with this:

var v = get(“xy_1”),
increment_1 = 0.001,
increment_5 = 0.009,
timeBetweenIncrements = 2 // ms

if (value) {
setInterval(()=>{
if (value < 0.2) {
(v[0] = v[0] + increment_5)
}
else if (value >= 0.2 && value < 0.45) {
(v[0] = v[0] + increment_1)
}
else if (value > 0.55 && value < 0.8) {
(v[0] = v[0] - increment_1)
}
else if (value >= 0.8) {
(v[0] = v[0] - increment_5)
}
}, timeBetweenIncrements)
}
set(“xy_1”, v)
console.log(v)
console.log(value)

Any idea why this might be the case?

Thanks in advance!

Your set() statement is outside of the setTimeout callback. Don’t forget to call clearTimeout() at some point if you don’t wan’t the callback to be called forever.

I got it to work, but also I run in a different problem:

Sliders on right are simulating joystick control
Setup as follows:
x and y each have it’s own script, accordingly x is incrementing v[0] and y is incrementing v[1], both of them do this: set(“xy_1”, v)
I understand that they are causing conflict, but not sure how to avoid it.

Never mind, I figured it out using 2 variables and 1 script!

Regardless, thank you for pointing me in right direction!