ScienceGeyser

BMEwithBlink

Jun 15th, 2021 (edited)
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Wire.h>
  2. #include <Adafruit_Sensor.h>
  3. #include <Adafruit_BME280.h>
  4. #define LEDPERIOD 250 // 1/2 the LED blink rate
  5. #define READPERIOD 1000 // 1x sample rate
  6. #define SEALEVELPRESSURE_HPA (1013.25)
  7.  
  8. Adafruit_BME280 bme;
  9. int ledState = HIGH;
  10. int
  11. unsigned long ledlastTime = 0;
  12. unsigned long readlastTime = 0;
  13.  
  14. void setup() {
  15.   Serial.begin(9600);
  16.   Wire.begin();
  17.  
  18.   pinMode(LED_BUILTIN, OUTPUT);
  19.   digitalWrite(LED_BUILTIN, ledState);
  20.  
  21.   if (!bme.begin(0x76)) {
  22.     Serial.println("Could not find a valid BME280 sensor, check wiring!");
  23.     while (1);
  24.   }
  25. }
  26.  
  27. void loop() {
  28.   unsigned long lednow = millis();
  29.   unsigned long readnow = millis();
  30.  
  31.   if(lednow - ledlastTime >= LEDPERIOD){
  32.     ledlastTime = lednow;
  33.     if (ledState == LOW){
  34.       ledState = HIGH;
  35.     }else{
  36.       ledState = LOW;
  37.     }
  38.   }
  39.   digitalWrite(LED_BUILTIN, ledState);
  40.  
  41.   if(readnow - readlastTime >= READPERIOD){
  42.     readlastTime = readnow;
  43.     Serial.print("Temperature = ");
  44.     Serial.print(bme.readTemperature());
  45.     Serial.println("*C");
  46.  
  47.     Serial.print("Pressure = ");
  48.     Serial.print(bme.readPressure() / 100.0F);
  49.     Serial.println("hPa");
  50.  
  51.     Serial.print("Approx. Altitude = ");
  52.     Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
  53.     Serial.println("m");
  54.  
  55.     Serial.print("Humidity = ");
  56.     Serial.print(bme.readHumidity());
  57.     Serial.println("%");
  58.  
  59.     Serial.println();
  60.   }
  61. }
Add Comment
Please, Sign In to add comment