Go Down

Topic: ramping up fading LED and suggestions (Read 504 times) previous topic - next topic

danieljay

so, Ive mapped sensor data to an LED's PWM values. with doing this, the fading is far from smooth. I've tried the example smoothing, but I'm not seeing any positive results. any feedback, suggestions, resources very appreciated. heres a chunk of code:

Code: [Select]
int values = cm; //cm = the values from sensor


  //forwrd fade
  outputValue1 = map(values, 25, 39, 0, 255);
  outputValue2 = map(values, 39, 25, 0, 255);
  outputValue3 = map(values, 2, 24, 0, 255);
outputValue4= map(values, 24, 2, 0, 255);
  // change the analog out value:
   }

  //analogWrite(led1, outputValue1);
  //  analogWrite(ledsignal, outputValue2);

  if(values >= 25 ){

    analogWrite(ledG1, outputValue1);
    analogWrite(ledG2, outputValue1);
    analogWrite(ledR1, outputValue2);
    analogWrite(ledR2, outputValue2);
    digitalWrite(ledY1, LOW);
    digitalWrite(ledY2, LOW);
  }

  if(values >= 2 && values <= 24)
  {
    analogWrite(ledY1, outputValue4);
      analogWrite(ledY2, outputValue4);
     analogWrite(ledR1, outputValue3);
    analogWrite(ledR2, outputValue3);
digitalWrite(ledG1, LOW);
digitalWrite(ledG2, LOW);

PaulS

Code: [Select]
  outputValue1 = map(values, 25, 39, 0, 255);
  outputValue2 = map(values, 39, 25, 0, 255);
  outputValue3 = map(values, 2, 24, 0, 255);
outputValue4= map(values, 24, 2, 0, 255);

Print out the values after mapping. You will see what the problem is. There is no overlap in the from range that any given value is values (why are you still using that lousy name?) can fall into such that all outputValueN values are in the to range.

danieljay

could you write in script what you mean? i don't understand.

PaulS

Quote
could you write in script what you mean? i don't understand.

No, I can't. I'll try again to explain, though.

The map function takes a value to map, a from range, and a to range. In your case, the values to map is in values, let's says its 22.

In the first call, you say that you want that value from the range 25 to 39 to the range 0 to 255. The value to be mapped is not in the from range (that is, it is not between 25 and 39. Therefore, the output value will not be between 0 and 255. It will, in fact, be a negative number.

In the second call, you say that you want that value from the range 39 to 25 to the range 0 to 255. The value to be mapped is not in the from range (that is, it is not between 25 and 39. Therefore, the output value will not be between 0 and 255. It will be greater than 255.

In the 3rd and 4th calls, the value is in the specified from range, so the output will be in the to range.

Suppose the value in values was 1. That value is not in the from range of any of the calls, so none of the output values will be between 0 and 255.

The same holds true if the value in values is greater than 39.

danieljay

i understand, but i think i left out crucial info about what effect i want. i basically want to emulate "momentum" with these LED's. so when the sensor data is scaling with the pwm, i would expect a delay would be visible and a lack of flickering with the led brightness as i go through the ranges.

I've tried incrementing the pwm max val (255) by different amounts with for statement, but results are same.

and thanks for the previous response.

Go Up