Pages: [1]   Go Down
Author Topic: error: expected unqualified-id before 'if'  (Read 623 times)
0 Members and 1 Guest are viewing this topic.
Czech Republic
Offline Offline
Newbie
*
Karma: 0
Posts: 11
EAGLE is addiciting
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello everybody, I'm trying to make a clock that also measures humidity and temperature using SparkFun DHT22 temp. and humidity sensor, DS3231 clock IC and a 16x2 LCD. I threw these three programs together (still a little new to programming). Now, the clock IC program is working with 1 second delay, the DHT22 is using 3 second delay. I honestly don't know millis() and I don't think it would be much reliable too. "This number will overflow (go back to zero), after approximately 50 days." I set up a little if() that executes the DHT22 part if count is 3 (and sets count to zero), else it adds one to count and does not execute the DHT22 part. I keep on getting this error right at the if(count == 3) part. Here is the code:

Code:
#include <Wire.h>

#define DS3231_I2C_ADDRESS 104
#define DHT11_PIN 0      // ADC0

byte seconds, minutes, hours, day, date, month, year;
int count;
char weekDay[4];

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  lcd.clear();
  Wire.begin();
}

void loop() {
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  get3231Date();
  watchConsole();
  lcd.setCursor(0, 4);
  lcd.print(hours, DEC);
  lcd.print(":");
  lcd.print(minutes, DEC);
  lcd.print(":");
  lcd.print(seconds, DEC);
}

// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
  return ( (val/10*16) + (val%10) );
}

void watchConsole()
{
  if (Serial.available()) {      // Look for char in serial queue and process if found
    if (Serial.read() == 84) {      //If command = "T" Set Date
      set3231Date();
      get3231Date();
      Serial.println(" ");
    }
  }
}

void set3231Date()
{
  //T(sec)(min)(hour)(dayOfWeek)(dayOfMonth)(month)(year)
  //T(00-59)(00-59)(00-23)(1-7)(01-31)(01-12)(00-99)
  //Example: 02-Feb-09 @ 19:57:11 for the 3rd day of the week -> T1157193020209

  seconds = (byte) ((Serial.read() - 48) * 10 + (Serial.read() - 48)); // Use of (byte) type casting and ascii math to achieve result.  
  minutes = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
  hours   = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
  day     = (byte) (Serial.read() - 48);
  date    = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
  month   = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
  year    = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.send(0x00);
  Wire.send(decToBcd(seconds));
  Wire.send(decToBcd(minutes));
  Wire.send(decToBcd(hours));
  Wire.send(decToBcd(day));
  Wire.send(decToBcd(date));
  Wire.send(decToBcd(month));
  Wire.send(decToBcd(year));
  Wire.endTransmission();
}


void get3231Date()
{
  // send request to receive data starting at register 0
  Wire.beginTransmission(DS3231_I2C_ADDRESS); // 104 is DS3231 device address
  Wire.send(0x00); // start at register 0
  Wire.endTransmission();
  Wire.requestFrom(DS3231_I2C_ADDRESS, 7); // request seven bytes

  if(Wire.available()) {
    seconds = Wire.receive(); // get seconds
    minutes = Wire.receive(); // get minutes
    hours   = Wire.receive();   // get hours
    day     = Wire.receive();
    date    = Wire.receive();
    month   = Wire.receive(); //temp month
    year    = Wire.receive();

    seconds = (((seconds & B11110000)>>4)*10 + (seconds & B00001111)); // convert BCD to decimal
    minutes = (((minutes & B11110000)>>4)*10 + (minutes & B00001111)); // convert BCD to decimal
    hours   = (((hours & B00110000)>>4)*10 + (hours & B00001111)); // convert BCD to decimal (assume 24 hour mode)
    day     = (day & B00000111); // 1-7
    date    = (((date & B00110000)>>4)*10 + (date & B00001111)); // 1-31
    month   = (((month & B00010000)>>4)*10 + (month & B00001111)); //msb7 is century overflow
    year    = (((year & B11110000)>>4)*10 + (year & B00001111));
  }
  else {
    //oh noes, no data!
  }

  switch (day) {
  case 1:
    strcpy(weekDay, "Sun");
    break;
  case 2:
    strcpy(weekDay, "Mon");
    break;
  case 3:
    strcpy(weekDay, "Tue");
    break;
  case 4:
    strcpy(weekDay, "Wed");
    break;
  case 5:
    strcpy(weekDay, "Thu");
    break;
  case 6:
    strcpy(weekDay, "Fri");
    break;
  case 7:
    strcpy(weekDay, "Sat");
    break;
  }
}

