Forums > Javascript

Mapping input numerical ranges to other numbers in MAX


GGH
May 11, 2010 | 2:02 pm

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< =x<=250 then y = 1
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;
}

}


May 11, 2010 | 2:17 pm

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

– Pasted Max Patch, click to expand. –


GGH
May 11, 2010 | 2:39 pm

Thanks that does help a lot.


May 12, 2010 | 12:38 pm

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


Viewing 4 posts - 1 through 4 (of 4 total)