Skip to content

Commit

Permalink
Issue #8
Browse files Browse the repository at this point in the history
Cleaned up comments and example code
  • Loading branch information
SV-Zanshin authored and SV-Zanshin committed May 13, 2020
1 parent aebaabe commit 44a16a1
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 49 deletions.
8 changes: 4 additions & 4 deletions examples/SPIDemo/SPIDemo.ino
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const uint8_t SPI_CS_PIN = SS;
*******************************************************************************************************************/
BME680_Class BME680; ///< Create an instance of the BME680

float altitude(const int32_t press, const float seaLevel = 1013.25); ///< Forward function declaration with default value for sea level
float altitude(const int32_t press, const float seaLevel = 1013.25); ///< Forward declaration with default value
float altitude(const int32_t press, const float seaLevel)
{
/*!
Expand All @@ -94,7 +94,7 @@ void setup()
/*!
@brief Arduino method called once at startup to initialize the system
@details This is an Arduino IDE method which is called first upon boot or restart. It is only called one time
and then control goes to the main "loop()" method, from which control never returns
and then control goes to the main "loop()" method, from which control never returns
@return void
*/
Serial.begin(SERIAL_SPEED); // Start serial port at Baud rate
Expand Down Expand Up @@ -123,8 +123,8 @@ void loop()
/*!
@brief Arduino method for the main program loop
@details This is the main program for the Arduino IDE, it is an infinite loop and keeps on repeating.
The "sprintf()" function is to pretty-print the values, since floating point is not supported on the
Arduino, split the values into those before and those after the decimal point.
The "sprintf()" function is to pretty-print the values, since floating point is not supported on the
Arduino, split the values into those before and those after the decimal point.
@return void
*/
static int32_t temp, humidity, pressure, gas; // Variable to store readings
Expand Down
109 changes: 64 additions & 45 deletions examples/SoftSPIDemo/SoftSPIDemo.ino
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,18 @@ The most recent version of the BME680 library is available at https://github.com
documentation of the library as well as example programs are described in the project's wiki pages located at
https://github.com/SV-Zanshin/BME680/wiki. \n\n
The BME680 is a very small package so it is unlikely for an Arduino hobbyist to play around with directly, the
hardware used to develop this library is a breakout board from AdaFruit which is well-documented at
https://learn.adafruit.com/adafruit-BME680-humidity-barometric-pressure-temperature-sensor-breakout \n\n
The BME680 is an extremely small physical package that is so tiny as to be impossible to solder at home, hence it
will be used as part of a third-party breakout board. There are several such boards available at this time, for
example \n
Company | Link
------- | ----------
BlueDot | https://www.bluedot.space/sensor-boards/bme680/
Adafruit | https://learn.adafruit.com/adafruit-BME680-humidity-barometric-pressure-temperature-sensor-breakout \n\n
Bosch supplies sample software that runs on various platforms, including the Arduino family; this can be downloaed
at https://github.com/BoschSensortec/BSEC-Arduino-library . This software is part of the Bosch "BSEC" (Bosch
Sensortec Environmental Cluster) framework and somewhat bulky and unwieldy for typical Arduino applications, hence
the choice to make a more compact and rather less abstract library.
This example program initializes the BME680 to use software SPI for communications. The library does not use
floating point mathematics to save on computation space and time, the values for Temperature, Pressure and Humidity
Expand Down Expand Up @@ -43,6 +52,7 @@ Written by Arnd\@SV-Zanshin
Version | Date | Developer | Comments
------- | ---------- | ----------------------------- | ---------------------------------------------------
1.0.2 | 2020-05-13 | https://github.com/SV-Zanshin | Issue #8 - clean up comments and code
1.0.1 | 2019-01-26 | https://github.com/SV-Zanshin | Issue #3 - convert documentation to Doxygen
1.0.0 | 2017-07-01 | https://github.com/SV-Zanshin | Cloned from original SPIDemo.ino program for BME280
*/
Expand Down Expand Up @@ -76,31 +86,30 @@ const uint32_t SERIAL_SPEED = 115200; ///< Set the baud rate for Serial I/O //
*******************************************************************************************************************/
BME680_Class BME680; ///< Create an instance of the BME680

/*!
* @brief This converts a pressure measurement into a height in meters
* @details The corrected sea-level pressure can be passed into the function if it is know, otherwise the standard
* atmospheric pressure of 1013.25hPa is used (see https://en.wikipedia.org/wiki/Atmospheric_pressure
* @param[in] seaLevel Sea-Level pressure in millibars
* @return floating point altitude in meters.
*/
float altitude(const float seaLevel = 1013.25)
float altitude(const int32_t press, const float seaLevel = 1013.25); ///< Forward declaration with default value
float altitude(const int32_t press, const float seaLevel)
{
/*!
* @brief This converts a pressure measurement into a height in meters
* @details The corrected sea-level pressure can be passed into the function if it is know, otherwise the standard
* atmospheric pressure of 1013.25hPa is used (see https://en.wikipedia.org/wiki/Atmospheric_pressure)
* @param[in] press Pressure reading from BME680
* @param[in] seaLevel Sea-Level pressure in millibars
* @return floating point altitude in meters.
*/
static float Altitude;
int32_t temp, hum, press, gas;
BME680.getSensorData(temp, hum, press, gas); // Get the most recent values from the device
Altitude = 44330.0*(1.0 - pow(((float)press / 100.0) / seaLevel, 0.1903)); // Convert into altitude in meters
Altitude = 44330.0*(1.0-pow(((float)press/100.0)/seaLevel,0.1903)); // Convert into altitude in meters
return(Altitude);
} // of method altitude()

