Arduino: Error with calculation

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

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:

val = ( 1 - ( val/ 1013.25 ) ^ 0.19) ) / 22.558;

The error is:

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!

The XOR operation isn’t really meaningful on floating-point numbers…

If what you actually intended with that caret was an exponentiation, that’s done in C via the pow(x,y) math library function.

check exponention operator - in C, it’s usually a function, not an operator as in BASIC or other languages. Usually called pow().

POW - power function.

(ANSI Standard)

Usage:

#include <math.h>

z = pow( x, y );

Where:

double x, y, z;

Description:

“pow” returns “x” to the power “y”. If “x” and “y” are both zero, or if “x” is non-positive and “y” is not an integer, “pow” return -HUGE_VAL and sets “errno” to EDOM. If the answer would cause an overflow, “pow” returns +HUGE_VAL.

See Also:

expl c lib errno


Also - your floats are probably single precision (24 bit mantissa) for this microprocessor. Type single and double are probably defined to be the same.


Also, you probably need to give

1.0

instead of

1

since the latter is an integer and this causes the compiler grief in mixed-mode math. Some compilers will automatically type-convert.

I’m pretty sure compilers are required to promote integer literals to floating point automatically, but maybe not all do. It’s still a good idea to make sure it’s parsed as floating point (you don’t actually need the trailing 0, I usually just write 1. for floating-point-1, but you can also write 1f for float and 1d for double at least with reasonably modern compilers.) Otherwise, once in a while you’ll accidentally write something where the compiler isn’t required to promote, and it’ll do an integer division and round off/truncate (oops).

Thanks guys, the pow function is what I needed indeed! Also mentioning the 1.0 instead of 1 was helpfull.

I am all new to this :oops:

I’m an old C guy. The compiler may optionally but is not required to do type conversions or promoting. So I never assume that it will. Same holds true for promoting char to int.

The Arduino environment turns out to be somewhat limited… I am completely new to the Arduino/Atmega stuff, so it may be just me.

  • variables defined as float are allowed (newest release), but take up a lot of space.

  • An expression that uses ^ (don’t know English word for it) is supported, but is really a bitwise XOR.

  • You can include math.h and get some extra functionality, like POW()… it seems to compile fine, but it costs a lot of extra memory.

In my opinion, it is useless to sell these boards with an Atmega8. I am using the Arduino with an pressure sensor. My target is to get a basic altimeter, but the little code I am currently using eats up all of the space in the Atmega8. It is possible to use an Atmega168, but you need some things to do before getting it to work. Information about this is available on the Arduino forums (http://www.arduino.cc → forums). A guy on the forums is offering Atmega168’s ready to use for the Arduino NG (US$ 15.00, incl. shipping).

I will send SF an email, suggesting them to move over to the Atmega168…

JD

I recall reading about Arduino a few weeks ago. What I remember (hopefully correctly), that Arduino is GCC for the AVR with an Arduino wrapper. If you can code in C, or want to learn more on doing so, for micros, skip Arduino and just get (freeware) WinAVR.

I use AtmanECL, a low cost IDE (wrapper) for GCC for the AVR. It allows me to create projects and develop C code without struggling with makefiles. There are several add-ons to GCC for the AVR besides AtmanECL, some are free.

IMO, the Arduino software tools try to abstract away the existence of a C compiler for their intended audience.

The CodeVison C compiler for AVRs is superb. It’s like $150 or so, but well worth it, in terms of ease of use, documentation, and a large library of ready-to-go I/O functions. An an active user group. There are other popular commerical C compilers.

If you’re a novice in micros, but experienced in Visual Basic 6, the AVR-based ZBasic.net module and free compiler are exemplary.

Thanks for all your suggestions. I will look into all of them and see what suits me best.

For now, I just ordered a Arduino NG ready Atmega168 to see if this helps me to get my software into this chip.

Otherwise, I think WinAVR would be my 2nd option, I think…

Thanks again!

Sadly the aduino environment takes up a huge amount of space on the controller. By the time you add user code you very quickly run out of space (and annoyingly it is usually ram, which of course shows up as a mysteriously non-working device…err…).

The 168 would help a little, but not much as it still only has 1k of ram.

I was pretty bummed when I ran into the ram limit a week or so after getting my first arduino. It reminded me of running into full memory segment problems back on the first C program I tried to make extensive changes to as a kid. At the time I didn’t know enough to recover from that.

However, programming the arduino in straight C using avr-gcc is pretty easy once you get over the initial speed bumps. The arduino bootloader is stk500 compatible, so you can easily use uisp or avrdude to load new code. Once you get one working makefile it is trivial to expand it to add more source files. AVRFreaks can lead you to all manner of code and libraries.

I don’t fault the arduino folks for their environment. I think it probably works very well in introduction courses. (it introduced me to microntrollers!) But it is resource heavy on a platform with very limited resources. Thankfully the hardware it uses is also totally usable outside the environment. And in fact easier to use than most other hardware due to the pre-installed bootloader.

So, ditch the arduino environment and use avr-gcc/winavr directly. You can even use atmel’s avrstudio (though i’ve never tried it since i lack a windows machine).

Oh! And also seriously look into doing your calculations without floating point! The moment you put a float in your program you instantly add about 2k to your code side.

Count the number of left and right parenthesis in this line of code.

val = ( 1 - ( val/ 1013.25 ) ^ 0.19) ) / 22.558;

There’s an extra right parenthesis… Do I win a prize?

The Arduino is elegant in it’s simplicity. Using some bigger (probably surface mount) avr by default wouldn’t help the people it’s aimed at.

Running into limitations can teach you to program better, if you take the time to understand things.