Pages: [1]   Go Down
Author Topic: calculating with analog input values  (Read 1488 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

I've tried to create a thermometer using the Arduino together with a temperature sensor.

However, for some reason I can get the arduino to send me the value read from the analog port, using analogRead() to store it in a value, and then using Serial.println() to print it in the serial monitor.

However the moment I start trying to calculate a value, it just sends a 0.

Here's the code I used;
Code:
// thermometer: gets voltage from a sensor connected to analog pin 5, calculates a temperature from this, and sends it to serial port every second

#define sensor 5

int val = 0;
float voltage = 0;
int temperature = 0;

void setup()
{
  Serial.begin(9600);    //open the serial port
}

void loop ()
{
  val = analogRead(sensor);
  Serial.println(val);
  voltage = ((val/1023)*5);
  Serial.println(voltage);
  temperature = ((voltage - 0.5) * 100);
  Serial.println(temperature);
  delay(1000);
}

The arduino returns; 142 (that's the analog read value)
0.00 (obviously I did something wrong with the calculation)
-50 ((0 - 0.5) *100 is -50 of course)

please help!
« Last Edit: September 15, 2010, 01:08:38 pm by letumeversor » Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17294
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I suspect your calculations are running into problems because of the use of float and int variables together in the same statements. I'll let the stronger software people explain it better then I can, but it's a common mistake for beginners to make.

Lefty
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks for the answer, however, the first time I tried this I calculated the temperature directly from the analogue value (temp = ((val / 1023 ) * 5 - 0.5 ) * 100), not really caring that I'd lose precision because I don't think the sensor's that precise.

I only found out it was in the calculations because I measured the voltage output of the sensor with a multimeter
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17294
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
(temp = ((val / 1023 ) * 5 - 0.5 ) * 100), not really caring that I'd lose precision because I don't think the sensor's that precise.

It's not just about precision loss but also truncation that can blow up calculations with gross math errors.

Lefty
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

well, I don't really know what truncation is, I'll have to look it up.

But in the 2nd calculation I posted I calculate with a integer, and store into an integer, and it gave the same error, so I don't think the problem is in the float.

I thought that if you go from a float to an integer it cuts out all numbers right of the . ?
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 208
Posts: 12932
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
 voltage = ((val/1023)*5);
"val / 1023" is an integer division which means any fractional part is truncated.  val is in the range 0 to 1023. So...

     0 / 1023 = 0
     1 / 1023 = 0  (remember, fractions dropped)
   142 / 1023 = 0
  1022 / 1023 = 0
  1023 / 1023 = 1


Try this...

Code:
 voltage = ((val/1023[glow].0[/glow])*5[glow].0[/glow]);
« Last Edit: September 15, 2010, 01:39:14 pm by bcook » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thank you Badly, it works now smiley

I thought it only truncated at the moment it had to store it in the integer.
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 208
Posts: 12932
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I thought it only truncated at the moment it had to store it in the integer.
C has two different sets of arithmetic operators: integer and floating-point.

By default, arithmetic is integer using the int datatype.  "5 * 37" is performed using integer arithmetic with int as the datatype.  Results less than -32768 or greater than +32767 are undefined.

  char C = 15;
  char X = 82;
  Serial.print( C * X );  // C and X are promoted to int and then an integer multiply is performed.

If an operand is a larger datatype, the operation is performed in that datatype...

  int F = 5;
  long TS = 37;
  Serial.print( F * TS );  // is performed using integer arithmetic with long as the datatype.  Results less than -2147483648 or greater than +2147483647 are undefined.

If an operand is a floating-point datatype...

  int F = 5;
  float PI = 3.1415;
  Serial.print( F * TS );  // is performed using floating-point arithmetic with float as the datatype

Adding ".0" forced to compiler to use floating-point arithmetic.
Logged

Pages: [1]   Go Up
Jump to: