Dear Fellow Arduinists,
Apologies now for this question, I get the feeling I should be able to solve this having looked at many posts on the same subject but I still can't get the sketch to compile.
I'm using an environmental sensor (BMP280) to record pressure and temp data to an SD card, there are also some flashy LED's just because I can. There is a relay controlling a heater circuit which switches things on at a given external temperature. The package will fly on a balloon payload. All this works well.
I then wanted to add a second temperature sensor (TMP36) to record the internal temp of the payload and write that to the SD card, I also thought it would be better to use this sensor to trigger the heater relay. I got the sensor working on it's own and merrily reporting data in the serial monitor.
The problem came when I tried to incorporate the code for the TMP36 into my working sketch. I've tried to track down all of the braces to ensure I have a balanced code but I still get the error message 'getVoltage was not declared in this scope'.
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>
#include <SD.h>
File myFile;
Adafruit_BMP280 bmp; // I2C
/* Temperature sensor pins connnection
* Vin --> 5V Arduino ---Red
* Gnd --> Gnd Arduino ---Black
* SCK --> A5 Arduino ----Yellow
* SDI --> A4 Arduino ----Orange
*/
/* MicroSD Board pins connnection
Connect the 5V pin to the 5V pin on the Arduino
Connect the GND pin to the GND pin on the Arduino
Connect CLK to pin 13
Connect DO to pin 12
Connect DI to pin 11
Connect CS to pin 10
*/
//Test Version!
float pressure; //BMP280 Variable
float temperature; //BMP280 Variable
int altimeter; //BMP280 Variable
const int temperaturePin = 3; //TMP36 Pin Set
unsigned long startMillis; //Timing variables for LEDS
unsigned long currentMillis; //Timing Variables for LEDS
const unsigned long period = 5000; //the value is a number of milliseconds, LED delay.
void setup() {
pinMode(8, OUTPUT); //Power to Red LED
pinMode(7, OUTPUT); //Power to Green LED
pinMode(6, OUTPUT); //Power to Heat Pad
bmp.begin();
Serial.begin(9600); //Begin serial communication at 9600bps
Serial.println("Adafruit BMP280 Wire Test");
if (!bmp.begin()) {
Serial.println(F("Could not find a valid BMP280 sensor, check wiring!"));
while (1);
startMillis = millis(); //initial start time
}
Serial.print("Initializing SD card...");
pinMode(10, OUTPUT);
if (!SD.begin(10)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
}
void loop() {
currentMillis = millis(); //get the current "time" (actually the number of milliseconds since the program started)
if (currentMillis>10000 && currentMillis - startMillis >= period) //test whether the period has elapsed for LEDS to light.
{
digitalWrite(8, HIGH);
delay(40);
digitalWrite(8, LOW);
delay(50);
digitalWrite(8, HIGH);
delay(40);
digitalWrite(8, LOW);
delay(50);
digitalWrite(8, HIGH);
delay(40);
digitalWrite(8, LOW);
delay(50);
digitalWrite(8, HIGH);
delay(40);
digitalWrite(8, LOW);
delay(50);
digitalWrite(8, HIGH);
delay(40);
digitalWrite(8, LOW);
delay(50);
digitalWrite(8, HIGH);
delay(40);
digitalWrite(8, LOW);
delay(50);
digitalWrite(8, HIGH);
delay(40);
digitalWrite(8, LOW);
delay(50);
digitalWrite(8, HIGH);
delay(40);
digitalWrite(8, LOW);
delay(50);
delay(250);
digitalWrite(7, HIGH);
delay(40);
digitalWrite(7, LOW);
delay(50);
digitalWrite(7, HIGH);
delay(40);
digitalWrite(7, LOW);
delay(50);
digitalWrite(7, HIGH);
delay(40);
digitalWrite(7, LOW);
delay(50);
digitalWrite(7, HIGH);
delay(40);
digitalWrite(7, LOW);
delay(50);
digitalWrite(7, HIGH);
delay(40);
digitalWrite(7, LOW);
delay(50);
digitalWrite(7, HIGH);
delay(40);
digitalWrite(7, LOW);
delay(50);
digitalWrite(7, HIGH);
delay(40);
digitalWrite(7, LOW);
delay(50);
digitalWrite(7, HIGH);
delay(40);
digitalWrite(7, LOW);
delay(50);
startMillis = currentMillis; //IMPORTANT to save the start time of the current LED state.
}
//Read values from the BMP280 sensor:
pressure = bmp.readPressure();
temperature = bmp.readTemperature();
altimeter = bmp.readAltitude (1002.1195); //1 inch mercury = 33.8639 millibars. Change the "1050.35" to your city current barrometric pressure (https://www.wunderground.com)
if(temperature>18 ) { //temperature required to switch heat pad on/off
digitalWrite(6, HIGH);
} else {
digitalWrite(6, LOW);
}
//TMP36
{
float voltage, degreesC, degreesF;
voltage = getVoltage(temperaturePin);
degreesC = (voltage - 0.5) * 100.0;
degreesF = degreesC * (9.0/5.0) + 32.0;
Serial.print(voltage);
Serial.print(degreesC);
Serial.print("Time: ");
Serial.print(currentMillis);
Serial.println((" milliseconds"));
Serial.print(F("Pressure: "));
Serial.print(pressure);
Serial.print(" Pa");
Serial.print("\t");
Serial.print(("Temp: "));
Serial.print(temperature);
Serial.print(" oC");
Serial.print("\t");
Serial.print("Altimeter: ");
Serial.print(altimeter); // this should be adjusted to your local pressure forecast
Serial.println(" m");
delay(500); // 0.5 sec delay (500ms)
float getVoltage(int pin) //TMP36
{
return (analogRead(pin) * 0.004882814);
}
myFile = SD.open("hab.txt", FILE_WRITE);
if (myFile) {
Serial.print("Writing to test.txt...");
//myFile.println("New Data"); //Removed to save file space.
//myFile.print("Pressure: "); //Removed to save file space.
myFile.print(currentMillis);
myFile.print(",");
myFile.print(pressure);
myFile.print(",");
//myFile.print(" Temperature: "); //Removed to save file space.
myFile.print(temperature);
myFile.print(",");
//myFile.print(" Altimeter: "); //Removed to save file space.
myFile.println(altimeter);
//myFile.println(" m"); //Removed to save file space.
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
}
Thank you in advance, please let me know if you need any further information. I have attached the INO file of my sketch, hope this is the right way to do things.
Best wishes
Phil
Relay_Millis_TMP36.ino (5.6 KB)