/branches/V0.82b-Arthur-P/MS561101BA/examples/MS561101BA_altitude/MS561101BA_altitude.pde |
---|
0,0 → 1,90 |
/* |
MS561101BA_altitude.pde - Computes altitude from sea level using pressure readings from the sensor. |
The algorithm uses the Hypsometric formula as explained in http://keisan.casio.com/has10/SpecExec.cgi?path=06000000.Science%2F02100100.Earth%20science%2F12000300.Altitude%20from%20atmospheric%20pressure%2Fdefault.xml&charset=utf-8 |
TODO: The results are not that good. Suggestions on improvements welcome. |
Copyright (C) 2011 Fabio Varesano <fvaresano@yahoo.it> |
Development of this code has been supported by the Department of Computer Science, |
Universita' degli Studi di Torino, Italy within the Piemonte Project |
http://www.piemonte.di.unito.it/ |
This program is free software: you can redistribute it and/or modify |
it under the terms of the version 3 GNU General Public License as |
published by the Free Software Foundation. |
This program is distributed in the hope that it will be useful, |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
GNU General Public License for more details. |
You should have received a copy of the GNU General Public License |
along with this program. If not, see <http://www.gnu.org/licenses/>. |
*/ |
//#define DEBUG_V |
#include <Wire.h> |
//#include <DebugUtils.h> |
#include <MS561101BA.h> |
#define MOVAVG_SIZE 32 |
MS561101BA baro = MS561101BA(); |
float movavg_buff[MOVAVG_SIZE]; |
int movavg_i=0; |
const float sea_press = 1013.25; |
float press, temp; |
void setup() { |
Wire.begin(); |
Serial.begin(115200); |
delay(1000); |
// Suppose that the CSB pin is connected to GND. |
// You'll have to check this on your breakout schematics |
baro.init(MS561101BA_ADDR_CSB_LOW); |
delay(100); |
// populate movavg_buff before starting loop |
for(int i=0; i<MOVAVG_SIZE; i++) { |
movavg_buff[i] = baro.getPressure(MS561101BA_OSR_4096); |
} |
} |
void loop() { |
Serial.print(" temp: "); |
temp = baro.getTemperature(MS561101BA_OSR_4096); |
Serial.print(temp); |
Serial.print(" degC pres: "); |
pushAvg(baro.getPressure(MS561101BA_OSR_4096)); |
press = getAvg(movavg_buff, MOVAVG_SIZE); |
Serial.print(press); |
Serial.print(" mbar altitude: "); |
Serial.print(getAltitude(press, temp)); |
Serial.println(" m"); |
delay(100); |
} |
float getAltitude(float press, float temp) { |
return ((pow((sea_press / press), 1/5.257) - 1.0) * (temp + 273.15)) / 0.0065; |
} |
void pushAvg(float val) { |
movavg_buff[movavg_i] = val; |
movavg_i = (movavg_i + 1) % MOVAVG_SIZE; |
} |
float getAvg(float * buff, int size) { |
float sum = 0.0; |
for(int i=0; i<size; i++) { |
sum += buff[i]; |
} |
return sum / size; |
} |
/branches/V0.82b-Arthur-P/MS561101BA/examples/MS561101BA_movaverage/MS561101BA_movaverage.pde |
---|
0,0 → 1,77 |
/* |
MS561101BA_movaverage.pde - Reads from the sensors and outputs a moving averaged pressure reading |
Copyright (C) 2011 Fabio Varesano <fvaresano@yahoo.it> |
Development of this code has been supported by the Department of Computer Science, |
Universita' degli Studi di Torino, Italy within the Piemonte Project |
http://www.piemonte.di.unito.it/ |
This program is free software: you can redistribute it and/or modify |
it under the terms of the version 3 GNU General Public License as |
published by the Free Software Foundation. |
This program is distributed in the hope that it will be useful, |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
GNU General Public License for more details. |
You should have received a copy of the GNU General Public License |
along with this program. If not, see <http://www.gnu.org/licenses/>. |
*/ |
//#define DEBUG_V |
#include <Wire.h> |
//#include <DebugUtils.h> |
#include <MS561101BA.h> |
#define MOVAVG_SIZE 32 |
MS561101BA baro = MS561101BA(); |
float movavg_buff[MOVAVG_SIZE]; |
int movavg_i=0; |
void setup() { |
Wire.begin(); |
Serial.begin(9600); |
delay(1000); |
// Suppose that the CSB pin is connected to GND. |
// You'll have to check this on your breakout schematics |
baro.init(MS561101BA_ADDR_CSB_LOW); |
delay(100); |
// populate movavg_buff before starting loop |
for(int i=0; i<MOVAVG_SIZE; i++) { |
movavg_buff[i] = baro.getPressure(MS561101BA_OSR_4096); |
} |
} |
void loop() { |
Serial.print(" temp: "); |
Serial.print(baro.getTemperature(MS561101BA_OSR_4096)); |
Serial.print(" degC pres: "); |
pushAvg(baro.getPressure(MS561101BA_OSR_4096)); |
Serial.print(getAvg(movavg_buff, MOVAVG_SIZE)); |
Serial.println(" mbar"); |
} |
void pushAvg(float val) { |
movavg_buff[movavg_i] = val; |
movavg_i = (movavg_i + 1) % MOVAVG_SIZE; |
} |
float getAvg(float * buff, int size) { |
float sum = 0.0; |
for(int i=0; i<size; i++) { |
sum += buff[i]; |
} |
return sum / size; |
} |
/branches/V0.82b-Arthur-P/MS561101BA/examples/MS561101BA_regscan/MS561101BA_regscan.pde |
---|
0,0 → 1,41 |
/*HMC5883LRegisterScanner.pde |
**A sketch that attempts to read every register from a slave device |
**Written by Wayne Truchsess http://dsscircuits.com |
*/ |
#include "Wire.h" |
#define I2C 0x77 |
byte x; |
void setup() { |
Wire.begin(); |
Serial.begin(9600); |
delay(1000); |
} |
void loop() { |
readRegisters(); |
} |
void readRegisters() { |
for(int l = 0x00; l < 256; l++){ |
Wire.beginTransmission(I2C); |
Wire.send(l); |
Wire.endTransmission(); |
//delay(100); |
Wire.beginTransmission(I2C); |
Wire.requestFrom(I2C,1); |
x = Wire.receive(); |
Serial.print("Register Address "); |
Serial.print(l,DEC); |
Serial.print("_"); |
Serial.print(l,HEX); |
Serial.print(" = "); |
Serial.print(x,BIN); |
Serial.print(" = "); |
Serial.print(x,DEC); |
Serial.println(" "); |
Wire.endTransmission(); |
} |
} |
/branches/V0.82b-Arthur-P/MS561101BA/examples/MS561101BA_simple/MS561101BA_simple.pde |
---|
0,0 → 1,51 |
/* |
MS561101BA_demo.pde - Example code for using the MS561101BA library. |
Displays temperature and pression readings from the sensor |
Copyright (C) 2011 Fabio Varesano <fvaresano@yahoo.it> |
Development of this code has been supported by the Department of Computer Science, |
Universita' degli Studi di Torino, Italy within the Piemonte Project |
http://www.piemonte.di.unito.it/ |
This program is free software: you can redistribute it and/or modify |
it under the terms of the version 3 GNU General Public License as |
published by the Free Software Foundation. |
This program is distributed in the hope that it will be useful, |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
GNU General Public License for more details. |
You should have received a copy of the GNU General Public License |
along with this program. If not, see <http://www.gnu.org/licenses/>. |
*/ |
//#define DEBUG_V |
#include <Wire.h> |
//#include <DebugUtils.h> |
#include <MS561101BA.h> |
MS561101BA baro = MS561101BA(); |
void setup() { |
Wire.begin(); |
Serial.begin(115200); |
delay(1000); |
// Suppose that the CSB pin is connected to GND. |
// You'll have to check this on your breakout schematics |
baro.init(MS561101BA_ADDR_CSB_LOW); |
} |
void loop() { |
Serial.print(" temp: "); |
Serial.print(baro.getTemperature(MS561101BA_OSR_4096)); |
Serial.print(" degC pres: "); |
Serial.print(baro.getPressure(MS561101BA_OSR_4096)); |
Serial.println(" mbar"); |
} |