We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I'm communicating with a microcontroller using the serial port using a demo I found and it works great. The issue is I do not want a floating point number to go to the microcontroller. Right now it does, using the code below.
What I would like to do is drop the decimal (or round off) "subtext[counter]" before sending via myPort.write. I have tried converting it to an int, but then myport.write doesn't like it. If there is a way to send an INT via myPort.write that would also be a good solution.
if(counter<subtext.length){
pan = Float.valueOf(subtext[counter]);
pan = (pan + 0.01) * 11.2;
(subtext[counter]) = Float.toString(pan);
myPort.write(subtext[counter]);
Answers
https://Processing.org/reference/nf_.html
nf expects ints, any way to do this with a string?
Can't you just use your
float
pan variable w/ nf()?Use the
round
function to round the float to the nearest integer.Yeah I've tried nf() and round. I'm using a serial monitor and I still see a decimal being transmitted.
I think I can work around this if I could send an int vs string via serial.
Is there a way to use myPort.write to send an int?
How about
(subtext[counter]) = Integer.toString(round(pan));
Thanks. That helped, the output lost the decimal. But, now it's not doing the other math I have in there. Can you check this for me?
float pan = Integer.parseInt(subtext[counter]);
pan = (pan + 0.01) * 11.2;
(subtext[counter]) = Integer.toString(round(pan));
myPort.write(subtext[counter]);
What's the code markdown here anyway?
Thanks again.
From the nf() reference:
If you only want to remove some of the floating point digits then it gets a bit more complex. But in your case, you want just an int.
The question is whether you want low/low results:
or low/high results:
int()
,(int)
, orfloor()
round()
You can also use
str()
to convert any result to string.Examples:
Output:
Line 2 will take the value in
pan
add 0.01 and multiply by 11.2 and then store the result inpan
overwritng the previous value.Line 3 rounds the value to the nearest integer and converts it to a String leaving
pan
unchangedWhat other math???
No I don't use the serial library perhaps someone else can help you.
Code markdown read the sticky post Format Text and Code
Thanks for all the help guys. We're up and running.