I am quite new to Arduino and I am learning how to use and set up Libraries. I Have followed the great tutorial on https://www.arduino.cc/en/Hacking/LibraryTutorial but I am trying to figure something out.
I have created an Library to read an temperature sensor. But how can I use variables in the main program to do more things such as print to SD or add into a string outside of the .cpp file? In other words, how can I use the variabel "temperature" in the main program since it is declared in the .ccp file? Is it possible?
#ifndef TMP36_h
#define TMP36_h
#include "Arduino.h"
class TMP36
{
public:
TMP36(int pin); // Command to set temperature Apin for Analog read
void readTemp(); // Command to execute temperature read function
private:
int _pin; // Configure user selected TMP36 Apin and save into a private variable for use in various functions
};
#endif
.cpp
#include "Arduino.h"
#include "TMP36.h"
// Declare variables used in code
TMP36::TMP36(int pin)
{
_pin = pin; // Set TMP36(used in this prgramme) as Apin set by user
}
void TMP36::readTemp() //Function to read temp
{
int tempreadsum = 0; // Integer values from analogRead(TMP36pin)
float TMP36pinAverage = 0; // Decimal values used to calculate averages from analogRead(TMP36pin)
float temperature = 0; // Decimal value for temperature reading [C]
for (int i = 0; i <= 4; i++) { // 5 readings per called readTemp() function
tempreadsum = tempreadsum + analogRead(_pin); //Adding integer readings from 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 for TMP36 sensor
Serial.println("Temperature");
Serial.println(temperature);
}
Temperature is local to readTemp(). If you want to use it outside that function, you need to make it global and call it either like this TMP36::temperature or make another function that returns its value.
Using variables from a library is not best practice. Instead use functions:
in your .h change the line:
void readTemp(); // Command to execute temperature read function
to:
float readTemp(); // Command to execute temperature read functionvoid readTemp(); // Command to execute temperature read function
and in .cpp change:
void TMP36::readTemp() //Function to read temp
{
int tempreadsum = 0; // Integer values from analogRead(TMP36pin)
float TMP36pinAverage = 0; // Decimal values used to calculate averages from analogRead(TMP36pin)
float temperature = 0; // Decimal value for temperature reading [C]
for (int i = 0; i <= 4; i++) { // 5 readings per called readTemp() function
tempreadsum = tempreadsum + analogRead(_pin); //Adding integer readings from 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 for TMP36 sensor
Serial.println("Temperature");
Serial.println(temperature);
}
to:
float TMP36::readTemp() //Function to read temp
{
int tempreadsum = 0; // Integer values from analogRead(TMP36pin)
float TMP36pinAverage = 0; // Decimal values used to calculate averages from analogRead(TMP36pin)
float temperature = 0; // Decimal value for temperature reading [C]
for (int i = 0; i <= 4; i++) { // 5 readings per called readTemp() function
tempreadsum = tempreadsum + analogRead(_pin); //Adding integer readings from 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 for TMP36 sensor
Serial.println("Temperature");
Serial.println(temperature);
return temperature;
}
Then you can do something like this in your main program: