publish libraries to avoid users to have to download them one by one

This commit is contained in:
1technophile committed 2018-03-04 13:47:37 +01:00
1 parent 93c075e880
commit f0546fab8c
396 files changed
+536928

No files matched your search

@@ -0,0 +1,116 @@
/******************************************************************************
CSV_Output.ino
BME280 Arduino and Teensy example
Marshall Taylor @ SparkFun Electronics
May 20, 2015
https://github.com/sparkfun/SparkFun_BME280_Arduino_Library
This sketch configures two BME280 to produce comma separated values for use
in generating spreadsheet graphs.
in 10 being used for chip select.
Resources:
Uses Wire.h for I2C operation
Uses SPI.h for SPI operation
Development environment specifics:
Arduino IDE 1.6.4
Teensy loader 1.23
This code is released under the [MIT License](http://opensource.org/licenses/MIT).
Please review the LICENSE.md file included with this example. If you have any questions
or concerns with licensing, please contact techsupport@sparkfun.com.
Distributed as-is; no warranty is given.
******************************************************************************/
#include <stdint.h>
#include "SparkFunBME280.h"
#include "Wire.h"
#include "SPI.h"
//Global sensor object
BME280 mySensor;
unsigned int sampleNumber = 0; //For counting number of CSV rows
void setup()
{
//***Driver settings********************************//
//commInterface can be I2C_MODE or SPI_MODE
//specify chipSelectPin using arduino pin names
//specify I2C address. Can be 0x77(default) or 0x76
//For I2C, enable the following and disable the SPI section
mySensor.settings.commInterface = I2C_MODE;
mySensor.settings.I2CAddress = 0x77;
//For SPI enable the following and dissable the I2C section
//mySensor.settings.commInterface = SPI_MODE;
//mySensor.settings.chipSelectPin = 10;
//***Operation settings*****************************//
mySensor.settings.runMode = 3; // 3, Normal mode
mySensor.settings.tStandby = 0; // 0, 0.5ms
mySensor.settings.filter = 0; // 0, filter off
//tempOverSample can be:
// 0, skipped
// 1 through 5, oversampling *1, *2, *4, *8, *16 respectively
mySensor.settings.tempOverSample = 1;
//pressOverSample can be:
// 0, skipped
// 1 through 5, oversampling *1, *2, *4, *8, *16 respectively
mySensor.settings.pressOverSample = 1;
//humidOverSample can be:
// 0, skipped
// 1 through 5, oversampling *1, *2, *4, *8, *16 respectively
mySensor.settings.humidOverSample = 1;
Serial.begin(57600);
Serial.print("Program Started\n");
Serial.print("Starting BME280... result of .begin(): 0x");
delay(10); //Make sure sensor had enough time to turn on. BME280 requires 2ms to start up.
//Calling .begin() causes the settings to be loaded
Serial.println(mySensor.begin(), HEX);
//Build a first-row of column headers
Serial.print("\n\n");
Serial.print("Sample,");
Serial.print("T(deg C),");
Serial.print("T(deg F),");
Serial.print("P(Pa),");
Serial.print("Alt(m),");
Serial.print("Alt(ft),");
Serial.print("%RH");
Serial.println("");
}
void loop()
{
//Print each row in the loop
//Start with temperature, as that data is needed for accurate compensation.
//Reading the temperature updates the compensators of the other functions
//in the background.
Serial.print(sampleNumber);
Serial.print(",");
Serial.print(mySensor.readTempC(), 2);
Serial.print(",");
Serial.print(mySensor.readTempF(), 3);
Serial.print(",");
Serial.print(mySensor.readFloatPressure(), 0);
Serial.print(",");
Serial.print(mySensor.readFloatAltitudeMeters(), 3);
Serial.print(",");
Serial.print(mySensor.readFloatAltitudeFeet(), 3);
Serial.print(",");
Serial.print(mySensor.readFloatHumidity(), 0);
Serial.println();
sampleNumber++;
delay(50);
}
@@ -0,0 +1,104 @@
#include "CircularBuffer.h"
#include <stdint.h>
//****************************************************************************//
//
// Circular buffer
//
//****************************************************************************//
//Construct a CircularBuffer type with arguments
// uint16_t inputSize: number of elements
CircularBuffer::CircularBuffer(uint16_t inputSize)
{
cBufferData = new float[inputSize];
cBufferLastPtr = 0;
cBufferElementsUsed = 0;
cBufferSize = inputSize;
}
CircularBuffer::~CircularBuffer()
{
delete[] cBufferData;
}
//Get an element at some depth into the circular buffer
//zero is the push location. Max is cBufferSize - 1
//
//Arguments:
// uint16_t elementNum: number of element in
//
float CircularBuffer::getElement( uint16_t elementNum )
{
//Translate elementNum into terms of cBufferLastPtr.
int16_t virtualElementNum;
virtualElementNum = cBufferLastPtr - elementNum;
if( virtualElementNum < 0 )
{
virtualElementNum += cBufferSize;
}
//Output the value
return cBufferData[virtualElementNum];
}
//Put a new element into the buffer.
//This also expands the size up to the max size
//Arguments:
//
// int16_t elementVal: value of new element
//
void CircularBuffer::pushElement( float elementVal )
{
//inc. the pointer
cBufferLastPtr++;
//deal with roll
if( cBufferLastPtr >= cBufferSize )
{
cBufferLastPtr = 0;
}
//write data
cBufferData[cBufferLastPtr] = elementVal;
//increase length up to cBufferSize
if( cBufferElementsUsed < cBufferSize )
{
cBufferElementsUsed++;
}
}
//Averages the last n numbers and provides that. Discards fractions
float CircularBuffer::averageLast( uint16_t numElements )
{
if( numElements < recordLength() )
{
numElements = recordLength();
}
//Add up all the elements
float accumulator = 0;
int8_t i;
for( i = 0; i < numElements; i++ )
{
accumulator += getElement( i );
}
//Divide by number of elements
if( numElements != 0 )
{
accumulator /= numElements;
}
else
{
accumulator = 0;
}
return accumulator;
}
//Returns the current size of the buffer
uint16_t CircularBuffer::recordLength( void )
{
return cBufferElementsUsed;
}
@@ -0,0 +1,33 @@
#ifndef CIRCULARBUFFER_H
#define CIRCULARBUFFER_H
#include <stdint.h>
//****************************************************************************//
//
// Circular Buffer
//
//****************************************************************************//
//Class CircularBuffer is int16_t
//Does not care about over-running real data ( if request is outside length's bounds ).
//For example, the underlying machine writes [48], [49], [0], [1] ...
class CircularBuffer
{
public:
CircularBuffer( uint16_t inputSize );
~CircularBuffer();
float getElement( uint16_t ); //zero is the push location
void pushElement( float );
float averageLast( uint16_t );
uint16_t recordLength( void );
private:
uint16_t cBufferSize;
float *cBufferData;
int16_t cBufferLastPtr;
uint8_t cBufferElementsUsed;
};
#endif // CIRCULARBUFFER_H
@@ -0,0 +1,170 @@
/******************************************************************************
I2C_DeltaAltitude.ino
BME280 Arduino and Teensy example
Marshall Taylor @ SparkFun Electronics
May 20, 2015
https://github.com/sparkfun/SparkFun_BME280_Arduino_Library
This sketch configures a BME280 to measure height changes, with a button
to zero relative altitude.
Hardware connections:
BME280 -> Arduino
GND -> GND
3.3 -> 3.3
SDA -> A4
SCL -> A5
Button (Normally open) between A4 and A5.
To use, run the sketch then press and hold the button until the data on the
serial monitor shows zero. Then, move the sensor to a new altitude and watch
how the output changes!
Note: For most accurate results keep the sensor orientation and temperature the
same between measurements.
Resources:
Uses Wire.h for I2C operation
Uses SPI.h for SPI operation
Included CircularBuffer class for averaging
Development environment specifics:
Arduino IDE 1.6.4
Teensy loader 1.23
This code is released under the [MIT License](http://opensource.org/licenses/MIT).
Please review the LICENSE.md file included with this example. If you have any questions
or concerns with licensing, please contact techsupport@sparkfun.com.
Distributed as-is; no warranty is given.
******************************************************************************/
#include <stdint.h>
#include "SparkFunBME280.h"
#include "CircularBuffer.h"
#include "Wire.h"
#include "SPI.h"
//Global sensor object
BME280 mySensor;
uint8_t buttonGroundPin = A0;
uint8_t buttonSensePin = A2;
//This is a fixed-size buffer used for averaging data, a software LP filter
//Depending on the settings, the response can be quite slow.
CircularBuffer myBuffer(50);
float reference = 0;
void setup()
{
//Configure the button IO
pinMode( buttonSensePin, INPUT_PULLUP );
pinMode( buttonGroundPin, OUTPUT );
digitalWrite( buttonGroundPin, 0 );
//***Driver settings********************************//
//commInterface can be I2C_MODE or SPI_MODE
//specify chipSelectPin using arduino pin names
//specify I2C address. Can be 0x77(default) or 0x76
//For I2C, enable the following and disable the SPI section
mySensor.settings.commInterface = I2C_MODE;
mySensor.settings.I2CAddress = 0x77;
//For SPI enable the following and dissable the I2C section
//mySensor.settings.commInterface = SPI_MODE;
//mySensor.settings.chipSelectPin = 10;
//***Operation settings*****************************//
//renMode can be:
// 0, Sleep mode
// 1 or 2, Forced mode
// 3, Normal mode
mySensor.settings.runMode = 3; //Normal mode
//tStandby can be:
// 0, 0.5ms
// 1, 62.5ms
// 2, 125ms
// 3, 250ms
// 4, 500ms
// 5, 1000ms
// 6, 10ms
// 7, 20ms
mySensor.settings.tStandby = 0;
//filter can be off or number of FIR coefficients to use:
// 0, filter off
// 1, coefficients = 2
// 2, coefficients = 4
// 3, coefficients = 8
// 4, coefficients = 16
mySensor.settings.filter = 4; //Lots of HW filter
//tempOverSample can be:
// 0, skipped
// 1 through 5, oversampling *1, *2, *4, *8, *16 respectively
mySensor.settings.tempOverSample = 5;
//pressOverSample can be:
// 0, skipped
// 1 through 5, oversampling *1, *2, *4, *8, *16 respectively
mySensor.settings.pressOverSample = 5;
//humidOverSample can be:
// 0, skipped
// 1 through 5, oversampling *1, *2, *4, *8, *16 respectively
mySensor.settings.humidOverSample = 1;
Serial.begin(57600);
Serial.print("Program Started\n");
Serial.print("Starting BME280... result of .begin(): 0x");
delay(10); //Make sure sensor had enough time to turn on. BME280 requires 2ms to start up.
//Calling .begin() causes the settings to be loaded
Serial.println(mySensor.begin(), HEX);
Serial.println();
}
void loop()
{
//Get the local temperature! Do this for calibration
mySensor.readTempC();
//Check button. No debounce, so hold for operation
if( digitalRead( buttonSensePin ) == 0 )
{
//Set reference altitude.
reference = mySensor.readFloatAltitudeFeet();
}
float lastAlt = mySensor.readFloatAltitudeFeet() - reference;
myBuffer.pushElement(lastAlt);
float tempAlt = myBuffer.averageLast(15); //Do an average of the latest samples
Serial.println();
Serial.print("Temperature: ");
Serial.print(mySensor.readTempC(), 2);
Serial.println(" degrees C");
Serial.print("Last sample: ");
if(lastAlt >= 0)
{
Serial.print(" ");
}
Serial.print(lastAlt, 2);
Serial.println(" ft");
Serial.print("Last 15 samples averaged: ");
if(tempAlt >= 0)
{
Serial.print(" ");
}
Serial.print(tempAlt);
Serial.println(" ft");
delay(200);
}
@@ -0,0 +1,214 @@
/******************************************************************************
I2C_ReadAllData.ino
BME280 Arduino and Teensy example
Marshall Taylor @ SparkFun Electronics
May 20, 2015
https://github.com/sparkfun/SparkFun_BME280_Arduino_Library
This sketch configures the BME280 to read all measurements. The sketch also
displays the BME280's physical memory and what the driver perceives the
calibration words to be.
Resources:
Uses Wire.h for I2C operation
Uses SPI.h for SPI operation
Development environment specifics:
Arduino IDE 1.6.4
Teensy loader 1.23
This code is released under the [MIT License](http://opensource.org/licenses/MIT).
Please review the LICENSE.md file included with this example. If you have any questions
or concerns with licensing, please contact techsupport@sparkfun.com.
Distributed as-is; no warranty is given.
******************************************************************************/
#include <stdint.h>
#include "SparkFunBME280.h"
//Library allows either I2C or SPI, so include both.
#include "Wire.h"
#include "SPI.h"
//Global sensor object
BME280 mySensor;
void setup()
{
//***Driver settings********************************//
//commInterface can be I2C_MODE or SPI_MODE
//specify chipSelectPin using arduino pin names
//specify I2C address. Can be 0x77(default) or 0x76
//For I2C, enable the following and disable the SPI section
mySensor.settings.commInterface = I2C_MODE;
mySensor.settings.I2CAddress = 0x77;
//For SPI enable the following and dissable the I2C section
//mySensor.settings.commInterface = SPI_MODE;
//mySensor.settings.chipSelectPin = 10;
//***Operation settings*****************************//
//renMode can be:
// 0, Sleep mode
// 1 or 2, Forced mode
// 3, Normal mode
mySensor.settings.runMode = 3; //Normal mode
//tStandby can be:
// 0, 0.5ms
// 1, 62.5ms
// 2, 125ms
// 3, 250ms
// 4, 500ms
// 5, 1000ms
// 6, 10ms
// 7, 20ms
mySensor.settings.tStandby = 0;
//filter can be off or number of FIR coefficients to use:
// 0, filter off
// 1, coefficients = 2
// 2, coefficients = 4
// 3, coefficients = 8
// 4, coefficients = 16
mySensor.settings.filter = 0;
//tempOverSample can be:
// 0, skipped
// 1 through 5, oversampling *1, *2, *4, *8, *16 respectively
mySensor.settings.tempOverSample = 1;
//pressOverSample can be:
// 0, skipped
// 1 through 5, oversampling *1, *2, *4, *8, *16 respectively
mySensor.settings.pressOverSample = 1;
//humidOverSample can be:
// 0, skipped
// 1 through 5, oversampling *1, *2, *4, *8, *16 respectively
mySensor.settings.humidOverSample = 1;
Serial.begin(57600);
Serial.print("Program Started\n");
Serial.print("Starting BME280... result of .begin(): 0x");
//Calling .begin() causes the settings to be loaded
delay(10); //Make sure sensor had enough time to turn on. BME280 requires 2ms to start up.
Serial.println(mySensor.begin(), HEX);
Serial.print("Displaying ID, reset and ctrl regs\n");
Serial.print("ID(0xD0): 0x");
Serial.println(mySensor.readRegister(BME280_CHIP_ID_REG), HEX);
Serial.print("Reset register(0xE0): 0x");
Serial.println(mySensor.readRegister(BME280_RST_REG), HEX);
Serial.print("ctrl_meas(0xF4): 0x");
Serial.println(mySensor.readRegister(BME280_CTRL_MEAS_REG), HEX);
Serial.print("ctrl_hum(0xF2): 0x");
Serial.println(mySensor.readRegister(BME280_CTRL_HUMIDITY_REG), HEX);
Serial.print("\n\n");
Serial.print("Displaying all regs\n");
uint8_t memCounter = 0x80;
uint8_t tempReadData;
for(int rowi = 8; rowi < 16; rowi++ )
{
Serial.print("0x");
Serial.print(rowi, HEX);
Serial.print("0:");
for(int coli = 0; coli < 16; coli++ )
{
tempReadData = mySensor.readRegister(memCounter);
Serial.print((tempReadData >> 4) & 0x0F, HEX);//Print first hex nibble
Serial.print(tempReadData & 0x0F, HEX);//Print second hex nibble
Serial.print(" ");
memCounter++;
}
Serial.print("\n");
}
Serial.print("\n\n");
Serial.print("Displaying concatenated calibration words\n");
Serial.print("dig_T1, uint16: ");
Serial.println(mySensor.calibration.dig_T1);
Serial.print("dig_T2, int16: ");
Serial.println(mySensor.calibration.dig_T2);
Serial.print("dig_T3, int16: ");
Serial.println(mySensor.calibration.dig_T3);
Serial.print("dig_P1, uint16: ");
Serial.println(mySensor.calibration.dig_P1);
Serial.print("dig_P2, int16: ");
Serial.println(mySensor.calibration.dig_P2);
Serial.print("dig_P3, int16: ");
Serial.println(mySensor.calibration.dig_P3);
Serial.print("dig_P4, int16: ");
Serial.println(mySensor.calibration.dig_P4);
Serial.print("dig_P5, int16: ");
Serial.println(mySensor.calibration.dig_P5);
Serial.print("dig_P6, int16: ");
Serial.println(mySensor.calibration.dig_P6);
Serial.print("dig_P7, int16: ");
Serial.println(mySensor.calibration.dig_P7);
Serial.print("dig_P8, int16: ");
Serial.println(mySensor.calibration.dig_P8);
Serial.print("dig_P9, int16: ");
Serial.println(mySensor.calibration.dig_P9);
Serial.print("dig_H1, uint8: ");
Serial.println(mySensor.calibration.dig_H1);
Serial.print("dig_H2, int16: ");
Serial.println(mySensor.calibration.dig_H2);
Serial.print("dig_H3, uint8: ");
Serial.println(mySensor.calibration.dig_H3);
Serial.print("dig_H4, int16: ");
Serial.println(mySensor.calibration.dig_H4);
Serial.print("dig_H5, int16: ");
Serial.println(mySensor.calibration.dig_H5);
Serial.print("dig_H6, uint8: ");
Serial.println(mySensor.calibration.dig_H6);
Serial.println();
}
void loop()
{
//Each loop, take a reading.
//Start with temperature, as that data is needed for accurate compensation.
//Reading the temperature updates the compensators of the other functions
//in the background.
Serial.print("Temperature: ");
Serial.print(mySensor.readTempC(), 2);
Serial.println(" degrees C");
Serial.print("Temperature: ");
Serial.print(mySensor.readTempF(), 2);
Serial.println(" degrees F");
Serial.print("Pressure: ");
Serial.print(mySensor.readFloatPressure(), 2);
Serial.println(" Pa");
Serial.print("Altitude: ");
Serial.print(mySensor.readFloatAltitudeMeters(), 2);
Serial.println("m");
Serial.print("Altitude: ");
Serial.print(mySensor.readFloatAltitudeFeet(), 2);
Serial.println("ft");
Serial.print("%RH: ");
Serial.print(mySensor.readFloatHumidity(), 2);
Serial.println(" %");
Serial.println();
delay(1000);
}
@@ -0,0 +1,147 @@
/******************************************************************************
I2C_and_SPI_Multisensor.ino
BME280 Arduino and Teensy example
Marshall Taylor @ SparkFun Electronics
May 20, 2015
https://github.com/sparkfun/SparkFun_BME280_Arduino_Library
This sketch configures two BME280 breakouts. One is configured as I2C and
the other as SPI. Data from both are displayed.
Sensor A is I2C and connected through A4 and A5 (SDA/SCL)
Sensor B is SPI and connected through pins 10, 11, 12, and 13 through a
level shifter, with pin 10 being used for chip select.
Resources:
Uses Wire.h for I2C operation
Uses SPI.h for SPI operation
Development environment specifics:
Arduino IDE 1.6.4
Teensy loader 1.23
This code is released under the [MIT License](http://opensource.org/licenses/MIT).
Please review the LICENSE.md file included with this example. If you have any questions
or concerns with licensing, please contact techsupport@sparkfun.com.
Distributed as-is; no warranty is given.
******************************************************************************/
#include <stdint.h>
#include "SparkFunBME280.h"
#include "Wire.h"
#include "SPI.h"
//Global sensor object
BME280 mySensorA;
BME280 mySensorB;
void setup()
{
//***Set up sensor 'A'******************************//
//commInterface can be I2C_MODE or SPI_MODE
mySensorA.settings.commInterface = I2C_MODE;
mySensorA.settings.I2CAddress = 0x77;
mySensorA.settings.runMode = 3; // 3, Normal mode
mySensorA.settings.tStandby = 0; // 0, 0.5ms
mySensorA.settings.filter = 0; // 0, filter off
//tempOverSample can be:
// 0, skipped
// 1 through 5, oversampling *1, *2, *4, *8, *16 respectively
mySensorA.settings.tempOverSample = 1;
//pressOverSample can be:
// 0, skipped
// 1 through 5, oversampling *1, *2, *4, *8, *16 respectively
mySensorA.settings.pressOverSample = 1;
//humidOverSample can be:
// 0, skipped
// 1 through 5, oversampling *1, *2, *4, *8, *16 respectively
mySensorA.settings.humidOverSample = 1;
//***Set up sensor 'B'******************************//
//commInterface can be I2C_MODE or SPI_MODE
//specify chipSelectPin using arduino pin names
//specify I2C address. Can be 0x77(default) or 0x76
mySensorB.settings.commInterface = SPI_MODE;
mySensorB.settings.chipSelectPin = 10;
mySensorB.settings.runMode = 3; // 3, Normal mode
mySensorB.settings.tStandby = 0; // 0, 0.5ms
mySensorB.settings.filter = 0; // 0, filter off
//tempOverSample can be:
// 0, skipped
// 1 through 5, oversampling *1, *2, *4, *8, *16 respectively
mySensorB.settings.tempOverSample = 1;
//pressOverSample can be:
// 0, skipped
// 1 through 5, oversampling *1, *2, *4, *8, *16 respectively
mySensorB.settings.pressOverSample = 1;
//humidOverSample can be:
// 0, skipped
// 1 through 5, oversampling *1, *2, *4, *8, *16 respectively
mySensorB.settings.humidOverSample = 1;
//***Initialize the things**************************//
Serial.begin(57600);
Serial.print("Program Started\n");
Serial.println("Starting BME280s... result of .begin():");
delay(10); //Make sure sensor had enough time to turn on. BME280 requires 2ms to start up.
//Calling .begin() causes the settings to be loaded
Serial.print("Sensor A: 0x");
Serial.println(mySensorA.begin(), HEX);
Serial.print("Sensor B: 0x");
Serial.println(mySensorB.begin(), HEX);
}
void loop()
{
//Start with temperature, as that data is needed for accurate compensation.
//Reading the temperature updates the compensators of the other functions
//in the background.
Serial.print("Temperature: ");
Serial.print(mySensorA.readTempC(), 2);
Serial.print(", ");
Serial.print(mySensorB.readTempC(), 2);
Serial.println(" degrees C");
Serial.print("Temperature: ");
Serial.print(mySensorA.readTempF(), 2);
Serial.print(", ");
Serial.print(mySensorB.readTempF(), 2);
Serial.println(" degrees F");
Serial.print("Pressure: ");
Serial.print(mySensorA.readFloatPressure(), 2);
Serial.print(", ");
Serial.print(mySensorB.readFloatPressure(), 2);
Serial.println(" Pa");
Serial.print("Altitude: ");
Serial.print(mySensorA.readFloatAltitudeMeters(), 2);
Serial.print(", ");
Serial.print(mySensorB.readFloatAltitudeMeters(), 2);
Serial.println("m");
Serial.print("Altitude: ");
Serial.print(mySensorA.readFloatAltitudeFeet(), 2);
Serial.print(", ");
Serial.print(mySensorB.readFloatAltitudeFeet(), 2);
Serial.println("ft");
Serial.print("%RH: ");
Serial.print(mySensorA.readFloatHumidity(), 2);
Serial.print(", ");
Serial.print(mySensorB.readFloatHumidity(), 2);
Serial.println(" %");
Serial.println();
delay(1000);
}
@@ -0,0 +1,104 @@
#include "CircularBuffer.h"
#include <stdint.h>
//****************************************************************************//
//
// Circular buffer
//
//****************************************************************************//
//Construct a CircularBuffer type with arguments
// uint16_t inputSize: number of elements
CircularBuffer::CircularBuffer(uint16_t inputSize)
{
cBufferData = new float[inputSize];
cBufferLastPtr = 0;
cBufferElementsUsed = 0;
cBufferSize = inputSize;
}
CircularBuffer::~CircularBuffer()
{
delete[] cBufferData;
}
//Get an element at some depth into the circular buffer
//zero is the push location. Max is cBufferSize - 1
//
//Arguments:
// uint16_t elementNum: number of element in
//
float CircularBuffer::getElement( uint16_t elementNum )
{
//Translate elementNum into terms of cBufferLastPtr.
int16_t virtualElementNum;
virtualElementNum = cBufferLastPtr - elementNum;
if( virtualElementNum < 0 )
{
virtualElementNum += cBufferSize;
}
//Output the value
return cBufferData[virtualElementNum];
}
//Put a new element into the buffer.
//This also expands the size up to the max size
//Arguments:
//
// int16_t elementVal: value of new element
//
void CircularBuffer::pushElement( float elementVal )
{
//inc. the pointer
cBufferLastPtr++;
//deal with roll
if( cBufferLastPtr >= cBufferSize )
{
cBufferLastPtr = 0;
}
//write data
cBufferData[cBufferLastPtr] = elementVal;
//increase length up to cBufferSize
if( cBufferElementsUsed < cBufferSize )
{
cBufferElementsUsed++;
}
}
//Averages the last n numbers and provides that. Discards fractions
float CircularBuffer::averageLast( uint16_t numElements )
{
if( numElements < recordLength() )
{
numElements = recordLength();
}
//Add up all the elements
float accumulator = 0;
int8_t i;
for( i = 0; i < numElements; i++ )
{
accumulator += getElement( i );
}
//Divide by number of elements
if( numElements != 0 )
{
accumulator /= numElements;
}
else
{
accumulator = 0;
}
return accumulator;
}
//Returns the current size of the buffer
uint16_t CircularBuffer::recordLength( void )
{
return cBufferElementsUsed;
}
@@ -0,0 +1,33 @@
#ifndef CIRCULARBUFFER_H
#define CIRCULARBUFFER_H
#include <stdint.h>
//****************************************************************************//
//
// Circular Buffer
//
//****************************************************************************//
//Class CircularBuffer is int16_t
//Does not care about over-running real data ( if request is outside length's bounds ).
//For example, the underlying machine writes [48], [49], [0], [1] ...
class CircularBuffer
{
public:
CircularBuffer( uint16_t inputSize );
~CircularBuffer();
float getElement( uint16_t ); //zero is the push location
void pushElement( float );
float averageLast( uint16_t );
uint16_t recordLength( void );
private:
uint16_t cBufferSize;
float *cBufferData;
int16_t cBufferLastPtr;
uint8_t cBufferElementsUsed;
};
#endif // CIRCULARBUFFER_H
@@ -0,0 +1,181 @@
/******************************************************************************
LCD_DeltaAltitude.ino
BME280 Arduino and Teensy example
Marshall Taylor @ SparkFun Electronics
Oct 19, 2015
https://github.com/sparkfun/SparkFun_BME280_Arduino_Library
This sketch configures a BME280 to measure height changes, with a button
to zero.
Hardware connections:
BME280 -> Arduino
GND -> GND
3.3 -> 3.3
SDA -> A4
SCL -> A5
Button (Normally open) between A4 and A5.
LCD:
* LCD RS pin to digital pin 7
* LCD Enable pin to digital 6
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
To use, run the sketch, then press and hold the button until the data on the
serial monitor shows zero. Then, move the sensor to a new altitude and watch
how the output changes!
Note: For most accurate results keep the sensor orientation and temperature the
same between measurements.
Resources:
Uses Wire.h for I2C operation
Uses SPI.h for SPI operation
Included CircularBuffer class for averaging
Development environment specifics:
Arduino IDE 1.6.4
Teensy loader 1.23
This code is released under the [MIT License](http://opensource.org/licenses/MIT).
Please review the LICENSE.md file included with this example. If you have any questions
or concerns with licensing, please contact techsupport@sparkfun.com.
Distributed as-is; no warranty is given.
******************************************************************************/
#include <stdint.h>
#include "SparkFunBME280.h"
#include "CircularBuffer.h"
#include "Wire.h"
#include "SPI.h"
#include <LiquidCrystal.h>
//Global sensor object
BME280 mySensor;
uint8_t buttonGroundPin = A0;
uint8_t buttonSensePin = A2;
CircularBuffer myBuffer(50);
//int icounter = 0;
float reference = 0;
LiquidCrystal lcd(7,6,5,4,3,2);
void setup()
{
//Configure the button IO
pinMode( buttonSensePin, INPUT_PULLUP );
pinMode( buttonGroundPin, OUTPUT );
digitalWrite( buttonGroundPin, 0 );
//***Driver settings********************************//
//commInterface can be I2C_MODE or SPI_MODE
//specify chipSelectPin using arduino pin names
//specify I2C address. Can be 0x77(default) or 0x76
//For I2C, enable the following and disable the SPI section
mySensor.settings.commInterface = I2C_MODE;
mySensor.settings.I2CAddress = 0x77;
//For SPI enable the following and dissable the I2C section
//mySensor.settings.commInterface = SPI_MODE;
//mySensor.settings.chipSelectPin = 10;
//***Operation settings*****************************//
//renMode can be:
// 0, Sleep mode
// 1 or 2, Forced mode
// 3, Normal mode
mySensor.settings.runMode = 3; //Forced mode
//tStandby can be:
// 0, 0.5ms
// 1, 62.5ms
// 2, 125ms
// 3, 250ms
// 4, 500ms
// 5, 1000ms
// 6, 10ms
// 7, 20ms
mySensor.settings.tStandby = 0;
//filter can be off or number of FIR coefficients to use:
// 0, filter off
// 1, coefficients = 2
// 2, coefficients = 4
// 3, coefficients = 8
// 4, coefficients = 16
mySensor.settings.filter = 4;
//tempOverSample can be:
// 0, skipped
// 1 through 5, oversampling *1, *2, *4, *8, *16 respectively
mySensor.settings.tempOverSample = 5;
//pressOverSample can be:
// 0, skipped
// 1 through 5, oversampling *1, *2, *4, *8, *16 respectively
mySensor.settings.pressOverSample = 5;
//humidOverSample can be:
// 0, skipped
// 1 through 5, oversampling *1, *2, *4, *8, *16 respectively
mySensor.settings.humidOverSample = 0;
Serial.begin(57600);
Serial.print("Program Started\n");
Serial.print("Starting BME280... result of .begin(): 0x");
//Calling .begin() causes the settings to be loaded
delay(10);//Needs > 2 ms to start up
Serial.println(mySensor.begin(), HEX);
Serial.println();
lcd.begin(16, 2);
lcd.clear();
}
void loop()
{
//Get the local temperature! Do this for calibration
mySensor.readTempC();
//Check button. No debounce, so hold for operation
if( digitalRead( buttonSensePin ) == 0 )
{
//Set reference altitude.
reference = mySensor.readFloatAltitudeFeet();
}
float lastAlt = mySensor.readFloatAltitudeFeet() - reference;
myBuffer.pushElement(lastAlt);
float tempAlt = myBuffer.averageLast(8); //Do an average of the latest samples
lcd.setCursor(0,0);
lcd.print("Altitude: ");
myBuffer.pushElement(lastAlt);
lcd.setCursor(0,1);
if(tempAlt >= 0)
{
lcd.print(" ");
}
lcd.print(tempAlt);
lcd.print(" ft ");
delay(200);
}
@@ -0,0 +1,230 @@
/*BME280 Arduino and Teensy example
Marshall Taylor @ SparkFun Electronics
Oct 19, 2015
https://github.com/sparkfun/SparkFun_BME280_Arduino_Library
This sketch displays the pressure in pascals and temperature
in fahrenheit.
Resources:
Uses Wire.h for I2C operation
Uses SPI.h for SPI operation
Hardware connections:
BME280 -> Arduino
GND -> GND
3.3 -> 3.3
SDA -> A4
SCL -> A5
LCD:
* LCD RS pin to digital pin 7
* LCD Enable pin to digital 6
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
Development environment specifics:
Arduino IDE 1.6.4
Teensy loader 1.23
This code is released under the [MIT License](http://opensource.org/licenses/MIT).
Please review the LICENSE.md file included with this example. If you have any questions
or concerns with licensing, please contact techsupport@sparkfun.com.
Distributed as-is; no warranty is given.
******************************************************************************/
// Load the LiquidCrystal library, which will give us
// commands to interface to the LCD:
#include <LiquidCrystal.h>
// Initialize the library with the pins we're using.
// (Note that you can use different pins if needed.)
// See http://arduino.cc/en/Reference/LiquidCrystal
// for more information:
#include <stdint.h>
#include "SparkFunBME280.h"
//Library allows either I2C or SPI, so include both.
#include "Wire.h"
#include "SPI.h"
//Global sensor object
BME280 mySensor;
LiquidCrystal lcd(7,6,5,4,3,2);
void setup()
{
lcd.begin(16, 2);
lcd.clear();
//***Driver settings********************************//
//commInterface can be I2C_MODE or SPI_MODE
//specify chipSelectPin using arduino pin names
//specify I2C address. Can be 0x77(default) or 0x76
//For I2C, enable the following and disable the SPI section
mySensor.settings.commInterface = I2C_MODE;
mySensor.settings.I2CAddress = 0x77;
//For SPI enable the following and dissable the I2C section
//mySensor.settings.commInterface = SPI_MODE;
//mySensor.settings.chipSelectPin = 10;
//***Operation settings*****************************//
//renMode can be:
// 0, Sleep mode
// 1 or 2, Forced mode
// 3, Normal mode
mySensor.settings.runMode = 3; //Forced mode
//tStandby can be:
// 0, 0.5ms
// 1, 62.5ms
// 2, 125ms
// 3, 250ms
// 4, 500ms
// 5, 1000ms
// 6, 10ms
// 7, 20ms
mySensor.settings.tStandby = 0;
//filter can be off or number of FIR coefficients to use:
// 0, filter off
// 1, coefficients = 2
// 2, coefficients = 4
// 3, coefficients = 8
// 4, coefficients = 16
mySensor.settings.filter = 0;
//tempOverSample can be:
// 0, skipped
// 1 through 5, oversampling *1, *2, *4, *8, *16 respectively
mySensor.settings.tempOverSample = 1;
//pressOverSample can be:
// 0, skipped
// 1 through 5, oversampling *1, *2, *4, *8, *16 respectively
mySensor.settings.pressOverSample = 1;
//humidOverSample can be:
// 0, skipped
// 1 through 5, oversampling *1, *2, *4, *8, *16 respectively
mySensor.settings.humidOverSample = 1;
Serial.begin(57600);
Serial.print("Program Started\n");
Serial.print("Starting BME280... result of .begin(): 0x");
//Calling .begin() causes the settings to be loaded
Serial.println(mySensor.begin(), HEX);
delay(10); //Use delay if reading directly after .begin is called. BME280 requires 2ms to start up.
Serial.print("Displaying ID, reset and ctrl regs\n");
Serial.print("ID(0xD0): 0x");
Serial.println(mySensor.readRegister(BME280_CHIP_ID_REG), HEX);
Serial.print("Reset register(0xE0): 0x");
Serial.println(mySensor.readRegister(BME280_RST_REG), HEX);
Serial.print("ctrl_meas(0xF4): 0x");
Serial.println(mySensor.readRegister(BME280_CTRL_MEAS_REG), HEX);
Serial.print("ctrl_hum(0xF2): 0x");
Serial.println(mySensor.readRegister(BME280_CTRL_HUMIDITY_REG), HEX);
Serial.print("\n\n");
Serial.print("Displaying all regs\n");
uint8_t memCounter = 0x80;
uint8_t tempReadData;
for(int rowi = 8; rowi < 16; rowi++ )
{
Serial.print("0x");
Serial.print(rowi, HEX);
Serial.print("0:");
for(int coli = 0; coli < 16; coli++ )
{
tempReadData = mySensor.readRegister(memCounter);
Serial.print((tempReadData >> 4) & 0x0F, HEX);//Print first hex nibble
Serial.print(tempReadData & 0x0F, HEX);//Print second hex nibble
Serial.print(" ");
memCounter++;
}
Serial.print("\n");
}
Serial.print("\n\n");
Serial.print("Displaying concatenated calibration words\n");
Serial.print("dig_T1, uint16: ");
Serial.println(mySensor.calibration.dig_T1);
Serial.print("dig_T2, int16: ");
Serial.println(mySensor.calibration.dig_T2);
Serial.print("dig_T3, int16: ");
Serial.println(mySensor.calibration.dig_T3);
Serial.print("dig_P1, uint16: ");
Serial.println(mySensor.calibration.dig_P1);
Serial.print("dig_P2, int16: ");
Serial.println(mySensor.calibration.dig_P2);
Serial.print("dig_P3, int16: ");
Serial.println(mySensor.calibration.dig_P3);
Serial.print("dig_P4, int16: ");
Serial.println(mySensor.calibration.dig_P4);
Serial.print("dig_P5, int16: ");
Serial.println(mySensor.calibration.dig_P5);
Serial.print("dig_P6, int16: ");
Serial.println(mySensor.calibration.dig_P6);
Serial.print("dig_P7, int16: ");
Serial.println(mySensor.calibration.dig_P7);
Serial.print("dig_P8, int16: ");
Serial.println(mySensor.calibration.dig_P8);
Serial.print("dig_P9, int16: ");
Serial.println(mySensor.calibration.dig_P9);
Serial.print("dig_H1, uint8: ");
Serial.println(mySensor.calibration.dig_H1);
Serial.print("dig_H2, int16: ");
Serial.println(mySensor.calibration.dig_H2);
Serial.print("dig_H3, uint8: ");
Serial.println(mySensor.calibration.dig_H3);
Serial.print("dig_H4, int16: ");
Serial.println(mySensor.calibration.dig_H4);
Serial.print("dig_H5, int16: ");
Serial.println(mySensor.calibration.dig_H5);
Serial.print("dig_H6, uint8: ");
Serial.println(mySensor.calibration.dig_H6);
Serial.println();
}
void loop()
{
//Get the local temperature! Do this for calibration
mySensor.readTempC();
lcd.setCursor(0,0);
lcd.print("Press: ");
lcd.print((uint32_t)mySensor.readFloatPressure());
lcd.print(" Pa");
lcd.setCursor(0,1);
lcd.print("Humid: ");
lcd.print((uint8_t)mySensor.readFloatHumidity());
lcd.print(" %");
delay(1000);
}