Advertisement
Guest User

Untitled

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