I"m using a Adafruit BMP183 SPI Altitude Sensor in a arduino project. The sensor works when I run the example program and gets correct results, but when I use the same code in my program I get weird values for altitude, i get varying negative values.
/*00000000 Libraries included 00000000*/
#include <SerialGraphicLCD.h>
#include <SoftwareSerial.h>
#include "DHT.h"
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP183.h>
#include <DS3234.h>
/*00000000 GLCD screen size 00000000*/
#define maxX 127//159
#define maxY 63 //127
/*00000000 BMPsensor 00000000*/
#define BMP183_CLK 13 // AKA CLK
#define BMP183_SDO 12 // AKA MISO
#define BMP183_SDI 11 // AKA MOSI
#define BMP183_CS 4
Adafruit_BMP183 bmp = Adafruit_BMP183(BMP183_CLK, BMP183_SDO, BMP183_SDI, BMP183_CS);
volatile float pressure_mmHg;
volatile float pressurepascal;
volatile float Altitude;
volatile float last_microsBMP;
float seaLevelPressure;
/*00000000 DeadOn time chip 00000000*/
volatile int TimeDate [7]; //second,minute,hour,null,day,month,year
const int CSpin=10;
const int MOSIpin=11;
const int MISOpin=12;
const int CLKpin=13;
float last_microsTime;
/*00000000 DHT sensor stuff 00000000*/
#define DHTPIN 9 // what pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE);
volatile float humidity=0;
volatile float airtemp=0;
volatile float heatindex=0;
volatile float last_microsDHT;
/*00000000 variables for PINS 00000000*/
//Bup Bdown Bleft Bright Bcenter But1 But2 But3
//const int ledPin13 = 13; // the number of the LED pin
/*00000000 variables for debounce 00000000*/
long debouncing_time = 250; //Debouncing delay coefficent
long last_micros;
int state = LOW;
/*00000000 variables for menus 00000000*/
/*00000000 LCD class declaration 00000000*/
LCD LCD;
/*00000000 CoSensor declaration 00000000*/
volatile int CoCode;
volatile int CoLevel;
float last_microsCO;
/*00000000 SETUP 00000000*/
void setup()
{
pinMode(5, INPUT_PULLUP); //button on pin 5
pinMode(6, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
Serial.begin(9600);
attachInterrupt(0, debounce, FALLING); //interrupt
// pinMode(ledPin13, OUTPUT); // pin 13 on board LED
RTC.configure(MOSIpin,MISOpin,CLKpin,CSpin);
// RTC.setDateTime(18,9,2014,23,28,0); // RTC.setDateTime(int d, int mo, int y, int h, int mi, int s);
dht.begin();
bmp.begin();
//RTC.setDateTime(24, 10, 2014, 23, 17, 0); // int d, int mo, int y, int h, int mi, int s
//splash screen
LCD.setBacklight(20);
LCD.clearScreen();
LCD.setHome();
LCD.printStr(" Program starts in "); //displays "program is starting in 5/4/3/2/1 etc"
for(int i = 5; i >= 0; i--)
{
LCD.setX(113);
LCD.setY(0);
LCD.printNum(i);
delay(500);
}
LCD.clearScreen();
delay(200);
}
/*00000000 LOOP 00000000*/
void loop(void)
{
Serial.print("\n");
Serial.print("Altitude: ");
Serial.print(bmp.getAltitude(seaLevelPressure));
Serial.println(" m");
Serial.println("");
/*Serial.print("\n");
Serial.print(Altitude);
Serial.println(" m");
Serial.println("");
*/
delay(50);
SensorUpdate(); //update all sensors including time
MainMenuDisplay();
BOXSELECTOR();
//menu_select();
//sensor_menu();
//time_menu();
}
void SensorUpdate()
{
Battery();
delay(100);
ClockRead();
delay(100);
DHTsensor();
delay(100);
CoSensor();
delay(100);
BMPsensor();
delay(100);
}
/*00000000 debounce functions 00000000*/
void debounce()
{
if(((long)(micros() - last_micros) >= debouncing_time * 2000) || last_micros==0)
{
state = !state;
//digitalWrite(13, state);
last_micros = micros();
//buttontest();
}
}
void buttontest()
{
if(digitalRead(5)==LOW)
{sensor_menu();}
if(digitalRead(6)==LOW)
{time_menu();}
if(digitalRead(7)==LOW)
{/*multimeter();*/}
if(digitalRead(8)==LOW)
{/*sleep();*/}
if(digitalRead(9)==LOW)
{MainMenuDisplay();}
if(digitalRead(10)==LOW)
{/*BUTTON10=true;*/}
}
void MainMenuDisplay()
{
optionrowdisplay(); //display info on glcd
TimeDisplay(0,0,0,0); //Display Time on glcd
Menubuttons_MenuOptions();
}
void BOXSELECTOR()
{
LCD.drawBox(4,53,0,47,0);//x1,y1,x2,y2 draw box on S Sensor
LCD.drawBox(3,52,1,48,0);
LCD.drawBox(2,51,2,49,0);
delay(50);
}
void CoSensor()
{
if((long)micros() - last_microsCO >= 100000 || last_microsCO==0) // Wait a few seconds between measurements. //delay(1000);
{
CoLevel = analogRead(A5);// 10k resistor noramlizes in basement at 70
if(CoLevel<300)
{CoCode=1;}
else if(CoLevel>301 && CoLevel<800)
{CoCode=2;}
else if(CoLevel>801)
{CoCode=3;}
}
last_microsCO=micros();
}
void Battery()
{
}
void ClockRead()
{
RTC.readDateTime(); //DD.MM.YYYY-hh.mm.ss
/*
RTC.time_h(); // hour
RTC.time_m(); // minutes
RTC.time_s(); // seconds
RTC.date_d(); // day
RTC.date_m(); // month
RTC.date_y(); // year
RTC.readTemp();
RTC.setDateTime(int d, int mo, int y, int h, int mi, int s);
*/
TimeDate[2]=RTC.time_h(); //2.1.0 hour minute second
TimeDate[1]=RTC.time_m();
TimeDate[0]=RTC.time_s();
TimeDate[5]=RTC.date_d(); // day
TimeDate[4]=RTC.date_m(); // month
TimeDate[3]=RTC.date_y()-2000; // year
}
/*0000000000000000000 BMPsensor 00000000000000000000000000000*/
void BMPsensor()
{
if((long)micros() - last_microsBMP >= 100500 || last_microsBMP==0) // Wait a few seconds between measurements. //delay(2050);
{
/* Display atmospheric pressue in Pascals */
//float pressurepascal;
//float pressure_mmHg;
pressurepascal=bmp.getPressure();
pressure_mmHg=pressurepascal*(0.0002953);
// Pressure bmp.getPressure() Pascals
// Pressure (bmp.getPressure() / 100) millibar (hPa)
// Pressure (pressure_mmHg); millimeter Mercury (mmHg)
/* First we get the current temperature from the BMP085 */
float temperatureF;
float temperatureC;
temperatureC =bmp.getTemperature();
temperatureF=(temperatureC*(1.8))+32; // F = C * 180 + 32
float seaLevelPressure = SENSORS_PRESSURE_SEALEVELHPA; // should be ~1000
seaLevelPressure=1011;
//SENSORS_PRESSURE_SEALEVELHPA millibar/hPa
Altitude=bmp.getAltitude(seaLevelPressure); // meters
}
last_microsBMP=micros();
}
/*0000000000000000000 DHTsensor 00000000000000000000000000000*/
void DHTsensor()
{
if((long)micros() - last_microsDHT >= 100100 || last_microsDHT==0) // Wait a few seconds between measurements. //delay(2100);
{
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
humidity=dht.readHumidity();
// Read temperature as Celsius
float t = dht.readTemperature();
// Read temperature as Fahrenheit
float f = dht.readTemperature(true);
airtemp=dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f))
{
Serial.println("Failed to read from DHT sensor!");
//last_micros2=micros();
return;
}
// Compute heat index
// Must send in temp in Fahrenheit!
float hi = dht.computeHeatIndex(f, h);
heatindex=dht.computeHeatIndex(f, h);
}
last_microsDHT=micros();
}
GLCD_menu_with_interrupts_V19_SPI_no_menus.ino (7.08 KB)
wrist_libraries.ino (9.61 KB)