Hi, following code "should" return numbers with decimal points and three digits after the comma but it does not: the first number remains 0.000 and changes to either 1.000 or 2.000 depending on what is on A0.
void setup(){
Serial.begin(9600);
}
void loop(){
float current;
static char disp[7];
int rawcurr = analogRead(0);
for( int i=0; i<1; i++)
{
float current = (516 - rawcurr) * 45/1023;
dtostrf(current, 5,3,disp);
delay(5);
}
delay(100);
Serial.print(disp);
Serial.print(" , ");
Serial.println(rawcurr);
}
What do I do wrong?
Your mistake is that you are doing integer math and then saving the result in a float.
float current = (516 - rawcurr) * 45 / 1023;
It's OK to do the subtract and multiply in integers but for the division, one of the two arguments has to be float to get a float result. Try this:
float current = (516 - rawcurr) * 45 / 1023.0;
1 Like
A simpler way to display a floating point value with three decimal places is to specify the number of decimal places in .print().
void setup() {
Serial.begin(9600);
}
void loop() {
int rawcurr = analogRead(A0);
float current = (516 - rawcurr) * 45 / 1023.0;
Serial.print(current, 3);
Serial.print(" , ");
Serial.println(rawcurr);
delay(500);
}
2 Likes
Johnwasser, you made my day!! Thank you, and I add this one to my ever increasing list of learning points! 
1 Like
johnwasser:
A simpler way to display a floating point value with three decimal places is to specify the number of decimal places in .print().
void setup() {
Serial.begin(9600);
}
void loop() {
int rawcurr = analogRead(A0);
float current = (516 - rawcurr) * 45 / 1023.0;
Serial.print(current, 3);
Serial.print(" , ");
Serial.println(rawcurr);
delay(500);
}
For some strange reason
Serial.println(current, 3);
returns all 0.000 (I use linebreak here, it is the last to be printed on the line). No float number. What did I do wrong?
brice3010:
For some strange reason
Serial.println(current, 3);
returns all 0.000 (I use linebreak here, it is the last to be printed on the line). No float number. What did I do wrong?
Sounds like a mistake in your sketch or you analog input is always 516. Show the sketch exactly as you are using it. Copy and paste the serial output, exactly as it appears to you.
/*
Measuring Current Using ACS712
*/
void setup(){
Serial.begin(9600);
}
void loop(){
float current;
float maxCurr;
float minCurr;
static char disp[7];
int rawcurr = analogRead(0);
for( int i=0; i<10; i++)
{
float current = current + (516 - rawcurr) * 45/1023.0;
current = current / 10.0;
dtostrf(current, 5,3,disp);
maxCurr = max(maxCurr, current);
minCurr = min(minCurr, current);
delay(50);
}
float noise = maxCurr - minCurr;
delay(10);
//Serial.print( current);
//Serial.print((float)(current),2);
//Serial.print(" , ");
Serial.print(disp);
Serial.print(" , ");
Serial.print(noise);
Serial.print(" , ");
Serial.print(current, 3);
Serial.print(" , ");
Serial.println(rawcurr);
}
At 0.5A load, "disp" shows around 0.100, "noise" around 0.1, "current, 3" shows zero, and "rawcurr" shows around 500.
The 'current' that you print is not the same 'current' that you calculate. The calculated 'current' is a local variable, only in scope inside the 'for' loop.
The analogRead() needs to be inside the "for" loop if you want to average 10 different values. The division by 10 needs to be done after the addition of the 10 values.
OldSteve, cattledog, thank you! You cannot image how much I appreciate your input, johnwasser: you too. This is another one that I add to my list of learning points. Obviously I need to take more care in logical programming structure.
OldSteve:
The 'current' that you print is not the same 'current' that you calculate. The calculated 'current' is a local variable, only in scope inside the 'for' loop.
As soon as I removed the float declaration on current inside the loop this was solved; if I understand correctly the fact that I declare current as float a second time -in the loop- means that it becomes a local variable, only in scope inside the "for" loop? I urgently need to read up on variable declarations.
To cattledog, if I understand it correctly, I read the analog value on 0 before starting the loop, so once in the loop I take 10 times the same value.. stupid of me; and thank you for pointing out.
so once in the loop I take 10 times the same value..
It's worse than that. Because of the location of your "for" loop brackets, you divide after each addition by 10, and the final result is approximately one tenth the magnitude it should be.
You can add all the number up and divide by 10 or divide each individual value by ten and then add, but don't keep adding a number to a previous sum divided by 10 and then divide by 10 again.
void setup() {
Serial.begin(115200);
float current = 0;
for(int i = 0; i<10; i++)
{
static int rawcurr = 500;
current = current + rawcurr;
current = current/10;
}
Serial.print(current); //result = 55.56 not 500
}
void loop() {}
To cattledog, thanks for pointing out; I had found this just before your posting but it still makes me feel silly 