Tuesday, 13 May 2014

Mapping softpot sliders (Values 0-100)

Softpot is a linear touch potentiometer, instead of turning a knob, you touch it and getting a value range from 0 - 1023.
Since Ableton uses normally values from 0-100 mapping for our values is reguired.

map(value, fromLow, fromHigh, toLow, toHigh)

Description

Re-maps a number from one range to another. That is, a value of fromLow would get mapped to toLow, a value of fromHigh to toHigh, values in-between to values in-between, etc.
Does not constrain values to within the range, because out-of-range values are sometimes intended and useful. The constrain() function may be used either before or after this function, if limits to the ranges are desired.
Note that the "lower bounds" of either range may be larger or smaller than the "upper bounds" so the map() function may be used to reverse a range of numbers, for example
y = map(x, 1, 50, 50, 1);
The function also handles negative numbers well, so that this example
y = map(x, 1, 50, 50, -100);
is also valid and works well.
The map() function uses integer math so will not generate fractions, when the math might indicate that it should do so. Fractional remainders are truncated, and are not rounded or averaged.

 code:

/* Map an analog value to 8 bits (0 to 255) */
void setup() {}

void loop()
{
  int val = analogRead(0);
  val = map(val, 0, 1023, 0, 255);
  analogWrite(9, val);
}
 -----------------------------------------------------------------------------------------------------------------------
 Mapping softpot sliders output Values from 0 - 1024 converted to  0 -100

 code:

int softpotPin0 = A0; //analog pin 0
int softpotPin1 = A1; //analog pin 1
int softpotPin2 = A2; //analog pin 2

void setup(){
 digitalWrite(softpotPin0, HIGH); //enable pullup resistor
 digitalWrite(softpotPin1, HIGH); //enable pullup resistor
 digitalWrite(softpotPin2, HIGH); //enable pullup resistor

 Serial.begin(9600);
}
void loop(){
 int softpotReading0 = analogRead(softpotPin0);
 int softpotReading1 = analogRead(softpotPin1);
 int softpotReading2 = analogRead(softpotPin2);
 int val1 = analogRead(softpotPin0);
 int val2 = analogRead(softpotPin1);
 int val3 = analogRead(softpotPin2);

  val1 = map(softpotReading0, 0, 1023, 0, 100);
  val2 = map(softpotReading1, 0, 1023, 0, 100);
  val3 = map(softpotReading2, 0, 1023, 0, 100);



  Serial.print(" ,out0: ");
  Serial.println(val1);
  Serial.print(" ,out1: ");
  Serial.println(val2);
  Serial.print(" ,out2: ");
  Serial.println(val3);


  delay(250); //just here to slow down the output for easier reading
}






No comments:

Post a Comment