Hi, I'm a student who is working on sending a helium balloon into the stratosphere. And this is my first time using a Arduino Uno. I was searching for code that is similar to what I'm doing: an adafruit data logger (P1141) connected to a MPL3115A2 sensor. So I found this link (https://forums.adafruit.com/viewtopic.php?f=22&t=61365&fbclid=IwAR1dFtCgCZWx23HZCLhpvtJcc6t5LSHkHHjsTqJHtJwk5bUCMGw7dHYB8Dg) But I am experiencing the same trouble as the person in the post. Especially the part where he changed his code to
String dataString = String(id) + "," + String (c) + "*C" + "," + String(f) + "*F";
And this is what I have, and it gives an error :'id' is not declared in this scope
/*
SD card datalogger
This example shows how to log data from three analog sensors
to an SD card using the SD library.
The circuit:
* SD card attached to SPI bus as follows:
** UNO: MOSI - pin 11, MISO - pin 12, CLK - pin 13, CS - pin 4 (CS pin can be changed)
and pin #10 (SS) must be an output
** Mega: MOSI - pin 51, MISO - pin 50, CLK - pin 52, CS - pin 4 (CS pin can be changed)
and pin #52 (SS) must be an output
** Leonardo: Connect to hardware SPI via the ICSP header
Pin 4 used here for consistency with other Arduino examples
a combination of sketches by adafruit used for SD card logger
and mcp9808 temp sensor
modified 9 Apr 2012 by Tom Igoe
This example code is in the public domain.
*/
#include <SPI.h> //SPI Library
#include <SD.h> //SD Library
#include <Wire.h> //I2C Library
#include "Adafruit_MPL3115A2.h"
// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
const int chipSelect = 10;
//I2C Temperature Pins
//SDA = Analog Pin 4
//SCL = Analog Pin 5
// Create the MPL3115A2 temperature sensor object
Adafruit_MPL3115A2 baro = Adafruit_MPL3115A2();
File dataFile;
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
Serial.println("MPL3115A data"); //Heading line
// Make sure the sensor is found, you can also pass in a different i2c
// address with tempsensor.begin(0x19) for example
if (!baro.begin()){
Serial.println("Couldn't find MPL3115A2!");
while (1);
}
Serial.println("Initializing SD card");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(10, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
while (1) ;
}
Serial.println("card initialized.");
// Open up the file we're going to log to!
dataFile = SD.open("datalog.txt", FILE_WRITE); //'tempsensor' was previously named 'datalog'
if (! dataFile) {
Serial.println("error opening datalog.txt");
// Wait forever since we cant write data
while (1) ;
}
}
// loop below needs to read and print data to sd card and screen
void loop()
{
// Here is the code from adafruit for reading the temp data:
// Read and print out the temperature, then convert to *F
//float c = tempsensor.readTempC();
//float f = c * 9.0 / 5.0 + 32;
//Serial.print("Temp: "); Serial.print(c); Serial.print("*C\t");
//Serial.print(f); Serial.println("*F");
//delay(3000);
float pascals = baro.getPressure();
// Our weather page presents pressure in Inches (Hg)
// Use http://www.onlineconversion.com/pressure.htm for other units
Serial.print(pascals/3377); Serial.println(" Inches (Hg)");
float altm = baro.getAltitude();
Serial.print(altm); Serial.println(" meters");
float tempC = baro.getTemperature();
Serial.print(tempC); Serial.println("*C");
delay(3000);
// make a string for assembling the data to log:
String dataString = String(id) + "," + String (c) + "*C" + "," + String(f) + "*F";
// read MCP9808 on A4 and append to the string:
Serial.println(dataString);
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
delay(3000);
}
}
Thank you so much for your help in advance!