Some help with Library


hey,

i quite new arduino , learning how use , set libraries. have followed great tutorial on https://www.arduino.cc/en/hacking/librarytutorial trying figure out.

i have created library read temperature sensor. how can use variables in main program more things such print sd or add string outside of .cpp file? in other words, how can use variabel "temperature" in main program since declared in .ccp file? possible?

any advice great=)


main program

code: [select]

#include <tmp36.h>

tmp36 tmp36(5);

void setup()
{
  serial.begin(9600);
}

void loop()
{

  tmp36.readtemp();
  delay(3000);
}


.h
code: [select]

#ifndef tmp36_h
#define tmp36_h

#include "arduino.h"

class tmp36
{
  public:
    tmp36(int pin); // command set temperature apin analog read
    void readtemp(); // command execute temperature read function
  private:
    int _pin; // configure user selected tmp36 apin , save private variable use in various functions
};

#endif


.cpp
code: [select]

#include "arduino.h"
#include "tmp36.h"

// declare variables used in code



tmp36::tmp36(int pin)
{
_pin = pin; // set tmp36(used in prgramme) apin set user
}


void tmp36::readtemp() //function read temp
{

int tempreadsum = 0; // integer values analogread(tmp36pin)
float tmp36pinaverage = 0; // decimal values used calculate averages analogread(tmp36pin)
float temperature = 0; // decimal value temperature reading [c]

for (int = 0; <= 4; i++) { // 5 readings per called readtemp() function

tempreadsum = tempreadsum + analogread(_pin); //adding integer readings tpm36pin

}

tmp36pinaverage = tempreadsum / 5.0; // average of 5 readings
tmp36pinaverage = tmp36pinaverage * 5.0 * 1000.0 / 1024.0; // calculate output voltage [mv]
temperature = ((tmp36pinaverage / 1000.0 - 0.5) / 0.01); // calculate temp, see datasheet tmp36 sensor

serial.println("temperature");
serial.println(temperature);

}

temperature local readtemp(). if want use outside function, need make global , call either this tmp36::temperature or make function returns value.


Arduino Forum > Using Arduino > Programming Questions > Some help with Library


arduino

Comments