void setup()
{
/*!
@brief Arduino method called once at startup to initialize the system
@details This is an Arduino IDE method which is called first upon boot or restart. It is only called one time
and then control goes to the main "loop()" method, from which control never returns
@return void
*/
void setup()
{
Serial.begin(SERIAL_SPEED); // Start serial port at Baud rate
*/ Serial.begin(SERIAL_SPEED); // Start serial port at Baud rate
#ifdef __AVR_ATmega32U4__ // If this is a 32U4 processor, then wait 3 seconds to initialize USB
delay(3000);
#endif
Expand All @@ -109,39 +118,49 @@ void setup()

while (!BME680.begin(SPI_CS_PIN,SPI_MOSI_PIN,SPI_MISO_PIN,SPI_SCK_PIN)) // Start using software SPI protocol
{
Serial.println(F("- Unable to find BME680. Waiting 3 seconds."));
delay(3000);
Serial.println(F("- Unable to find BME680. Waiting 5 seconds."));
delay(5000);
} // of loop until device is located
Serial.println(F("- Setting 16x oversampling for all sensors"));
BME680.setOversampling(TemperatureSensor,Oversample16); // Use enumerated type values
BME680.setOversampling(HumiditySensor, Oversample16);
BME680.setOversampling(PressureSensor, Oversample16);
Serial.println(F("- Setting IIR filter to value of 2 samples"));
BME680.setIIRFilter(IIR2); // Use enumerated type values
BME680.setIIRFilter(IIR16); // Use enumerated type values
Serial.print(F("- Setting gas measurement to 320\xC2\xB0\x43 for 150ms\n")); // "°C" symbols
BME680.setGas(320,150); // 320°c for 150 milliseconds
} // of method setup()

/*!
@brief Arduino method for the main program loop
@details This is the main program for the Arduino IDE, it is an infinite loop and keeps on repeating.
@return void
*/
void loop()
void loop()
{
static int32_t temperature, humidity, pressure, gas; // Store readings
BME680.getSensorData(temperature,humidity,pressure,gas); // Get most recent readings
Serial.print(temperature/100.0,2); // Temperature in deci-degrees
#if defined(ESP32) || defined(ARDUINO_SAMD_ZERO)
Serial.print(F(" ")); // Compiler doesn't liked escaped string
#else
Serial.print(F("\xC2\xB0\C ")); // Representation of the ° symbol
#endif
Serial.print(humidity/1000.0,2); // Humidity in milli-percent
Serial.print(F("%Hum "));
Serial.print(pressure/100.0,2); // Pressure in Pascals
Serial.print(F("hPa "));
Serial.print(altitude(),2);
Serial.print(F("m "));
Serial.print(gas/100.0,2);
Serial.println(F("mOhm"));
delay(5000);
/*!
@brief Arduino method for the main program loop
@details This is the main program for the Arduino IDE, it is an infinite loop and keeps on repeating.
The "sprintf()" function is to pretty-print the values, since floating point is not supported on the
Arduino, split the values into those before and those after the decimal point.
@return void
*/
static int32_t temp, humidity, pressure, gas; // Variable to store readings
static char buf[16]; // Text buffer for sprintf
static float alt; // temp variable for altitude
static uint16_t loopCounter = 0; // Display iterations
if (loopCounter % 25 == 0) // Display header every 25 loops
{ //
Serial.print(F("\nLoop Temp\xC2\xB0\x43 Humid% Press hPa Alt m Air m")); // Show header plus unicode "°C"
Serial.print(F("\xE2\x84\xA6\n==== ====== ====== ========= ======= =======\n"));// and "?" symbols
} // if-then time to show headers //
BME680.getSensorData(temp,humidity,pressure,gas); // Get the most recent readings
sprintf(buf, "%4d %3d.%02d", ++loopCounter%9999, // Clamp iterations to 9999,
(int8_t)(temp/100),(uint8_t)(temp%100)); // Temperature in decidegrees
Serial.print(buf); //
sprintf(buf, "%3d.%03d", (int8_t)(humidity/1000),(uint16_t)(humidity%1000)); // Humidity in milli-percent
Serial.print(buf); //
sprintf(buf, "%7d.%02d", (int16_t)(pressure/100),(uint8_t)(pressure%100)); // Pressure in Pascals
Serial.print(buf); //
alt = altitude(pressure); // temp variable for altitude
sprintf(buf, "%5d.%02d", (int16_t)(alt),((uint8_t)(alt*100)%100)); // Altitude in meters
Serial.print(buf); //
sprintf(buf, "%5d.%02d\n", (int16_t)(gas/100),(uint8_t)(gas%100)); // Resistance in milliohms
Serial.print(buf); //
delay(10000); // Wait 10s before repeating
} // of method loop()

0 comments on commit 44a16a1

Please sign in to comment.