Mapping input numerical ranges to other numbers in MAX

GGH's icon

Hi guys, I'm a bit new to javascripting so bear with me. I'm trying to create an object that will take an input with a certain range, say x =[200, 600] and mapping that range using "if else statements" onto various output ranges say y =[1, 10] and based upon this calibration direct the outputs out 10 separate outlets.

ie

if 200
output y = 1 to an outlet 1

and so on until y = 10

or what would the equivalent of this switching code in javascript:

// these constants won't change:
const int sensorMin = 0; // sensor minimum, discovered through experiment
const int sensorMax = 600; // sensor maximum, discovered through experiment

void setup() {
// initialize serial communication:
Serial.begin(9600);
}

void loop() {
// read the sensor:
int sensorReading = analogRead(0);
// map the sensor range to a range of four options:
int range = map(sensorReading, sensorMin, sensorMax, 0, 3);

// do something different depending on the
// range value:
switch (range) {
case 0: // your hand is on the sensor
Serial.println("dark");
break;
case 1: // your hand is close to the sensor
Serial.println("dim");
break;
case 2: // your hand is a few inches from the sensor
Serial.println("medium");
break;
case 3: // your hand is nowhere near the sensor
Serial.println("bright");
break;
}

}

spectro's icon

You may have good reasons to do this within a JS, but FWIW this is pretty easily done in max land. Check [split]

Max Patch
Copy patch and select New From Clipboard in Max.

GGH's icon

Thanks that does help a lot.

the_man361's icon

you could probably also use the regular if else max object on your data stream