hi
i am mainly taking resistance from a pot, feeding it into a 16bit adc and then using the value. the problem is my adc0 value which has the digital data for resistance from pot is showing correct values, but my variable rer(of type int) is giving me a value zero most of the times or else values which shouldnt be coming logically.pls pls advice
p.s. i have added the necessary headers as well <wire.h> and <Adafruit_ADS1015.h>
void setup(void)
{
Serial.begin(57600);
ads1115.begin();
}
void loop(void)
{
int16_t adc0;
int rer;
adc0 = ads1115.readADC_SingleEnded(0); //reading value of resistance from pot at port A0 of adc and converting it to digital
rer=adc*10000/26365; //performing calibration
@nick
according to me since precedence of division is higher, so it would do 10000/26365 first and thn multiply it with adc0. So i dont think that we have a problem there. Moreover it would have given an overflow error for the same.
the problem still persists
also i dont understand that since in arduino uno even int is signed and of 2 bytes which is same as int16_t, so this problem shouldn't occur at all.
It uses unsigned long registers to compute the result, so 1023 * 10000 will fit without rollover.
You could use
rer=adc*10000.0/26365.0; //performing calibration
to perform floating point arithmetic, instead, if the intended result is a float, rather than an int.
milano_new: @nick
according to me since precedence of division is higher, so it would do 10000/26365 first and thn multiply it with adc0. So i dont think that we have a problem there. Moreover it would have given an overflow error for the same.
Division and multiplication are the same precedence and left-associative, so therefore the multiplication is done first.
Yes but this is in the context of the original multiplication:
int16_t adc0;
int rer;
adc0 = ads1115.readADC_SingleEnded(0); //reading value of resistance from pot at port A0 of adc and converting it to digital
rer = adc0 * 10000 / 26365; //performing calibration
So this is the sequence:
Find adc0 as an int (same as int16_t). Say it is 800.
The above value is truncated to fit into an int. (80000000 = 0x7A1200, truncated to 16 bits = 0x1200, which is 4608)
Divide 4608 / 26365 giving 0.174777. Since this is an int this division gives 0.
So the issue is not so much that 10000 / 26365 is zero as an int (which it is) but that the previous multiplication did not fit into an int.
The earlier suggested solution of using longs instead of ints would work, because 80000000 fits into a long. Your other solution of using floats also works as they can handle fractional numbers.