Trouble with Serial.println() and float variables

Hello all,

I am new to Arduino and am working through the tutorials.

I am attempting to send the value of a float variable back to my debug screen through the serial port and it works fine using integer variables, but I get a compile error if I try it with a float variable. I have the Arduino NG rev. C and am using Arduino 0007 software.

Here's the code:

int value = 0;
float value3 = 0;
float value4 = 0;

int ledpinR = 3;
int ledpinG = 5;

void setup()
{
Serial.begin(9600); // initialize serial communications at 9600 bps
}

void loop()
{
for(value = 255; value >=0; value-=5) // fade red and blue out -> now dark
{
value3 = value / 2.55; // scale the brighter blue LED to the red values
value4 = value3 * 100; // multiply the float by 100 because of the Serial.println() truncate

analogWrite(ledpinR, value);
analogWrite(ledpinB, value3);
delay(50);

Serial.println(value4); // Changing this to output an int type works fine
}

}

When I compile this I get the error:

In function 'void loop()':
error: call of overloaded 'print(float&)' is ambiguous/Users/alan/local apps/arduino/arduino-0007/lib/targets/arduino/HardwareSerial.h:42: note: candidates are: void HardwareSerial::print(char)
void HardwareSerial::print(uint8_t)
void HardwareSerial::print(int)
void HardwareSerial::print(unsigned int)
void HardwareSerial::print(long int)
void HardwareSerial::print(long unsigned int)

I know that Serial.println() truncates float values to integers, but the error seems to suggest that I can only output chars and ints types. Do I have a syntax or initialization problem?

Any help would be most appreciated,
Alan

You need to cast the float to a particular integral type first, otherwise the compiler doesn't know which one to use (even though they're all basically the same).

Try:

Serial.println((long) value4);

mellis,

Thanks!

I wondered if it needed to be converted, but the Serial.println() documentation specifically says that it would be truncated.

Thanks again,
Alan

Thanks for pointing that out; I corrected the reference.