[glow]if (count == 3) {[/glow]

  count = 0;

  byte read_dht11_dat()
  {
    byte i = 0;
    byte result=0;
    for(i=0; i< 8; i++){

      while(!(PINC & _BV(DHT11_PIN)));  // wait 50us finish
      delayMicroseconds(30);
      if(PINC & _BV(DHT11_PIN))
        result |=(1<<(7-i));
      while((PINC & _BV(DHT11_PIN)));  // wait '1' finish

    }
    return result;
  }

  //Serial.println("A");
  byte dht11_dat[5];
  byte dht11_in;
  byte i;
  // start condition
  // 1. pull-down i/o pin from 18ms
  PORTC &= ~_BV(DHT11_PIN);
  //delay(18);  //DHT11 must >=18ms
  delay(5);  //DHT22 must 1~10ms
  PORTC |= _BV(DHT11_PIN);  // pull-up 20-40us
  delayMicroseconds(40);

  DDRC &= ~_BV(DHT11_PIN);
  delayMicroseconds(40);

  dht11_in = PINC & _BV(DHT11_PIN);
  //Serial.println("B"); //miss first 2 bits on DHT22
  if(dht11_in){
    Serial.println("dht11 start condition 1 not met");
    return;
  }
  delayMicroseconds(80);
  dht11_in = PINC & _BV(DHT11_PIN);

  if(!dht11_in){
    Serial.println("dht11 start condition 2 not met");
    return;
  }
  delayMicroseconds(80);

  // now ready for data reception
  for (i=0; i<5; i++)
    dht11_dat[i] = read_dht11_dat();

  DDRC |= _BV(DHT11_PIN);
  PORTC |= _BV(DHT11_PIN);

  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");

  //debug
  for (i=0; i<5; i++) {
    Serial.print(dht11_dat[i], HEX);
    Serial.print(' ');
  }
  Serial.print("check_sum=");
  Serial.print(dht11_check_sum, HEX);
  Serial.println(' ');
  //end of debug

  int HighByte, LowByte, TReading, SignBit, Whole, Fract;   //DHT22
  HighByte = dht11_dat[0];   //DHT22
  LowByte = dht11_dat[1];   //DHT22
  TReading = (HighByte << 8) + LowByte;   //DHT22
  Whole = TReading / 10;   //DHT22
  Fract = TReading % 10;   //DHT22

  lcd.setCursor(1,3);
  //      Serial.print(dht11_dat[0], DEC);  //DHT11
  //      Serial.print(".");  //DHT11
  //      Serial.print(dht11_dat[1], DEC);  //DHT11
  lcd.print(Whole);  //DHT22
  lcd.print("%  ");   //DHT22

  HighByte = dht11_dat[2];
  LowByte = dht11_dat[3];
  TReading = (HighByte << 8) + LowByte;
  SignBit = TReading & 0x8000;  // test most sig bit
  if (SignBit) // negative
    TReading = (TReading ^ 0xffff) + 1;
  Whole = TReading / 10;
  Fract = TReading % 10;

  lcd.setCursor(1,10);
  //      Serial.print(dht11_dat[2], DEC);  //DHT11
  //      Serial.print(".");  //DHT11
  //      Serial.print(dht11_dat[3], DEC);  //DHT11

  if (SignBit) // If its negative
    lcd.print("-");
  lcd.print(Whole);  //DHT22
  Serial.println("C");  //DHT22

  //delay(2000);  //DHT11
  delay(3000);  //DHT22

  delayMicroseconds(810);
}

else{
  count++;
  delay(1);
}
delay(999);

