Im having problems on why is my float result are Negative. Here is my code along with the screenshoot of the problem.
int mA = ACS.mA_AC(); //ACS712 Current
float Watt = (220*mA)/1000;
char daya[6];
dtostrf(Watt, 4, 2, daya);
sprintf(myStr, "ACS Current(mA)= %d. Watt Usage(W)= %s", mA, daya);
Serial.println("Sending to Antares... ");
Serial.println(myStr);
lora.sendUplink(myStr, strlen(myStr), mA);
Thank you in advance.
Integer arithmetic overflowing - try
panduh31:
(220.0*mA)/1000.0
1 Like
gcjr
July 5, 2022, 8:51am
3
are you sure your current isn't negative (swap the probes
ACS Current(mA)= 100. Watt Usage(W)= 22.00
ACS Current(mA)= -150. Watt Usage(W)= -33.00
void
pr (
float mA )
{
char myStr [80];
char daya [6];
float Watt = (220*mA)/1000;
dtostrf(Watt, 4, 2, daya);
sprintf(myStr, "ACS Current(mA)= %d. Watt Usage(W)= %s", int(mA), daya);
Serial.println (myStr);
}
void setup ()
{
Serial.begin (9600);
pr (100.);
pr (-150.);
}
void loop ()
{
}
The values are consistent with sixteen bit signed integer arithmetic overflow.
It works. Thank you so much!
1. Have you realized why did you get -25.00 in post #1 and why are you getting expected result using the following floating point arithmetic of Step-2:
2.
3. If you don't want fractional part of the result, you can use the following integer arithmetic:
int mA = 180;//ACS.mA_AC(); //ACS712 Current
unsigned long Watt = (220ul * mA) / 1000ul; //ul for unsigned long (32-bit)
Serial.println(Watt);
system
Closed
January 2, 2023, 5:20am
8
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.