Go Down

Topic: Problems using System.println with a floar variable (Read 418 times) previous topic - next topic

dan_plan

Hi.

This is my test sketch:

Code: [Select]
void setup()
{
  Serial.begin(9600);
 
  float a = 1.23;
  Serial.println(a); 
 
  float b = 5/255;
  Serial.println(b);
}

void loop()
{
}


This is its output:
Code: [Select]

1.23
0.00


Can you please help me understand why it is printing 0.00 ?  How can I print my float variable?

Thanks,
Dan

AWOL

#1
Nov 14, 2012, 04:59 pm Last Edit: Nov 14, 2012, 05:01 pm by AWOL Reason: 1
It is printing the value of your float variable, the value of which is 0.00.

Code: [Select]
float b = 5.0/255.0;
or
Code: [Select]
float b = (float)5/(float)255;  (though one "float" would be sufficient)
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

dan_plan

Thanks a lot, that worked!

How can I vote for your answer so to increase your karma?

dxw00d

Click on the plus sign next to the Karma figure.

dan_plan

I can't see it - probably I am too new to the forum for being able to vote :-(

Go Up