Error when compiling dht11 and Dallas OneWire

I have created two separate sketches, one to return dht11 temp and humidity to an LCD and one to return 2 dallas onewire to the LCD. When I try to combine the two I get an error message. Can I get some help?

I get the error message "a function-definition is not allowed here before '{' token"

This is the specific code segment that gives the error

{  
void printTemperature(DeviceAddress deviceAddress)
{
float tempC = sensors.getTempC(deviceAddress);
if (tempC == -127.00) {
lcd.print("Error");
} else {
lcd.print(DallasTemperature::toFahrenheit(tempC));
}

I am running Arduino IDE 1.0 on a SMD UNO. The sketches for the DHT11 and the Dallas OneWire work separately, just not together.

//DHT11 set to analog 0 pin
#define DHT11_PIN 0      // define anlog  port 0
#include <LiquidCrystal.h>
#include <OneWire.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS 6

OneWire oneWire(ONE_WIRE_BUS);

DallasTemperature sensors(&oneWire);

DeviceAddress OTA={0x28, 0x94, 0xBB, 0x60, 0x03, 0x00, 0x00, 0x38};
DeviceAddress EP={0x28, 0xFE, 0xF6, 0xB6, 0x03, 0x00, 0x00, 0x39};

LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

byte read_dht11_dat()
{
  byte i = 0;
  byte result=0;
  for(i=0; i< 8; i++)
  {
    while(!(PINC & _BV(DHT11_PIN)))
    {};  // wait  forever until anlog input port 0 is '1'   (NOTICE: PINC reads all the analog input ports 
    //and  _BV(X) is the macro operation which pull up positon 'X'to '1' and the rest positions to '0'. it is equivalent to 1<<X.) 
    delayMicroseconds(30);
    if(PINC & _BV(DHT11_PIN))  //if analog input port 0 is still '1' after 30 us
      result |=(1<<(7-i));     //this position is 1
    while((PINC & _BV(DHT11_PIN)));  // wait '1' finish
    }
    return result;
}


void setup()
{
  // Start up the library
sensors.begin();
// set the resolution to 10 bit (good enough?)
sensors.setResolution(OTA, 10);
sensors.setResolution(EP, 10);
{
lcd.begin(16,2); // columns, rows. use 16,2 for a 16x2 LCD, etc.
lcd.clear(); // start with a blank screen
}


  DDRC |= _BV(DHT11_PIN);   //let analog port 0 be output port 
  PORTC |= _BV(DHT11_PIN);  //let the initial value of this port be '1'
  Serial.begin(9600);
  Serial.println("Ready");
{  
void printTemperature(DeviceAddress deviceAddress)
{
float tempC = sensors.getTempC(deviceAddress);
if (tempC == -127.00) {
lcd.print("Error");
} else {
lcd.print(DallasTemperature::toFahrenheit(tempC));
}
}
}	
void loop()     
{
  byte dht11_dat[5];    
  byte dht11_in;
  byte i;// start condition
	 
  PORTC &= ~_BV(DHT11_PIN);    // 1. pull-down i/o pin for 18ms
  delay(18);
  PORTC |= _BV(DHT11_PIN);     // 2. pull-up i/o pin for 40ms
  delayMicroseconds(1);
  DDRC &= ~_BV(DHT11_PIN);     //let analog port 0 be input port 
  delayMicroseconds(40);      
  
  dht11_in = PINC & _BV(DHT11_PIN);  // read only the input port 0
  if(dht11_in)
  {
    Serial.println("dht11 start condition 1 not met"); // wait for DHT response signal: LOW
    delay(1000);
    return;
  }
  delayMicroseconds(80);
  dht11_in = PINC & _BV(DHT11_PIN); //  
  if(!dht11_in)
  {
    Serial.println("dht11 start condition 2 not met");  //wair for second response signal:HIGH
    return;
  }
  
  delayMicroseconds(80);// now ready for data reception
  for (i=0; i<5; i++)
  {  dht11_dat[i] = read_dht11_dat();}  //recieved 40 bits data. Details are described in datasheet
  
  DDRC |= _BV(DHT11_PIN);      //let analog port 0 be output port after all the data have been received
  PORTC |= _BV(DHT11_PIN);     //let the  value of this port be '1' after all the data have been received
  byte dht11_check_sum = dht11_dat[0]+dht11_dat[1]+dht11_dat[2]+dht11_dat[3];// check check_sum
  if(dht11_dat[4]!= dht11_check_sum)
  {
    Serial.println("DHT11 checksum error");
  }
  Serial.print("Current humdity = ");
  Serial.print(dht11_dat[0], DEC);
  Serial.print(".");
  Serial.print(dht11_dat[1], DEC);
  Serial.print("%  ");
  Serial.print("temperature = ");
  Serial.print(dht11_dat[2], DEC);
  Serial.print(".");
  Serial.print(dht11_dat[3], DEC);
  Serial.println("C  ");
  
  lcd.setCursor(0,0);
  lcd.print("T:");
  lcd.print(dht11_dat[2], DEC);
  lcd.setCursor(5,0);
  lcd.print("H");
lcd.print(dht11_dat[0], DEC);
lcd.print("%");
lcd.setCursor(10,0);
lcd.print("Dew");


sensors.requestTemperatures();
lcd.setCursor(0,1);
lcd.print("OTA");
printTemperature(OTA);
lcd.setCursor(9,1);
lcd.print("EP");
printTemperature(EP);



  delay(1000);  //fresh time
}

I remember having a bit of bother with something similar, though for me i just simplified the process and it seemed to do the trick, call a global temperature reading at the top of your loop

sensors.requestTemperatures();,

then print that to your screen using the

sensors.getTempCByIndex(0)

change the (0) for the number of the device that you want to read

you might have to scan for index numbers though, but didn't seem to affect me.

When I try to combine the two I get an error message]

So are you going to tell us or do we have to guess what the error message is?

What version of the arduino are you running, recent changes mean many older libraries need changing.

Because the code you posted was not in a box it is mangled by the forum software. Modify the post. Remove the code, re paste it in, highlight it all and hit the # icon then save it.
Edit - thanks for doing that :slight_smile:

Updated original post.

Setup() has no closing }

{
  
void printTemperature(DeviceAddress deviceAddress)

should be

}
  
void printTemperature(DeviceAddress deviceAddress)

Your next problem will be "There is no WConstants.h" That's because they changed it to "Arduino.h"

Edit the file in arduino-1.0\libraries\DallasTemperature\DallasTemperature.cpp

The DallasTemperature_371Beta version has it on line 9

extern "C" {
  #include "WConstants.h"
}

To this.

extern "C" {
  #include "Arduino.h"
}

Or

extern "C" {
#if ARDUINO >= 100
  #include "Arduino.h"
#else
  #include "WProgram.h"
#endif
}

To keep the library compatible with Older versions of the IDE

Thanks for the help. Closing the setup with the correct } fixed it up. Now it works fine! Now to figure out how to make the temperature values displayed on the LCD show only whole numbers. I don't care about anything less than one degree F.

Try changing the lcd.print line to cast the result as an int.

 lcd.print( (int) DallasTemperature::toFahrenheit(tempC));