Go Down

Topic: Create a proportional scale of numbers where 10 = 0 and 60 =255 (Read 840 times) previous topic - next topic

joeedel89

I have a sensor where the largest value I expect is 60 and the minimum is 10.  I have the code working but my leds never get very bright (255 is max brightness) or all the way off (10 is dim but not off).  The part of my code that pertains to the question looks like this.

  Serial.println(averageValue); // So I can see the reading
  for(int i = 0; i < 10; i++ ) {
    if (averageValue > 60)
    {
      averageValue = 60; // Limits high reading
    }
  leds.r = averageValue, leds.g = averageValue, leds.b=averageValue;
  }
  FastSPI_LED.show();

I would like to create a "scale" where 10 = 0 and 60 = 255 and all the numbers in between are proportional (ie. 10 = 0, 35 = 127.5, 60 = 255 and of course include all the other numbers in between)  I googled for a while but don't really even know what to call what I am looking for.

Thanks



robtillaart


and you can use constrain(x, lower, upper)  to limit the value.  But be aware it is a macro....

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

joeedel89

Thank you both, the map was what I was looking for makes prefect sense.  It seems when I try to constrain the arduino does weird things so I will work with that later.

  averageValue = map(averageValue,0,60,0,255);
  Serial.println(averageValue); // So I can see the reading
  for(int i = 0; i < 10; i++ ) {
    if (averageValue > 255)
    {
      averageValue = 255; // Limits high reading
    }
  leds.r = averageValue, leds.g = averageValue, leds.b=averageValue;
  }
  FastSPI_LED.show();

PaulS

Code: [Select]
leds.r = averageValue, leds.g = averageValue, leds.b=averageValue;
I seriously doubt that this code is doing what you think it is. The comma operator is just about the most abused operator that C has. And, you are abusing it.

Go Up