Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #include <BMP180.h>
- #include <SPI.h>
- #include <SD.h>
- const int chipSelect = 4;
- // Store an instance of the BMP180 sensor.
- BMP180 barometer;
- // We are going to use the on board LED for an indicator.
- int indicatorLed = 13;
- // Store the current sea level pressure at your location in Pascals.
- float seaLevelPressure = 101325;
- void setup()
- {
- // We start the serial library to output our messages.
- Serial.begin(9600);
- // We start the I2C on the Arduino for communication with the BMP180 sensor.
- Wire.begin();
- // Set up the Indicator LED.
- pinMode(indicatorLed, OUTPUT);
- // We create an instance of our BMP180 sensor.
- barometer = BMP180();
- // We check to see if we can connect to the sensor.
- if (barometer.EnsureConnected())
- {
- Serial.println("Connected to BMP180."); // Output we are connected to the computer.
- digitalWrite(indicatorLed, HIGH); // Set our LED.
- // When we have connected, we reset the device to ensure a clean start.
- barometer.SoftReset();
- // Now we initialize the sensor and pull the calibration data.
- barometer.Initialize();
- }
- else
- {
- Serial.println("No sensor found.");
- digitalWrite(indicatorLed, LOW); // Set our LED.
- }
- // Open serial communications and wait for port to open:
- Serial.begin(9600);
- while (!Serial) {
- ; // wait for serial port to connect. Needed for native USB port only
- }
- Serial.print("Initializing SD card...");
- // 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:
- return;
- }
- Serial.println("card initialized.");
- }
- void loop()
- {
- if (barometer.IsConnected)
- {
- // Retrive the current pressure in Pascals.
- long currentPressure = barometer.GetPressure();
- // Print out the Pressure.
- Serial.print("Pressure: ");
- Serial.print(currentPressure);
- Serial.print(" Pa");
- // Retrive the current altitude (in meters). Current Sea Level Pressure is required for this.
- float altitude = barometer.GetAltitude(seaLevelPressure);
- // Print out the Altitude.
- Serial.print("\tAltitude: ");
- Serial.print(altitude);
- Serial.print(" m");
- // Retrive the current temperature in degrees celcius.
- float currentTemperature = barometer.GetTemperature();
- // Print out the Temperature
- Serial.print("\tTemperature: ");
- Serial.print(currentTemperature);
- Serial.print(" C");
- Serial.println(); // Start a new line.
- delay(1000); // Show new results every second.
- }
- // 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.print("\tAltitude: ");
- dataFile.print(altitude);
- dataFile.print(" m");
- dataFile.print("Pressure: ");
- dataFile.print(currentPressure);
- dataFile.print(" Pa");
- dataFile.print("\tTemperature: ");
- dataFile.print(currentTemperature);
- dataFile.print(" C");
- dataFile.close();
- // print to the serial port too:
- Serial.println(dataString);
- }
- // if the file isn't open, pop up an error:
- else {
- Serial.println("error opening datalog.txt");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement