Convert 0.0-1.0 float to 0-255 int (from OSC)

I'm trying to use touchosc faders to dim an RGB LED.

I'm using android touchosc to processing to StandardFirmata.

As it is, I can turn the LEDs off when the float value = 1.0, otherwise they're on. I'd like to use analogWrite instead of digitalWrite but of course for that I need a value 0-255.

My approach has been to try to convert the float values, but is there another way?

My processing code is this:

// import oscP5 libraries and Arduino serial libraries
import oscP5.*;
import netP5.*;
import processing.serial.*;
import cc.arduino.*;
 
// make a new Arduino object
Arduino arduino;
 
// make a new oscP5 object
OscP5 oscP5;
 
  int r_fader = 255;
  int g_fader = 255;
  int b_fader = 255;
// run the setup loop
void setup() {

   // start oscP5, listening for incoming messages at port 8000
  // if your outgoing port on TouchOSC is not 8000, edit it here
  oscP5 = new OscP5(this,8000);
 
  // establish a serial connection with the Arduino at 57600 baud
  // this will use the first listing in the serial list, change if necessary
  arduino = new Arduino(this,Serial.list()[0],57600);
 
  // setup Arduino output
  arduino.pinMode(3, Arduino.OUTPUT);
  arduino.pinMode(5, Arduino.OUTPUT);
  arduino.pinMode(6, Arduino.OUTPUT);
 
}
 
// when OSC packets are received
 void oscEvent(OscMessage theOscMessage) {
 
  // set the current address to the control name
  String addr = theOscMessage.addrPattern();
 
  // get the float value of the control
  float floatval  = theOscMessage.get(0).floatValue();
  
  int message = (int) floatval;

  // get the int value of the control
//  int intval  = theOscMessage.get(0).intValue();
 
  // if the control toggled is toggle1, then toggle float value
  if(addr.equals("/1/fader1"))  { r_fader = message; }
  if(addr.equals("/1/fader2"))  { g_fader = message; }
  if(addr.equals("/1/fader3"))  { b_fader = message; }
   
}
 
// the program loop
void draw() {
 
  // write the int value of toggle button to pin 13 on Arduino
  arduino.digitalWrite(3, int(r_fader));
 
  // print the status in serial debugger
  println(r_fader);
  
  // write the int value of toggle button to pin 13 on Arduino
  arduino.digitalWrite(5, (int) g_fader);
 
  // print the status in serial debugger
  println(g_fader);
 
  // write the int value of toggle button to pin 13 on Arduino
  arduino.digitalWrite(6, (int) b_fader);
 
  // print the status in serial debugger
  println(b_fader);

}

My approach has been to try to convert the float values, but is there another way?

Convert them to what? Potato chips?

What do you do with them after converting them to whatever?

"Converting" a float in the range 0.0 to 1.0 to an int results in values of either 0 or 1. Converting by multiplying by 255, then truncating would result in values from 0 to 255.

Yes, that's exactly what I'm looking for, I just can't figure out how to do that.

You can't figure out how to multiply a value by 255?

PaulS:
You can't figure out how to multiply a value by 255?

Well when you put it like that it makes me sounds like an idiot.

I think my brain's just fried from all the other little concepts I've crammed in there over the last few days.

But thanks for the hint.

Agree 99.6% with PaulS,

just multiplying by 255 will give you only the value 255 if the float is exact 1.0

Better multiply the value with 255.999 and then truncate, this will result in a evenly distribution over the whole range 0.0 .. 1.0

int x = (int)(f * 255.999);