Advertisement
learnelectronics

Climber's Friend

Nov 30th, 2017
1,056
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.90 KB | None | 0 0
  1. /*
  2.  * Climber's Friend
  3.  *
  4.  * learnelectronics
  5.  * 29 NOV 2017
  6.  *
  7.  * www.youtube.com/c/learnelectronics
  8.  */
  9.  
  10. #include <Wire.h>                                                     //I2C library for both sensors
  11. #include <Adafruit_Sensor.h>                                          //Adafruit sensor library for BME280
  12. #include <Adafruit_BME280.h>                                          //BME280 library
  13. #include <Adafruit_SSD1306.h>                                         //OLED library
  14.  
  15.  
  16. #define OLED_RESET 4                                                  //not connected
  17.  
  18.  
  19. #define SEALEVELPRESSURE_HPA (1013.25)                                //put your local Sea level pressure here
  20.  
  21. Adafruit_BME280 bme;                                                  //create instance of BME280 called bme
  22. Adafruit_SSD1306 display(OLED_RESET);                                 //create instance of oled called display
  23.  
  24. unsigned long delayTime;                                              //declare var for delay
  25. float altstart = 0.0;                                                 //declare var for starting altitude
  26.  
  27. void setup() {
  28.     Serial.begin(9600);                                               //serial comms for debug
  29.  
  30.   display.begin(SSD1306_SWITCHCAPVCC, 0x3C);                          // start the oled screen @ hex addy 0x3c
  31.   display.display();
  32.   display.clearDisplay();
  33.   Serial.println(F("BME280 test"));
  34.  
  35.     bool status;
  36.    
  37.     // default settings
  38.     status = bme.begin();                                             //start the bme280 sensor
  39.     if (!status) {
  40.         Serial.println("NO BME");
  41.         while (1);
  42.     }
  43.    
  44.     Serial.println("-- Default Test --");
  45.     delayTime = 2000;                                                 //define delay time
  46.  
  47.     Serial.println();
  48.     delay(5000);                                                      //wait 5 seconds for BME to start
  49.  
  50.     altstart = ((bme.readAltitude(SEALEVELPRESSURE_HPA))*3.28084);    //grab initial altitude and store in var
  51. }
  52.  
  53.  
  54. void loop() {
  55.     printValues();                                                    //call printVaulues function
  56.     delay(delayTime);                                                 //wait
  57. }
  58.  
  59.  
  60. void printValues() {
  61.   display.clearDisplay();                                             //clear the display and buffer
  62.   display.setTextSize(1);                                             //set text to smallest size
  63.   display.setTextColor(WHITE);                                        //set color to white
  64.   display.setCursor(0,0);                                             //cursor to upper left
  65.    
  66.     display.print("Temp.    = ");                                     //output line title to buffer
  67.     display.print((bme.readTemperature()*1.8)+32);                    //output line value to buffer converted to F
  68.     display.println(" F");                                            //output line units to buffer with linefeed
  69.  
  70.     display.print("Pressure = ");
  71.  
  72.     display.print((bme.readPressure() / 100.0F)*.0295);               //output line value to buffer converted to inhes of mercury
  73.     display.println(" in");
  74.  
  75.     display.print("Alt.     = ");
  76.     display.print((bme.readAltitude(SEALEVELPRESSURE_HPA))*3.28084);  //output line value to buffer converted to feet
  77.     display.println(" Ft");
  78.     float nowalt = ((bme.readAltitude(SEALEVELPRESSURE_HPA))*3.28084);//create a float call nowalt to holt curent alt data
  79.  
  80.     display.print("Humidity = ");
  81.     display.print(bme.readHumidity());
  82.     display.println(" %");
  83.     display.println("");
  84.     display.print("Alt.Chg. =");
  85.     display.print((altstart-nowalt)*-1);                              //output change in altitude (start alt - curr alt) * -1
  86.     display.print(" Ft");
  87.  
  88.     display.display();                                                //show whats in the buffer on the display
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement