Pages: [1]   Go Down
Author Topic: Error with mathematical calculation  (Read 1672 times)
0 Members and 1 Guest are viewing this topic.
Netherlands
Offline Offline
Sr. Member
****
Karma: 0
Posts: 414
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Code:
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:
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!
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!!)
Logged

Netherlands
Offline Offline
Sr. Member
****
Karma: 0
Posts: 414
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You are correct about that, I changed that after posting.

The Arduino environment does support this type of code:
Code:
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)?

« Last Edit: November 12, 2006, 11:07:06 am by jds » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Forum Administrator
Cambridge, MA
Offline Offline
Faraday Member
*****
Karma: 12
Posts: 3538
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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 "^".  
Logged

Netherlands
Offline Offline
Sr. Member
****
Karma: 0
Posts: 414
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.


Logged

Florida, USA
Offline Offline
Full Member
***
Karma: 0
Posts: 146
meow!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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));
}
Logged

Pages: [1]   Go Up
Jump to: