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
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.
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.
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 ?
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?
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.
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.
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 . How do I return a number in scientific notation with the exponents instead of in float 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.