Library programing issue

Hi.

I’m about to programing a library for a RTD temp sensor.

I have done a sketch that working an now I want to implant it into a library. I’ve read a lot of exemple a many syntax, and non of them seem to work.

I’m very newbie on programing, I don’t understand haft of what I do. but my need are very simple. the library compile correctely but
I,m not able to make it do in a sketch. all I want to understand is the correct syntax to take a reading of an int, do my void library, and set an int of the output value.

this is my header and cpp file and an exemple of what I’ve try so far.

thank.

#ifndef RTD10kB3950_h
#define RTD10kB3950_h

#include “Arduino.h”

class RTD
{
public:
RTD(int Rt);
RTD(double temp);
void RTDraw();
private:
int _Rt;
double _temp;

};

#endif

#include “Arduino.h”
#include “RTD10kB3950.h”

RTD::RTD(int Rt){
//Rt=0;
_Rt = Rt;
}
RTD::RTD(double temp) {
temp = _temp;

}
int linearray = 0;

double RTDarray={
200, 149.78,
250, 139.91,
300, 132.19,
350, 125.88,
400, 120.58,
450, 116.01,
500, 112.02,
//… array is very long, I resume it
144650, -24.99,
144700, -25,
1000000, -50,
};

void RTD::RTDraw() {

while( _Rt > linearray[RTDarray]){
linearray++;
}
if (_Rt <= linearray[RTDarray]){
linearray++;
if ( (linearray[RTDarray] - _Rt) <= ((linearray-2[RTDarray])-_Rt)) {
_temp = linearray[RTDarray];
}
else {_temp = linearray-2[RTDarray];
}
linearray=0;
}
}

#include <RTD10kB3950.h>

int input = 10000;
double output = 0;
double temp;

void setup() {
Serial.begin(9600);
analogReadResolution(12);

}

void loop() {
RTD(Rt) = input;
RTD RTDraw();
output = temp;

Serial.println(output);

delay (1000);
}

How big is the RTDarray[] array? Because the Arduino doesn't really support the double data type, can you make it a float? Some boards in the Arduino family have pretty limited SRAM and that might be an issue.

Also, please use code tags for your code listings. The two "stickies" at the top of this Forum list by Nick Gammon explains the format that will ensure you get the most readers and responses.

Change:

RTD::RTD(double temp) {
temp = _temp;

}

to

RTD::RTD(double temp) {
_temp = temp;

}

Is this code for the Due? I can't remember if doubles are true doubles on the Due,
but they are simply floats on other boards.

@econjack ;My array is big but not to much. take a lot of memory but still work in a sketch. and I would try to find how to format my post next time… :P. thanks

@MarkT Yes I could have use float instead. double and float seem to be the same. and it is Due programing.
I’ve have already try your solution and it still don’t work.
My guest is that is maybe not the library itself that is worng more that I dont know well how to use it in a sketch.

like this:

void loop() {
RTD(Rt) = input;
RTD RTDraw();
output = temp;

Serial.println(output);

delay (1000);
}

thanks

RTD RTDraw();

What do you think this is doing? It looks like a function prototype, for a function called RTDraw that returns an instance of the class RTD. Pretty useless to have a function prototype in loop().

And this:

RTD(Rt) = input;

Functions can never be lvalues.

exactly what I do.

here’s my header in my library:

#ifndef RTD10kB3950_h
#define RTD10kB3950_h

#include "Arduino.h"

class RTD
{
  public:
    RTD(int Rt);
    RTD(double temp);
    void RTDraw();
  private:
    int _Rt;
    double _temp;

};

#endif

Rt in the interger for the resistance in ohm I read from my analog input , RTDraw is my fonction to compare that value in an array to a tempeture equivalent; temp. ( this fonction already have been tested in a sketch ). I know I shoul use float instead of double variable but seem to work anyway the same.

here the cpp file for the library;

#include "Arduino.h"
#include "RTD10kB3950.h"

RTD::RTD(int Rt){
//Rt=0;
_Rt = Rt;
}
RTD::RTD(double temp) {
_temp = temp;

}
int linearray = 0;

double RTDarray[]={
    200, 149.78,
    250, 139.91,
    300, 132.19,
    350, 125.88,
    400, 120.58,
    450, 116.01,
    500, 112.02,
    550, 108.47,
// list continue
    1000000, -50,
};




