2015年9月20日 星期日

BMP180+DS18x20+DCcduino_LCD2004A

DCcduino  這塊山寨版UNO剛好滿足我不喜歡用麵包板的特性,擴充各兩組5V及3.3V的接點
這次連接I2C LCD 及BMP180和DS18B20,沒有須增加電源的問題。


















#include "SFE_BMP180.h"
#include "Wire.h"
#include "LiquidCrystal_I2C.h"
#include "OneWire.h"
#include "DallasTemperature.h"

SFE_BMP180 pressure;

#define ALTITUDE 1655.0

LiquidCrystal_I2C lcd(0x3f,20,4);

#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

void setup()
{
  lcd.init();
  lcd.backlight();
  lcd.print("BMP180 Demo");

  Serial.begin(9600);
  Serial.println("REBOOT");

  if (pressure.begin())
    Serial.println("BMP180 init success");
  else
  {

    Serial.println("BMP180 init fail\n\n");
    while(1); // Pause forever.
  }

sensors.begin();

}



void loop()
{
  char status;
  double T,P,p0,a;

  Serial.println();
  Serial.print("provided altitude: ");
  Serial.print(ALTITUDE,0);
  Serial.print(" meters, ");
  Serial.print(ALTITUDE*3.28084,0);
  Serial.println(" feet");

  status = pressure.startTemperature();
  if (status != 0)
  {
   
    delay(status);

    status = pressure.getTemperature(T);
    if (status != 0)
    {

      Serial.print("temperature: ");
      Serial.print(T,2);
      Serial.print(" deg C, ");
      Serial.print((9.0/5.0)*T+32.0,2);
      Serial.println(" deg F");
   
      status = pressure.startPressure(3);
      if (status != 0)
      {

        delay(status);

        status = pressure.getPressure(P,T);
        if (status != 0)
        {
     
          Serial.print("absolute pressure: ");
          Serial.print(P,2);
          Serial.print(" mb, ");
          Serial.print(P*0.0295333727,2);
          Serial.println(" inHg");
     
          p0 = pressure.sealevel(P,ALTITUDE);
          Serial.print("relative (sea-level) pressure: ");
          Serial.print(p0,2);
          Serial.print(" mb, ");
          Serial.print(p0*0.0295333727,2);
          Serial.println(" inHg");

          a = pressure.altitude(P,p0);
          Serial.print("computed altitude: ");
          Serial.print(a,0);
          Serial.print(" meters, ");
          Serial.print(a*3.28084,0);
          Serial.println(" feet");
        }
        else Serial.println("error retrieving pressure measurement\n");
      }
      else Serial.println("error starting pressure measurement\n");
    }
    else Serial.println("error retrieving temperature measurement\n");
  }
  else Serial.println("error starting temperature measurement\n");

 sensors.requestTemperatures();

  lcd.setCursor(0,1);
  lcd.print(T,2);
  lcd.print("C ");
  lcd.print(P,2);
  lcd.print("mb ");
  lcd.setCursor(0,2);
  lcd.print(a,0);
  lcd.print("M ");
  lcd.print(p0,2);
  lcd.print("mb ");
  lcd.setCursor(0,3);
  lcd.print(sensors.getTempCByIndex(0));
  lcd.print("C ");
  lcd.print(sensors.getTempCByIndex(1));
  lcd.print("C ");

  Serial.print("Ds18s20");
  Serial.print(",");
   Serial.print(sensors.getTempCByIndex(0));
  Serial.print(",");
   Serial.print("Ds18b20");
  Serial.print(",");
  Serial.print(sensors.getTempCByIndex(1));
  Serial.println(",");

   
      // Serial.print("temperature: ");
     // Serial.print(T,2);
     // Serial.print(" deg C, ");
     // Serial.print((9.0/5.0)*T+32.0,2);
     // Serial.println(" deg F");

  delay(2000);
}

沒有留言:

張貼留言