# Converting float to int

Hi guys,

Im building an android program in processing and I need some help. I would of postid this in the processing forum but it should be an easy question and nobody is there?!!? Also processing language is very similar if not identical to the arduino language.

So I need to convert a float(accelerometer value) to an integer between 0...255:

``````         ax = round(ax + 10.0);
convertx = int (ax);
convertx = map(convertx, 0,20,0,255);
Roll = convertx;

ay = round(ay + 10.0);
converty = int (ay);
converty = map(converty,0,20,0,255);
Pitch = converty;
``````

I followed this example:http://processing.org/learning/basics/datatypeconversion.html

But when I try to compile my code it says: convertx = map(convertx, 0,20,0,255); [javac] ^ [javac] required: int [javac] found: float

So this means that convertx is not an int. Why? What do I need to change?

You need to declare convertx to be an int, [u]or[/u]

``````Roll = map((int)ax,0,20,0,255);
``````

and probably do the same kind of thing for Pitch

I had declared convertx as an integer at the beginnign and I also tried to substitute what I had with the code you suggested but I still get the same error?

Try the code you have been given by Morris before you come back saying it still doesn't work...

If you read all I write, you wouldnt be saying that :D

Yeah, I guess I might have been a bit unclear, I tried his code and It still gives me the same error.

Then it appears that software (other than yours) is confused because with the (int) cast in place, there should be no way to interpret the first parameter as a float.

If you don't mind doing a quick experiment, try adding a pair of parentheses as follows:

``````Roll = map(((int)ax),0,20,0,255);
``````

and let me know if that helps...

Unfortunatelly it doesnt work(still same problem).... And of course I dont mind doing experiments :).

I just fired up the Arduino IDE and compiled

``````void setup(void)
{   int ax = 5;
int ay = 5;
int convertx;
int converty;
int Roll,Pitch;

ax = round(ax + 10.0);
convertx = int (ax);
convertx = map(convertx, 0,20,0,255);
Roll = convertx;

ay = round(ay + 10.0);
converty = int (ay);
converty = map(converty,0,20,0,255);
Pitch = converty;
}
void loop(void)
{   /* stub */
}
``````

And got a clean compile. I suspect that if you've done what you said, there may be a gremlin loose in processing...

Here's the whole code, its a bit long and needs optimizing but it might help:

``````import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import java.util.ArrayList;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;
import apwidgets.*;

private static final int REQUEST_ENABLE_BT = 3;
ArrayList devices;
BluetoothDevice device;
BluetoothSocket socket;
InputStream ins;
OutputStream ons;
boolean registered = false;
PFont f1;
PFont f2;
int state = 0;
int convertx;
int converty;
//////////////Values//////////////
int Throttle;
int Roll = 50;
int Pitch = 50;
int Yaw = 50;
String error;

