system
June 20, 2012, 7:35pm
1
Hello!
I am trying to print the resistance of an LDR to the Serial Monitor as a float like this:
float resistance;
word RawADC;
char output[40];
void setup(){
analogReference(EXTERNAL);
Serial.begin(9600);
}
void loop(){
RawADC = analogRead(A0);
resistance = 10240000.0/RawADC - 10000.0;
sprintf(output, "ADC: %d\tResistance: %.2f\n", RawADC, resistance);
Serial.print(output);
delay(1000);
}
I used sprintf() for the conservation of space. However, this is the output:
ADC: 687 Resistance: ?
ADC: 687 Resistance: ?
ADC: 687 Resistance: ?
ADC: 688 Resistance: ?
ADC: 693 Resistance: ?
ADC: 692 Resistance: ?
ADC: 692 Resistance: ?
Why isn't the float printing correctly? How can I get sprintf() to correctly print the resistance to the char buffer with two digits after the decimal point?
If I download a program onto the Arduino using Arduino 1.0.1 that prints stuff to the Serial Monitor, move the Arduino to a different computer with Arduino 0023, will I still be able to download programs, and see the correct output on the Serial Monitor?
Thanks!
system
June 20, 2012, 7:38pm
2
The Arduino version of sprintf() doesn't support floats.
system
June 20, 2012, 7:40pm
3
I used sprintf() for the conservation of space
?
There's 13.5 billion light years of space - what about it needs to be conserved?
Why "word"?
Why not Serial.print?
Why the odd scope?
system
June 20, 2012, 7:49pm
4
AWOL:
There's 13.5 billion light years of space - what about it needs to be conserved?
I don't like crammed or repetitive code. You know that I am talking about space in the IDE.
AWOL:
Why "word"?
The word datatype is equivalent to unsigned int. analogRead() does not go into negative. What is wrong with that?
AWOL:
Why not Serial.print?
I did use Serial.print().
Oh well, I'll just do this. It is for the carbon dioxide project tomorrow. Why can't sprintf() do floats in Arduino?
float resistance;
word RawADC;
void setup(){
analogReference(EXTERNAL);
Serial.begin(9600);
}
void loop(){
RawADC = analogRead(A0);
resistance = 10240000.0/(float)RawADC - 10000.0;
Serial.print("ADC: ");
Serial.print(RawADC);
Serial.print("\tResistance: ");
Serial.println(resistance);
delay(1000);
}
system
June 20, 2012, 7:56pm
5
Why can't sprintf() do floats in Arduino?
It was removed, for the conservation of space.
I believe you can use dtostrf().
system
June 20, 2012, 7:58pm
6
You know that I am talking about space in the IDE.
I run my IDE on a machine with at least 4GB of RAM - why should I care how much memory the IDE takes?
If you've got a number that can't exceed 1/30th the range for an int, why bother worrying about size?
If you use Serial.print, you'll know it works for floating point types.
Sprintf takes bucket-loads of RAM to work properly.
Why global scope?
system
June 20, 2012, 8:03pm
7
AWOL:
I run my IDE on a machine with at least 4GB of RAM - why should I care how much memory the IDE takes?
Stop arguing about memory. It is just the look (I don't like repetitive code). Not memory. My computer only has 2GB RAM.
I'm not worrying about size.
Thank you.
system
June 20, 2012, 8:38pm
9
AWOL:
Why global scope?
What is wrong with that? I've seen examples that do global scope for these types of variables!
P.S. You cross-posted.
system
June 20, 2012, 8:56pm
11
Cross-posting means posting the same thing in separate threads.
I removed your taunt.
No offence.