void RTD::RTDraw() {

  while(  _Rt > linearray[RTDarray]){
    linearray++;
  }
  if (_Rt  <= linearray[RTDarray]){
    linearray++;
if ( (linearray[RTDarray] - _Rt) <= ((linearray-2[RTDarray])-_Rt)) {
  _temp = linearray[RTDarray];
 }
else {_temp = linearray-2[RTDarray];
}
linearray=0;
}
}

I think I got the good code for the library. it is in the skecht, like the previous post, I think there something worng .

here's my header in my library:

Which says that RTD is a class with two constructors and one method (RTDraw).

So, there is still no explanation of this code:

RTD RTDraw();

It is not calling the RTDraw method, since there is no instance in front of RTDraw, and RTDraw does not return a RTD.

So, what DO you think it is doing?

I think (make that KNOW) that it is declaring a function (not a method) called RTDraw() that returns an instance of the RTD class. Clearly that is a useless thing to do, because the RTDraw() function is never actually implemented.

Nor did you explain this:

RTD(Rt) = input;

RTD(Rt) would be creating an instance of the RTD class, with Rt (assuming it is defined somewhere) as input. You can't then assign the value of input to that instance.

yes I konw many thing is wrong on the sketch. from what I read from other exemple and forum, the syntax for calling the Methode,
should be

RTD::TRDraw();

but the arduino DUE compiler does't accept it.
in the same way, I dont find the corect wayt to set the integer value inside the library and write the output into another integer.

Don't forget, I'm realy not a programmer and most of the real way to call thing for me is chinise.. I apologise...

thank

ok i’ve got it all wrong. all the exemple I’ve read a to simple or to complex to understand to me.

so, i’ve try some litle change on the library, for what I think I understood so far.

it doesnt work, and fo that part I dont know why.

here the header:

#ifndef RTD10kB3950_h
#define RTD10kB3950_h

#include "Arduino.h"

class RTD
{
  public:
   // RTD(int Rt);
    float temp;
    void RTDraw(int Rt);
  private:
    int _Rt;
    float _temp;

};

#endif

If I understand, here are declare the class, the fonction and the variable.

the cpp file:

#include "Arduino.h"
#include "RTD10kB3950.h"

RTD::RTDraw(int Rt){
//Rt=0;
_Rt = Rt;
_temp = temp;

}

int linearray = 0;

double RTDarray[]={
    200, 149.78,
    250, 139.91,
    300, 132.19,
    350, 125.88,
    400, 120.58,
    450, 116.01,
      144500, -24.97,
    144550, -24.98,
    144600, -24.98,
    144650, -24.99,
    144700, -25,
    1000000, -50,
};




void RTD::RTDraw(int Rt) {

  while(  _Rt > linearray[RTDarray]){
    linearray++;
  }
  if (_Rt  <= linearray[RTDarray]){
    linearray++;
if ( (linearray[RTDarray] - _Rt) <= ((linearray-2[RTDarray])-_Rt)) {
  _temp = linearray[RTDarray];
 }
else {_temp = linearray-2[RTDarray];
}
linearray=0;
}
}

I use that sketch to test it;

#include <RTD10kB3950.h>

int input = 10000;
double output = 0;
double temp;

void setup() {
Serial.begin(9600);
analogReadResolution(12);

}

void loop() {
RTD.RTDraw(input);
output = temp;

Serial.println(output);

delay (1000);
}

and get the error;

sketch_sep16a.ino: In function 'void loop()':
sketch_sep16a:14: error: expected unqualified-id before '.' token

I think I need some explication on the good way to fix it. for now I’m a little stuck of what to try… :stuck_out_tongue:

thank anyway.

from what I read from other exemple and forum, the syntax for calling the Methode,
should be

That is not even close to correct.

A method, like RTDraw(), needs to be called on an instance of the class. You have NOT created an instance of the class. Your sketch should look like:

#include <RTD10kB3950.h>

int input = 10000;
double output = 0;
double temp;

RTD useless; // Create an instance of the class

void setup()
{ // Down here where it belongs
   Serial.begin(9600); // Properly indented
   analogReadResolution(12);
}

void loop()
{
   useless.RTDraw(input);
   output = temp; // Assigning 0 to output seems pointless

   // Perhaps you meant
   output = useless.temp;
   // Of course that won't do anything, since you never
   // assign a value to temp in the RTDraw method
   // You DO assign a value to _temp, but that is private
   // and can not be accessed by this sketch

   Serial.println(output);

   delay (1000);
}