////////Accelerometer//////////
AccelerometerManager accel;
float ax, ay, az;
///////////////////////////////
APWidgetContainer widgetContainer;
APButton button1;
APButton button2;
int rectSize = 100;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
background(0);
f1 = createFont("Arial",50,true);
f2 = createFont("Arial",25,true);
textAlign(CENTER);
translate(width / 2, height / 2);
textFont(f1);
text("Connecting...",0,0);
stroke(255);
accel = new AccelerometerManager(this);
orientation(LANDSCAPE);
widgetContainer = new APWidgetContainer(this);
button1 = new APButton(10,245,100,75,  "Decrease");
button2 = new APButton(380,245,100,75, "Increase");

}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void draw() {
switch(state)
{

case 0:
connectDevice();
break;
case 1:
MainLoop();
break;
case 4:
Errors();
break;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void onStart()
{
super.onStart();
println("onStart");
{
{
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
else
{
state = 0;
}
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void connectDevice()
{
try
{
socket = device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
/*
Method m = device.getClass().getMethod("createRfcommSocket", new Class[] { int.class });
socket = (BluetoothSocket) m.invoke(device, 1);
*/
socket.connect();
ins = socket.getInputStream();
ons = socket.getOutputStream();
println("Connected");
state = 1;
}
catch(Exception ex)
{
state = 4;
error = "Could Not Connect";
println(error);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////ACELEROMETER////////////////////////////
public void shakeEvent(float force) {
println("shake : " + force);
}

public void accelerationEvent(float x, float y, float z) {
//  println("acceleration: " + x + ", " + y + ", " + z);
ax = x;
ay = y;
az = z;
}
/////////////////////////////////////////////////////////////////////////////////////////

void MainLoop()
{
try
{
/*   while(ins.available() > 0)
{
}
*/
ons.write(Throttle); // Throttle
ons.write(Roll); // Roll
ons.write(Pitch); // Pitch
ons.write(50); // Yaw
ons.write(50); //Aux1

}
catch(Exception ex)
{
state = 4;
error = ex.toString();
println(error);
}

///////MAIN LOOP MAIN LOOP MAIN LOOP MAIN LOOP MAIN LOOP MAIN LOOP MAIN LOOP////////

background(0);
fill(255);
textFont(f1);

Throttle = constrain(Throttle,0,255);
text(Throttle, width/2, height/2);

textFont(f2);
text("x: " + nf(ax, 1, 2) + "\n" +
"y: " + nf(ay, 1, 2) + "\n" +
"z: " + nf(az, 1, 2),
-190, 10, width, height);

ax = round(ax + 10.0);
Roll = map(((int)ax),0,20,0,255);

ay = round(ay + 10.0);
Pitch = map(((int)ay),0,20,0,255);

redraw();
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void onClickWidget(APWidget widget){
if(widget == button1){ //if it was button1 that was clicked
Throttle -= 5;
}
else if(widget == button2){ //or if it was button2
Throttle += 5; //set the bigger size
}
}
/////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Errors()
{
background(255, 0, 0);
translate(width/2, height/2);
textFont(f1);
text(error,0,0);
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void onStop()
{
println("onStop");
/*
if(registered)
{
}
*/

if(socket != null)
{
try
{
socket.close();
}
catch(IOException ex)
{
println(ex);
}
}
super.onStop();
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
``````

It has got to be a processing bug.... Do you have maybe a suggestion?

You actually declared ax and ay as floats, so I made that change (to floats) in my source and still got a clean compile in a plain-vanilla Arduino IDE.

About the only suggestion I could make might be to fix in processing…

:/ Im going to report this bug and see if they will fix it.

Ok this is INTERESTING, I decided to make a simple sketch just to see if it works:

``````float x;
int a;

void setup(){
x = 55.6;
}
void draw(){

background(255);
x = round(x);
a = int(x);
rect(0,0,a,50);
}
``````

And what do you know, It WORKS, processing compiled it sucessfully so now im really confused. What doesnt it like about my sketch?

Never mind, it was false hope, it seems that processing doesnt like the map function or it doesnt convert float to integer:

``````float x = 55.6;
int a;

void setup(){

}
void draw(){

background(0);
fill(255);
x = round(x);
a = int(x);
a = map(a,0,60,0,100);
rect(0,0,a,50);
}
``````

Error:

a = map(a,0,60,0,100); [javac] ^ [javac] required: int [javac] found: float

Ok. Try this:

``````float x = 55.6;
int a;

void setup(){

}
void draw(){

background(0);
fill(255);
x = round(x);
a = (int)x;
a = 100 * a / 60;
rect(0,0,a,50);
}
``````

Tried it, it works but not sure how it helps? I also added a println of a and the value returned is 93 which seems about right. So it must be the map function that doesnt work?

Dunno. I made /two/ changes: (1) I re-wrote your cast so that it couldn't be confused with a function call and (2) did the mapping the "hard" way.

LOL I should start reading the processing referance:http://processing.org/reference/map_.html

I assumed the map function worked for integers but its the opposite(it works for float).However it was the compiler that told me the wrong thing :).

Thanks a lot for the help :D.