Advertisement
Guest User

Untitled

a guest
May 27th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <BMP180.h>
  3. #include <SPI.h>
  4. #include <SD.h>
  5.  
  6. const int chipSelect = 4;
  7. // Store an instance of the BMP180 sensor.
  8. BMP180 barometer;
  9. // We are going to use the on board LED for an indicator.
  10. int indicatorLed = 13;
  11.  
  12. // Store the current sea level pressure at your location in Pascals.
  13. float seaLevelPressure = 101325;
  14.  
  15. void setup()
  16. {
  17. // We start the serial library to output our messages.
  18. Serial.begin(9600);
  19. // We start the I2C on the Arduino for communication with the BMP180 sensor.
  20. Wire.begin();
  21. // Set up the Indicator LED.
  22. pinMode(indicatorLed, OUTPUT);
  23. // We create an instance of our BMP180 sensor.
  24. barometer = BMP180();
  25. // We check to see if we can connect to the sensor.
  26. if (barometer.EnsureConnected())
  27. {
  28. Serial.println("Connected to BMP180."); // Output we are connected to the computer.
  29. digitalWrite(indicatorLed, HIGH); // Set our LED.
  30.  
  31. // When we have connected, we reset the device to ensure a clean start.
  32. barometer.SoftReset();
  33. // Now we initialize the sensor and pull the calibration data.
  34. barometer.Initialize();
  35. }
  36. else
  37. {
  38. Serial.println("No sensor found.");
  39. digitalWrite(indicatorLed, LOW); // Set our LED.
  40. }
  41.  
  42. // Open serial communications and wait for port to open:
  43. Serial.begin(9600);
  44. while (!Serial) {
  45. ; // wait for serial port to connect. Needed for native USB port only
  46. }
  47.  
  48.  
  49. Serial.print("Initializing SD card...");
  50.  
  51. // see if the card is present and can be initialized:
  52. if (!SD.begin(chipSelect)) {
  53. Serial.println("Card failed, or not present");
  54. // don't do anything more:
  55. return;
  56. }
  57. Serial.println("card initialized.");
  58. }
  59.  
  60. void loop()
  61. {
  62. if (barometer.IsConnected)
  63. {
  64. // Retrive the current pressure in Pascals.
  65. long currentPressure = barometer.GetPressure();
  66.  
  67. // Print out the Pressure.
  68. Serial.print("Pressure: ");
  69. Serial.print(currentPressure);
  70. Serial.print(" Pa");
  71.  
  72. // Retrive the current altitude (in meters). Current Sea Level Pressure is required for this.
  73. float altitude = barometer.GetAltitude(seaLevelPressure);
  74.  
  75. // Print out the Altitude.
  76. Serial.print("\tAltitude: ");
  77. Serial.print(altitude);
  78. Serial.print(" m");
  79.  
  80. // Retrive the current temperature in degrees celcius.
  81. float currentTemperature = barometer.GetTemperature();
  82.  
  83. // Print out the Temperature
  84. Serial.print("\tTemperature: ");
  85. Serial.print(currentTemperature);
  86. Serial.print(" C");
  87. Serial.println(); // Start a new line.
  88. delay(1000); // Show new results every second.
  89. }
  90.  
  91.  
  92.  
  93.  
  94. // open the file. note that only one file can be open at a time,
  95. // so you have to close this one before opening another.
  96. File dataFile = SD.open("datalog.txt", FILE_WRITE);
  97.  
  98. // if the file is available, write to it:
  99. if (dataFile) {
  100.  
  101. dataFile.print("\tAltitude: ");
  102. dataFile.print(altitude);
  103. dataFile.print(" m");
  104. dataFile.print("Pressure: ");
  105. dataFile.print(currentPressure);
  106. dataFile.print(" Pa");
  107. dataFile.print("\tTemperature: ");
  108. dataFile.print(currentTemperature);
  109. dataFile.print(" C");
  110.  
  111. dataFile.close();
  112. // print to the serial port too:
  113. Serial.println(dataString);
  114. }
  115. // if the file isn't open, pop up an error:
  116. else {
  117. Serial.println("error opening datalog.txt");
  118. }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement