help writing library

Hi,

I am working on my first libraries. Essentially I would like to read an analog pin and get a pressure reading back via some functions. Here is what I have for the header file

#ifndef CONVECTRON_h
#define CONVECTRON_h

//#include "WProgram.h"
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif

class CONVECTRON
{
    public:
        CONVECTRON_ReadPressure(pressure);
    private:
        int _Voltage;
        float _pressure;
};


#endif

and the .cpp file

//#include <WProgram.h>
  #if defined(ARDUINO) && ARDUINO >= 100
  #include "Arduino.h"
  #else
  #include "WProgram.h"
  #endif

#include "CONVECTRON.h"

CONVECTRON::CONVECTRON_ReadPressure();
{
    int senorValue = analogRead(A0);
    float voltage = sensorValue * (5.0 / 1023.0);
        
        if (voltage<=2.842) //0.375-2.842V
            pressure=(-0.02585+(0.03767*voltage)+(0.04563*voltage*voltage)+(0.1151*pow(voltage, 3))-(0.04158*pow(voltage, 4))+(0.008737*pow(voltage, 5)));{
            {voltage<=4.945; //2.842-4.945V
                pressure=((0.1031-(0.02322*voltage)+(0.07229*voltage*voltage))/(1-(0.3986*voltage)+(0.07438*voltage*voltage)-(0.006866*pow(voltage, 3))));}
            {voltage<=5.659;   //4.945-5.659V
                pressure=((100.624-(20.5623*voltage))/(1-(0.37679*voltage)+(0.0348656*voltage*voltage)));}}
    return pressure;
}

Is this correct? Would just like some help with this. Thanks!

You aren't using the private member variables _Voltage or _pressure so there is not real need to have an object:

#ifndef CONVECTRON_h
#define CONVECTRON_h

//#include "WProgram.h"
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif

float CONVECTRONReadPressure(const int pin);
#endif

and the .cpp file

//#include <WProgram.h>
  #if defined(ARDUINO) && ARDUINO >= 100
  #include "Arduino.h"
  #else
  #include "WProgram.h"
  #endif

#include "CONVECTRON.h"

float CONVECTRONReadPressure(const int analog_pin = A0);
{
    int senorValue = analogRead(analog_pin);
    float voltage = sensorValue * (5.0 / 1023.0);
    float pressure;
        
    if (voltage<=2.842) //0.375-2.842V
        pressure=(-0.02585+(0.03767*voltage)+(0.04563*voltage*voltage)+(0.1151*pow(voltage, 3))-(0.04158*pow(voltage, 4))+(0.008737*pow(voltage, 5)));
    {
        {
            voltage<=4.945; //2.842-4.945V
             pressure=((0.1031-(0.02322*voltage)+(0.07229*voltage*voltage))/(1-(0.3986*voltage)+(0.07438*voltage*voltage)-(0.006866*pow(voltage, 3))));
         }
         {
             voltage<=5.659;   //4.945-5.659V
             pressure=((100.624-(20.5623*voltage))/(1-(0.37679*voltage)+(0.0348656*voltage*voltage)));
        }
    }
    return pressure;
}

Your code looks a little wonky. You have conditional statements all by themselves like "voltage<=5.659;". That won't do anything except calculate a true/false value and then throw it away. You probably intended to use else and if statements.

Also note that the highest voltage you can measure is 4.995V (5V minus one LSB or 1/1024th). You are comparing the voltage to 5.695 and it will always be less than that. if you need to measure higher voltages you need a voltage divider to bring the desired range down to fit within the hardware limits.

johnwasser:
Your code looks a little wonky. You have conditional statements all by themselves like "voltage<=5.659;". That won't do anything except calculate a true/false value and then throw it away. You probably intended to use else and if statements.

Also note that the highest voltage you can measure is 4.995V (5V minus one LSB or 1/1024th). You are comparing the voltage to 5.695 and it will always be less than that. if you need to measure higher voltages you need a voltage divider to bring the desired range down to fit within the hardware limits.

How would I change to else and if statements?

I realize that the Arduino can only read to 5V. I was just going to write the library with the equations given and then change it when I knew what resistors I would be using in my voltage divider.

Thanks

Like this:

if (voltage<=2.842) //0.375-2.842V
{
    pressure=(-0.02585+(0.03767*voltage)+(0.04563*voltage*voltage)+(0.1151*pow(voltage, 3))-(0.04158*pow(voltage, 4))+(0.008737*pow(voltage, 5)));
}
else if (voltage<=4.945) //2.842-4.945V
{
    pressure=((0.1031-(0.02322*voltage)+(0.07229*voltage*voltage))/(1-(0.3986*voltage)+(0.07438*voltage*voltage)-(0.006866*pow(voltage, 3))));
}
else if (voltage<=5.659) //4.945-5.659V
{
    pressure=((100.624-(20.5623*voltage))/(1-(0.37679*voltage)+(0.0348656*voltage*voltage)));
}

guix:
Like this:

if (voltage<=2.842) //0.375-2.842V

