Go Down

Topic: Error with mathematical calculation (Read 2045 times) previous topic - next topic

CrashingDutchman

I have to do this calculation which I do with the following source:


Code: [Select]

int analogPin = 0;   // Pressure sensor connected to analog pin 0
long val = 0;   // variable to store the read value

void setup()
{
  Serial.begin(9600);      // opens serial port, sets data rate to 9600 bps
}

void loop()
{

   val = analogRead(analogPin);   // read the input pin (getting value of around 840-860)
   
   val = ( 1 - ( val/ 1013.25 ) ^ 0.19) ) / 22.558;
   
   Serial.print("Value: ");
   Serial.println(val);
   
   delay(1000);

}



This line gives an error:
Code: [Select]
val = ( 1 - ( val/ 1013.25 ) ^ 0.19) ) / 22.558;

The error is:
Quote
In function 'void loop()':
error: invalid operands of types 'double' and 'double' to binary 'operator^


I hope someone can help to solve this. I am totally new to the Arduino.

Thank you in advance!

abbie77

humm, isn't your val variable declared as an integer? in te code you give it values like a float (with decimals). from what i read on the ard ref pages, arduino doesn't handle floats just char, int and long. anyone cares to comment? (and please help with my push button question!!)

CrashingDutchman

#2
Nov 12, 2006, 05:06 pm Last Edit: Nov 12, 2006, 05:07 pm by jds Reason: 1
You are correct about that, I changed that after posting.

The Arduino environment does support this type of code:
Code: [Select]
float var = 0;

But, it consumes a lot of extra memory... I am running already into the limits of the memory of the Atmega8.

Would it be really hard to use the Atmega168 on the Arduino board (not the mini)?


abbie77

The Arduino reference pages at http://www.arduino.cc/en/Reference/HomePage state:
Variables

Variables are expressions that you can use in programs to store values, like e.g. sensor reading from an analog pin. They can have various types, which are described below. Floating point variables and operations are not currently supported.

char
int
long
string
array

i know that the atmega168 has twice as much programming space as atmega8. the pin configuration is different. this web page has a datasheet on atmega168 -- http://www.arduino.cc/playground/Learning/ArduinoMini

best,
A

mellis

Damn.  It appears that the Arduino core is now small enough to support basic operations with floats.  I'll update the reference according.  Unfortunately, Arduino still doesn't support exponentiation, which I assume is what you're trying to do with the "^".  

CrashingDutchman

Quote

i know that the atmega168 has twice as much programming space as atmega8. the pin configuration is different. this web page has a datasheet on atmega168 -- http://www.arduino.cc/playground/Learning/ArduinoMini


I think it should be possible to use the Atmega168 in the 'standard' Arduino board, I read about it here:
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1159499314/

See posting #9 from user 'mellis' and the messages after that posting.



CosineKitty

Does Arduino support the functions exp (raise e to a power) and log (natural log)?  If so, you can raise A to the B power using the following formula:

float pow (float A, float B)
{
   return exp (B * log(A));
}

Go Up