Some additional notes. If the library file names are RTD10kB3950.h and RTD10kB3950.cpp, the class name should be RTD10kB3950. If the class name is RTD, the file names should be RTD.h and RTD.cpp.

Public variables are a bad idea. Variables should be private, and access methods (getters and setters) should be public.

Ok now it’s begin to be more clear. I’ve try and now that’s what I get;

#include <RTD10kB3950.h>

int input = 10000;
double output = 0;
double temp;

RTD useless(input);

/*
herre I get error;

sketch_sep16a:7: error: no matching function for call to 'RTD::RTD(int&)'
C:\Users\Nitrof\Documents\Arduino\libraries\RTD10kB3950/RTD10kB3950.h:18: note: candidates are: RTD::RTD()
C:\Users\Nitrof\Documents\Arduino\libraries\RTD10kB3950/RTD10kB3950.h:18: note:                 RTD::RTD(const RTD&)
*/

void setup() {
Serial.begin(9600);
analogReadResolution(12);

}

void loop() {
useless.RTDraw(input);
output = temp;

Serial.println(output);

delay (1000);
}

and for the variable , I done this;

RTD::RTDraw(int Rt){
//Rt=0;
_Rt = Rt;
temp = _temp; // instead of _temp = temp;


//and at the end of the loop:

if ( (linearray[RTDarray] - _Rt) <= ((linearray-2[RTDarray])-_Rt)) {
  _temp = linearray[RTDarray];
 }
else {_temp = linearray-2[RTDarray];
}
linearray=0;
temp = _temp;

Is that the good way ? or should I just don’t use _temp? for what I understand it is better cause it is a private variable…

thank

RTD useless(input);

Why? You removed the constructor that took an argument. So, the compiler generated one for you that didn't take any arguments. You can't provide one just because you think one is needed.

and for the variable , I done this;

No. Having a public and private variable with the same value makes no sense.

Provide a method, getTemp() (although temp is a stupid name) that returns the value in _temp. That is an accessor method (a getter). Get rid of the public variable called temp.

oups just saw my mistake.

RDT useless; // I remove the (input)

but now I get error:

C:\Users\Nitrof\Documents\Arduino\libraries\RTD10kB3950\RTD10kB3950.cpp:17: error: ISO C++ forbids declaration of 'RTDraw' with no type
C:\Users\Nitrof\Documents\Arduino\libraries\RTD10kB3950\RTD10kB3950.cpp:17: error: prototype for 'int RTD::RTDraw(int)' does not match any in class 'RTD'
C:\Users\Nitrof\Documents\Arduino\libraries\RTD10kB3950\/RTD10kB3950.h:22: error: candidate is: void RTD::RTDraw(int)

but now I get error:

Is there some part of that message that you don't understand?

Edit: The declaration in the header file says the function returns nothing. The implementation is missing a return type.

yes, I do not understand what will be the retur type in the header...

yes, I do not understand what will be the retur type in the header…

There is nothing wrong with the header.

void RTD::RTDraw(int Rt){

ok... not enough clear to me. if I add the void I get another error, double declare.

#include "Arduino.h"
#include "RTD10kB3950.h"

void RTD::RTDraw(int Rt){
//Rt=0;
_Rt = Rt;
_temp = temp;

}

int linearray = 0;

double RTDarray[]={
    200, 149.78,
    250, 139.91,
    300, 132.19,

void RTD::RTDraw(int Rt) {

C:\Users\Nitrof\Documents\Arduino\libraries\RTD10kB3950\RTD10kB3950.cpp:2924: error: redefinition of 'void RTD::RTDraw(int)'
C:\Users\Nitrof\Documents\Arduino\libraries\RTD10kB3950\RTD10kB3950.cpp:17: error: 'void RTD::RTDraw(int)' previously defined here

now.. it one or another ''void'' that is not correctly define in the cpp ? is't it ?

Off to http://snippets-r-us.com you go. I'm done trying to decipher your snippets. They are much more patient.

I sincerely apologize for all the trouble.

The good news is that, I made it work. With the cue that you gave me, I have been able to spot what were wrong.
I don't realy know what I do, I mostly copy exemple, so when it come more complicate I make mistake when adaptate it to my need...

Anyway this is only a thank message PaulS!
You heve been very patien and kindfull.

Im not a programmer. If you need something in electric desing It will be a plasure ! :stuck_out_tongue: I will probably pubilsh my sketch on day.

thank again.

Nitrof