The source of the code I'm trying out:
http://interface.khm.de/index.php/lab/experiments/nonlinear-mapping/
Or, below:
// curve mapping with n interpolation points
// KHM 2010 Lab3
// nodes for linear equations / nodepoits are concatening lines
float nodepoints[5][2]= {
{
0,10 }
, {
10,50 }
, {
20,150 }
, {
40,200 }
,{
50,200 }
};
void setup() {
Serial.begin(115200);
}
void loop() {
// test
// a given linear input leads to remapped output
for ( int ii = 0; ii <= 50; ii++) {
Serial.print(ii);
Serial.print(" ");
int result= reMap(nodepoints,ii);
Serial.print(result);
Serial.println(" ");
}
while(1);
}
//***************************************************************************
//
int reMap(float pts[10][2], int input) {
int rr;
float bb,mm;
for (int nn=0; nn < 4; nn++) {
if (input >= pts[nn][0] && input <= pts[nn+1][0]) {
mm= ( pts[nn][1] - pts[nn+1][1] ) / ( pts[nn][0] - pts[nn+1][0] );
mm= mm * (input-pts[nn][0]);
mm = mm + pts[nn][1];
rr = mm;
}
}
return(rr);
}
[first issue solved]First of all, when I try this sketch with no modifications whatsoever, nothing prints to Serial (specifically: a brief burst of junk, then nothing). Could this be a configuration issue on my part?
Second, I'm making an led light up with varying brightness with sound input, and remapping the values. After a trigger, I experience a "trail" effect; in other words, after the triggering signal ceases, the light instantly goes to what looks like full brightness, and then trails down to off over the course of 1 sec.
This behavior sounds to me like something funny is happening with exponential curves, but I can't for the life of me figure out what that would be. I'm investigating the hardware on my end, but if someone might catch something in the code that I'm missing, that would be greatly appreciated. Thanks for your help,
Josh
My code:
int micPin = 2;
int pot = 0;
int light = 11; //$!!!!!!!!!! LOOK IT's 11! *****$$$
int micValue = 0;
int threshold = 0;
// nodes for linear equations / nodepoits are concatening lines
float nodepoints[6][2]= {
{
0,0 }
, {
40,0 }
, {
70,75 }
, {
100,150 }
, {
150,225 }
,{
300,255 }
};
void setup() {
pinMode(light, OUTPUT);
Serial.begin(115200);
}
void loop() {
micValue = analogRead(micPin);
threshold = analogRead(pot);
int result= reMap(nodepoints,micValue);
analogWrite(light, result);
};
int reMap(float pts[10][2], int input) {
int rr;
float bb,mm;
for (int nn=0; nn < 4; nn++) {
if (input >= pts[nn][0] && input <= pts[nn+1][0]) {
mm = ( pts[nn][1] - pts[nn+1][1] ) / ( pts[nn][0] - pts[nn+1][0] );
mm = mm * (input-pts[nn][0]);
mm = mm + pts[nn][1];
rr = mm;
}
}
return(rr);
};