{
    pressure=(-0.02585+(0.03767voltage)+(0.04563voltagevoltage)+(0.1151pow(voltage, 3))-(0.04158pow(voltage, 4))+(0.008737pow(voltage, 5)));
}
else if (voltage<=4.945) //2.842-4.945V
{
    pressure=((0.1031-(0.02322voltage)+(0.07229voltagevoltage))/(1-(0.3986voltage)+(0.07438voltagevoltage)-(0.006866pow(voltage, 3))));
}
else if (voltage<=5.659) //4.945-5.659V
{
    pressure=((100.624-(20.5623
voltage))/(1-(0.37679voltage)+(0.0348656voltage*voltage)));
}

Thank you.

Now I am getting expected unqualified-id error.

johnwasser:
You aren't using the private member variables _Voltage or _pressure so there is not real need to have an object:

#ifndef CONVECTRON_h

#define CONVECTRON_h

//#include "WProgram.h"
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif

float CONVECTRONReadPressure(const int pin);
#endif

Wouldn't it still be necessary to define public and private variables?

ichris93:

johnwasser:
You aren't using the private member variables _Voltage or _pressure

Wouldn't it still be necessary to define public and private variables?

You only need to define public and private variables (and functions) that you actually use.

johnwasser:
You only need to define public and private variables (and functions) that you actually use.

Am I not using any variables for this?

If you only need to take some parameters, do some calculation with them and return the result, you don't need a class at all. That's stateless code, i.e. a "simple" function.

Let's compare two simple (ok, stupid) examples:

No state information between calls:

int sum(int a, int b) { return a + b; }

Each "Counter" object has its own state, that is its own value:

class Counter {
public:
    Counter() { _value = 0; };
    void inc() { _value++; };
    void dec() { if (_value > 0) { _value--; } };
    int getValue() { return _value; };
private:
    int _value;
};

Okay. But if I wanted to get it working with a library, how would I go about doing that? It should be pretty simple, right? However, I do need some help with it.

Thanks!

ichris93:
Okay. But if I wanted to get it working with a library, how would I go about doing that? It should be pretty simple, right? However, I do need some help with it.

Thanks!

Which I translate to: I either didn't read your code or I didn't understand a single bit of it... could you convert my code into a library ?

tuxduino:

ichris93:
Okay. But if I wanted to get it working with a library, how would I go about doing that? It should be pretty simple, right? However, I do need some help with it.

Thanks!

Which I translate to: I either didn't read your code or I didn't understand a single bit of it... could you convert my code into a library ?

It doesn't translate into that exactly. I did read you code and thought I understood the basics of it. To confer it into a library wouldn't you just make the second code your .h file and then create you .cpp file?

Ok, sorry.

An Arduino library is usually split in a .h file (declaration) and a .cpp file (implementation), but that's not a strict requirement.
It depends on how the library code is written.

In essence a library is just a bag of reusable code.

That is, a group of related functions or one or more classes.

If by "second code" you mean this:

class Counter {
public:
    Counter() { _value = 0; };
    void inc() { _value++; };
    void dec() { if (_value > 0) { _value--; } };
    int getValue() { return _value; };
private:
    int _value;
};

this can go into a .h file and no .cpp file is needed, since both declaration and implementation are specified.

Have a look here

http://www.cplusplus.com/doc/tutorial/

under "object oriented programming"

Also, look up the tutorial on this very site about how to write an Arduino lib.

tuxduino:
Ok, sorry.

An Arduino library is usually split in a .h file (declaration) and a .cpp file (implementation), but that's not a strict requirement.
It depends on how the library code is written.

In essence a library is just a bag of reusable code.

That is, a group of related functions or one or more classes.

If by "second code" you mean this:

class Counter {

public:
    Counter() { _value = 0; };
    void inc() { _value++; };
    void dec() { if (_value > 0) { _value--; } };
    int getValue() { return _value; };
private:
    int _value;
};



this can go into a .h file and no .cpp file is needed, since both declaration and implementation are specified.

Have a look here

http://www.cplusplus.com/doc/tutorial/

under "object oriented programming"

Also, look up the tutorial on this very site about how to write an Arduino lib.

I didn't know that you could do it without a .cpp file. Thank you for your help. The Arduino library tutorial (I admittedly don't know how I missed) was helpful in writing another library I was working on. Hopefully now, I will be able to get this one working with a little work.

Hey guys. I got the library working. Thank you for your help. Now I have a new question :relaxed:. How do I return a number in scientific notation with the exponents instead of in float form?

How do I return a number in scientific notation with the exponents instead of in float form?

You don't. That's a method of presenting the data, not a way of storing/passing it.

PaulS:

How do I return a number in scientific notation with the exponents instead of in float form?

You don't. That's a method of presenting the data, not a way of storing/passing it.

Okay then lets say my Arduino code was

lcd.print(sensor.readpressure());

How do I print it in exponential form?

How do I print it in exponential form?

The Print class, which a lot of classes derive from, does not have a method to do that. The dtostre() function can be used to create a string, though, that the Print class can print.

PaulS:
The Print class, which a lot of classes derive from, does not have a method to do that. The dtostre() function can be used to create a string, though, that the Print class can print.

Great thank you! I have this now

char buffer[10];
dtostrf(sensor.readpressure(), 10, 2, buffer);
  lcd.print(buffer);

But it doesn't seem to be adding the exponential notation.

post what you are getting, if you make buffer large enough you can manipulate extra text in there.