1820达拉斯温度传感器,返回错误值

时间:2015-04-15 16:37:44

标签: arduino

当我运行一段仅从临时传感器收集值的代码时,它返回正确的值。 当我将这个功能集成到更大的代码中时,它会不断返回-127度。

#include <OneWire.h>
#include <DallasTemperature.h>
#include <SPP.h>  
#include <LiquidCrystal.h> 

#define ONE_WIRE_BUS 9
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature TempSensor(&oneWire);

USB Usb; // create usb 
BTD Btd(&Usb); // create bluetooth  dongle instance
SPP SerialBT(&Btd, "Arduino", "0000"); // set device name and pin 
LiquidCrystal lcd(7, 6, 5, 4, 3, 2); // create lcd instance using declared pins
int LightPin = A0;  // pin connected to light sensor
int LcdType = 0;  // variable for lcd display

int state; // variable for storing incoming data
int LDRvalue = 0; // value from light sensor
int totalLdr = 0;
int totalLdrNums = 0;
String output = ""; // output string for light sensor
String output2 = ""; // output string for temp sensor
int tempvalue = 0; // value from temp sensor 
int totalTemp = 0;
int totalTempNums = 0;
float celsius = 0; // temp in celsius

void setup() {
  Serial.begin(9600); // Begin serial comms at speed 9600
  if (Usb.Init() == -1) { // if usb hasn't been initilsed
    while(1); //wait
  }
  lcd.begin(16, 2); // set up lcd with 16 coloums and 2 rows
  pinMode(LightPin, INPUT); // declare light pin as input 
  printStart(); // run printStart method
  LcdType = 1; // set lcd display type to 1
  TempSensor.begin();
 }

void loop() {
  Usb.Task(); // polls connected devices for status

  if(SerialBT.connected) {  // if BT connected 
    if(SerialBT.available() > 0){ // and BT is available 
    state = SerialBT.read() - '0'; // read incoming value and turn into   regular int (0 - 9 in ascii is 48 - 57)


 if(state == 0){ // if value read is zero 
   checkLightValue(); // run checkLightValue method 
    SerialBT.write(output[0]); // send first value in string 
    delay(50); // delay before sending next value
    SerialBT.write(output[1]); // send second value
    delay(50); // delay before sending next value
    SerialBT.write(output[2]); // send third value 

   }


   if(state == 1){ // if value read is 1
     checkTempValue();
     SerialBT.write(output2[0]); // send first value
     delay(50); // delay before sending next value 
     SerialBT.write(output2[1]); // send second value
     delay(50); // delay before sending next value 
     SerialBT.write(output2[3]); // send third value
     delay(50); // delay before sending next value 
     SerialBT.write(output2[5]); // send fourth value
     delay(50); // delay before sending next value   
   }


   if(state == 2){
    //Restart();
    SerialBT.disconnect();
   }

   if(state == 3){
    LcdType = 1; 
   }

   if(state == 4){
    LcdType = 2; 
   }

   if(state == 5){
     LcdType = 3;
   }

   if(LcdType == 1){ // if lcd type variable = 1
     printLcd();} // run printLcd method

   if (LcdType == 2){
     printLcd2();
   }

   if(LcdType == 3){
     printLcd3();
   }

} 
}

}

  //void (*resetFunc)(void) = 0;

  void checkLightValue(){
    output = ""; // reset output string to nothing  
    LDRvalue = analogRead(LightPin);  // read in light value
    totalLdr = totalLdr + LDRvalue;
    totalLdrNums = totalLdrNums + 1;
    int LDR1 = LDRvalue / 100; // get int 1 from value 
    int twod = LDRvalue - (LDR1 * 100);  // get int 2 and 3 from value
    int LDR2 = twod / 10; // get int 2 from value
    int LDR3 = twod - (LDR2 * 10);  // get int 3 from value
    output += LDR1; //
    output += LDR2; //
    output += LDR3; // appends values to string  
 }


void checkTempValue(){

  output2 = ""; // reset output string to nothing
  TempSensor.requestTemperatures();
  Serial.println(TempSensor.getTempCByIndex(0));
  celsius = TempSensor.getTempCByIndex(0);
  totalTemp = totalTemp + celsius;
  totalTempNums = totalTempNums + 1;
  int c1 = celsius /  100; // get first int from value
  int twod = celsius - (c1 * 100); // 
  int c2 = twod / 10;
  int c3 = twod - (c2 *10);
  output2 += c1; //
  output2 += c2; // 
  output2 += c3; // add values to string to send

}

void printLcd(){
  lcd.clear(); // clear lcd 
  lcd.print("LDR : "); lcd.print(LDRvalue); // display current ldr value
  lcd.setCursor(0,1); // set cursor to second row
  lcd.print("Temp : "); lcd.print(celsius); // display current temp value on row 2
}

void printLcd2(){
  int Averagevalue = totalLdr / totalLdrNums;

  lcd.clear();
  lcd.print("Average light :");
  lcd.setCursor(0,1);
  lcd.print(Averagevalue);
}

void printLcd3(){
  int Averagevalue = totalTemp / totalTempNums;

  lcd.clear();
  lcd.print("Average temp :");
  lcd.setCursor(0,1);
  lcd.print(Averagevalue);
}


void printStart(){
  lcd.clear(); // clear lcd 
  lcd.setCursor(1,1); // set cursor on second row 
  lcd.print("Waiting for a Connection ?"); // print message
  for(int x=1; x<16; x++) { // do following 15 times
    lcd.setCursor(x,0); // set cursor to first row
    lcd.print("Arduino Started"); // print message // doing this as lcd scrolls means the top line (message) appears static while the bottom line (message) scrolls
    lcd.scrollDisplayLeft(); // scroll display left
    delay(250); // delay before moving again
    }

 }

0 个答案:

没有答案