Yes I already read all the topics about this and similar errors, I couldn't find anything useful. Either I'm blind or this is caused by something else. Does anyone see anything incorrect in the code? I might edit the code to omit functions like weekday etc. because I'm not going to be using them. Only hours, minutes, seconds and temperature + humidity will be used.
Logged

North Carolina, USA
Offline Offline
Full Member
***
Karma: 1
Posts: 137
:O Arduino!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've never been able to read code effectively in the forum code boxes, but is that if statement in a method? seems like it's just chilling out there.
Logged

Czech Republic
Offline Offline
Newbie
*
Karma: 0
Posts: 11
EAGLE is addiciting
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Bingo. And I nearly broke my keyboard with my head. I also moved other bytes and voids out of the void loop. And it compiled. Thanks. A lot.
Code:
#include <Wire.h>

#define DS3231_I2C_ADDRESS 104
#define DHT11_PIN 0      // ADC0

byte seconds, minutes, hours, day, date, month, year;
int count;
char weekDay[4];

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  lcd.clear();
  Wire.begin();
}

void loop() {
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  get3231Date();
  watchConsole();
  lcd.setCursor(0, 4);
  lcd.print(hours, DEC);
  lcd.print(":");
  lcd.print(minutes, DEC);
  lcd.print(":");
  lcd.print(seconds, DEC);

  if (count == 3) {

    count = 0;



    //Serial.println("A");
    byte dht11_dat[5];
    byte dht11_in;
    byte i;
    // start condition
    // 1. pull-down i/o pin from 18ms
    PORTC &= ~_BV(DHT11_PIN);
    //delay(18);  //DHT11 must >=18ms
    delay(5);  //DHT22 must 1~10ms
    PORTC |= _BV(DHT11_PIN);  // pull-up 20-40us
    delayMicroseconds(40);

    DDRC &= ~_BV(DHT11_PIN);
    delayMicroseconds(40);

    dht11_in = PINC & _BV(DHT11_PIN);
    //Serial.println("B"); //miss first 2 bits on DHT22
    if(dht11_in){
      Serial.println("dht11 start condition 1 not met");
      return;
    }
    delayMicroseconds(80);
    dht11_in = PINC & _BV(DHT11_PIN);

    if(!dht11_in){
      Serial.println("dht11 start condition 2 not met");
      return;
    }
    delayMicroseconds(80);

    // now ready for data reception
    for (i=0; i<5; i++)
      dht11_dat[i] = read_dht11_dat();

    DDRC |= _BV(DHT11_PIN);
    PORTC |= _BV(DHT11_PIN);

    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");

    //debug
    for (i=0; i<5; i++) {
      Serial.print(dht11_dat[i], HEX);
      Serial.print(' ');
    }
    Serial.print("check_sum=");
    Serial.print(dht11_check_sum, HEX);
    Serial.println(' ');
    //end of debug

    int HighByte, LowByte, TReading, SignBit, Whole, Fract;   //DHT22
    HighByte = dht11_dat[0];   //DHT22
    LowByte = dht11_dat[1];   //DHT22
    TReading = (HighByte << 8) + LowByte;   //DHT22
    Whole = TReading / 10;   //DHT22
    Fract = TReading % 10;   //DHT22

    lcd.setCursor(1,3);
    //      Serial.print(dht11_dat[0], DEC);  //DHT11
    //      Serial.print(".");  //DHT11
    //      Serial.print(dht11_dat[1], DEC);  //DHT11
    lcd.print(Whole);  //DHT22
    lcd.print("%  ");   //DHT22

    HighByte = dht11_dat[2];
    LowByte = dht11_dat[3];
    TReading = (HighByte << 8) + LowByte;
    SignBit = TReading & 0x8000;  // test most sig bit
    if (SignBit) // negative
      TReading = (TReading ^ 0xffff) + 1;
    Whole = TReading / 10;
    Fract = TReading % 10;

    lcd.setCursor(1,10);
    //      Serial.print(dht11_dat[2], DEC);  //DHT11
    //      Serial.print(".");  //DHT11
    //      Serial.print(dht11_dat[3], DEC);  //DHT11

    if (SignBit) // If its negative
      lcd.print("-");
    lcd.print(Whole);  //DHT22
    Serial.println("C");  //DHT22

    //delay(2000);  //DHT11
    delay(3000);  //DHT22

    delayMicroseconds(810);
  }

  else{
    count++;
    delay(1);
  }
  delay(999);
}

// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
  return ( (val/10*16) + (val%10) );
}

void watchConsole()
{
  if (Serial.available()) {      // Look for char in serial queue and process if found
    if (Serial.read() == 84) {      //If command = "T" Set Date
      set3231Date();
      get3231Date();
      Serial.println(" ");
    }
  }
}

void set3231Date()
{
  //T(sec)(min)(hour)(dayOfWeek)(dayOfMonth)(month)(year)
  //T(00-59)(00-59)(00-23)(1-7)(01-31)(01-12)(00-99)
  //Example: 02-Feb-09 @ 19:57:11 for the 3rd day of the week -> T1157193020209

  seconds = (byte) ((Serial.read() - 48) * 10 + (Serial.read() - 48)); // Use of (byte) type casting and ascii math to achieve result.  
  minutes = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
  hours   = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
  day     = (byte) (Serial.read() - 48);
  date    = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
  month   = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
  year    = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.send(0x00);
  Wire.send(decToBcd(seconds));
  Wire.send(decToBcd(minutes));
  Wire.send(decToBcd(hours));
  Wire.send(decToBcd(day));
  Wire.send(decToBcd(date));
  Wire.send(decToBcd(month));
  Wire.send(decToBcd(year));
  Wire.endTransmission();
}

void get3231Date()
{
  // send request to receive data starting at register 0
  Wire.beginTransmission(DS3231_I2C_ADDRESS); // 104 is DS3231 device address
  Wire.send(0x00); // start at register 0
  Wire.endTransmission();
  Wire.requestFrom(DS3231_I2C_ADDRESS, 7); // request seven bytes

  if(Wire.available()) {
    seconds = Wire.receive(); // get seconds
    minutes = Wire.receive(); // get minutes
    hours   = Wire.receive();   // get hours
    day     = Wire.receive();
    date    = Wire.receive();
    month   = Wire.receive(); //temp month
    year    = Wire.receive();

    seconds = (((seconds & B11110000)>>4)*10 + (seconds & B00001111)); // convert BCD to decimal
    minutes = (((minutes & B11110000)>>4)*10 + (minutes & B00001111)); // convert BCD to decimal
    hours   = (((hours & B00110000)>>4)*10 + (hours & B00001111)); // convert BCD to decimal (assume 24 hour mode)
    day     = (day & B00000111); // 1-7
    date    = (((date & B00110000)>>4)*10 + (date & B00001111)); // 1-31
    month   = (((month & B00010000)>>4)*10 + (month & B00001111)); //msb7 is century overflow
    year    = (((year & B11110000)>>4)*10 + (year & B00001111));
  }
  else {
    //oh noes, no data!
  }

  switch (day) {
  case 1:
    strcpy(weekDay, "Sun");
    break;
  case 2:
    strcpy(weekDay, "Mon");
    break;
  case 3:
    strcpy(weekDay, "Tue");
    break;
  case 4:
    strcpy(weekDay, "Wed");
    break;
  case 5:
    strcpy(weekDay, "Thu");
    break;
  case 6:
    strcpy(weekDay, "Fri");
    break;
  case 7:
    strcpy(weekDay, "Sat");
    break;
  }
}

byte read_dht11_dat()
{
  byte i = 0;
  byte result=0;
  for(i=0; i< 8; i++){

    while(!(PINC & _BV(DHT11_PIN)));  // wait 50us finish
    delayMicroseconds(30);
    if(PINC & _BV(DHT11_PIN))
      result |=(1<<(7-i));
    while((PINC & _BV(DHT11_PIN)));  // wait '1' finish

  }
  return result;
}
« Last Edit: January 03, 2011, 11:42:28 am by Johnyz » Logged

Pages: [1]   Go Up
Jump to: