// BMP085_test2
// by Filipe Vieira
// Simple test of BMP085 output using default settings using dynamic update.
// In order to take full advantage of dynamic measurement, automatic temperature updates must be disabled.
// To disable automatic updates set AUTO_UPDATE_TEMPERATURE to false in bmp085.h.
// IMPORTANT!! To get correct values you MUST CHANGE init() parameters, in 
// this example I've set 250m based on GPS data for my location.
#define CYT

#include <Wire.h>
#include <SPI.h>
#include <PT6955.h>
#include <BMP085.h>

BMP085 dps = BMP085();      // Digital Pressure Sensor 

long Pressure = 0, Altitude = 0, Temperature = 0, tmp = 0;
unsigned long time1=0;

PT6955 LED = PT6955();

void setup(void) {
  LED.init(4);
  LED.clearLED();
  Serial.begin(9600);
  pinMode(10, OUTPUT); //  Set SPI pins to be outputs
  pinMode(11, OUTPUT);
  pinMode(13, OUTPUT);
  Wire.begin();
  delay(1000);
  
  // uncomment for different initialization settings
  //dps.init();     // QFE (Field Elevation above ground level) is set to 0 meters.
                  // same as init(MODE_STANDARD, 0, true);
  
  //dps.init(MODE_STANDARD, 101850, false);  // 101850Pa = 1018.50hPa, false = using Pa units
                  // this initialization is useful for normalizing pressure to specific datum.
                  // OR setting current local hPa information from a weather station/local airport (QNH).
  dps.init(MODE_ULTRA_HIGHRES, 0, true); 
  //dps.init(MODE_ULTRA_HIGHRES, 15290, true);  // 250 meters, true = using meter units
                  // this initialization is useful if current altitude is known,
                  // pressure will be calculated based on TruePressure and known altitude.

  // note: use zeroCal only after initialization.
  //dps.zeroCal(101300, 0);    // set zero point
}            

int ch = 0;
void loop(void) {  
  // calculate temperature every 1 sec (dynamic measurement)
  // NOTE: in order to take full advantage of dynamic measurement set AUTO_UPDATE_TEMPERATURE to false in bmp085.h
  /*if (((millis() - time1)/1000.0) >= 1.0) {     
     dps.calcTrueTemperature();
     time1 = millis();      
  }*/
  Pressure=0;
  Altitude=0;
  Temperature=0;
#ifdef CYT
  for(int i = 0; i < 10; i++)
  {
#endif
  dps.getPressure(&tmp);
  Pressure=Pressure+tmp;
  dps.getAltitude(&tmp);
  Altitude=Altitude+tmp;
  dps.getTemperature(&tmp);
  Temperature=Temperature+tmp;
#ifdef CYT
  }
  Pressure=Pressure/10;
  Altitude=Altitude/10;
  Temperature=Temperature/10;
#endif
  
  Serial.print("  Alt(m):");
  Serial.print(Altitude/100.0);
  if(ch > 0 && ch <= 10)
  {
    LED.writecolLED1();
    LED.updateLED2();
    LED.printdigit(Altitude/100);
  }
  if(ch > 10 && ch <= 20)
  {
    LED.writecolLED2();
    LED.updateLED2();
    LED.printdigit(Pressure/133.322368);
  }
  if(ch > 20 && ch <= 30)
  {
    LED.writecolLED3(); 
    LED.updateLED2();
    LED.printdigit(Temperature/10);
  }
  Serial.print("  Pressure:");
  Serial.print(Pressure/133.322368);
  Serial.print("  Temperature:");
  Serial.println(Temperature/10.0);
  if(ch > 30)
  {
   ch=0; 
  }
  ch++;
}

