Go Down

Topic: "a function-definition is not allowed here before '{' token" error (Read 813 times) previous topic - next topic

ylling

請問我的程式裡括號的部分是錯在哪呢,一直找不到答案......



#include <DHT.h>

#include <DHT_U.h>



void setup(){

#include" DHT.h "

#define dhtPin 2 //讀取DHT11數據

#define dhtType DHT11 // //插入DHT11   



#define FAN 8

#define HOT 9



#define PERIOD1 10 * 60 * 60 * 1000 //以幾為單位

#define PERIOD2 15 * 60 * 60 * 1000 //以毫米為單位

#define PERIOD3 25 * 60 * 60 * 1000



  // millis()返回無符號長,因此我們將使用它來跟踪

  無符號較長lastTime = 0;



  / *

    控制溫度的程序

    10分鍾維持80度

    15分鍾維持70度

    25分鍾維持60度

  * /



  DHT dht(dhtPin,dhtType); //初始化DHT傳感器



  int階段= 0;



  浮點t



  void setup(){

    Serial.begin(9600);//設置鮑率9600

    pinMode(FAN,OUTPUT);

    pinMode(熱,輸出);



    digitalWrite(FAN,LOW);

    digitalWrite(HOT,LOW);



    dht.begin(); //啟動DHT



    lastTime = millis();



    階段= 80;

  }



  void up()

  {

    if(stage> = t-3)

    {

      digitalWrite(FAN,HIGH);

      digitalWrite(HOT,LOW);

    }

    否則(階段<= t-3)

    {

      digitalWrite(FAN,HIGH);

      digitalWrite(HOT,LOW);

    }

    其他

    {

      digitalWrite(FAN,LOW);

      digitalWrite(HOT,LOW);

    }



  }



  無效循環()

  {

    t = dht.readTemperature();//讀取攝氏溫度

    float f = dht.readTemperature(真);//讀取華氏溫度是否

    (isnan(h)||  isnan(t)||  isnan(f)){

      Serial.println("無法從DHT傳感器讀取!");

      返回;

    }





    unsigned long now = millis();



    if(now-lastTime> = PERIOD1)//每隔PERIOD數為一次

    {

      階段= 80;

    }

    否則if(now-lastTime> = PERIOD2)//每隔PERIOD數為一次

    {

      階段= 70;

    }

    else if(now-lastTime> = PERIOD3)//每隔PERIOD數為一次

    {

      階段= 60;

    }



  }

}

UKHeliBob

My guess is that you have not got matched pairs of curly braces but I will not look at the code while it is in that state and much of it is not in English

Please follow the advice on posting a programming question given in Read this before posting a programming question

In particular note the advice to Auto format code in the IDE and to use code tags when posting code here as it prevents some combinations of characters in code being interpreted as HTML commands such as italics, bold or a smiley character, all of which render the code useless
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

6v6gt

1. You can only have one function called setup(). You have two.
2. The first function setup has no closing bracket like this:  '}'

@ crossed with UKHeliBob

UKHeliBob

OK.  I am feeling generous

Here is the code translated by Google, Auto formatted in teh IDE and posted here in code tags

Code: [Select]

#include <DHT.h>

#include <DHT_U.h>



void setup()
{
#include" DHT.h "
#define dhtPin 2 //Read DHT11 data
#define dhtType DHT11 // //Insert DHT11
#define FAN 8
#define HOT 9
#define PERIOD1 10 * 60 * 60 * 1000 //in a few units
#define PERIOD2 15 * 60 * 60 * 1000 //in millimeters
#define PERIOD3 25 * 60 * 60 * 1000
  // millis() returns unsigned long, so we will use it to track
  Unsigned longer lastTime = 0;
  / *
  Temperature control program
  Maintain 80 degrees for 10 minutes
  Maintain 70 degrees for 15 minutes
    Maintain 60 degrees in 25 minutes
    * /
    DHT dht (dhtPin, dhtType); //Initialize DHT sensor
  int stage = 0;
  Floating point t
  void setup()
  {
    Serial.begin(9600);//Set the baud rate to 9600
    pinMode (FAN, OUTPUT);
    pinMode (heat, output);
    digitalWrite(FAN, LOW);
    digitalWrite(HOT, LOW);
    dht.begin(); //Start DHT
    lastTime = millis();
    Stage = 80;
  }
  void up()
  {
    if (stage >= t - 3)
    {
      digitalWrite(FAN, HIGH);
      digitalWrite(HOT, LOW);
    }
    Otherwise (stage <= t - 3)
    {
      digitalWrite(FAN, HIGH);
      digitalWrite(HOT, LOW);
    }
    other
    {

      digitalWrite(FAN, LOW);

      digitalWrite(HOT, LOW);

    }
  }
  Invalid loop()
  {
    t = dht.readTemperature();//Read the temperature in Celsius
    float f = dht.readTemperature(true);//Whether read the temperature in Fahrenheit
    (Isnan(h) || isnan(t) || isnan(f))
    {
      Serial.println("Unable to read from DHT sensor!");
      return;
    }
    unsigned long now = millis();
    if (now - lastTime >= PERIOD1) //every PERIOD count
    {
      Stage = 80;
    }
    Otherwise if (now - lastTime >= PERIOD2) //every PERIOD count is once
    {
      Stage = 70;
    }
    else if (now - lastTime >= PERIOD3) //every PERIOD count
    {
      Stage = 60;
    }
  }
}


I will leave others to comment further on it
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

ylling

1.您只能擁有一個名為setup()的函數。你有兩個
2.第一個功能設置沒有這樣的

右括號:'}' @與UKHeliBob交叉
可以告訴我該如何解決嗎
兩個setup()函數可以同時存在嗎

void loop()
  {
    t = dht.readTemperature();//讀取攝氏溫度
    float f = dht.readTemperature(true);//讀取華氏溫度
    if (isnan(h) || isnan(t) || isnan(f))
    {
      Serial.println("無法從DHT傳感器讀取!");
      return;
    }


    unsigned long now = millis();

    if (now - lastTime >= PERIOD1) // this will be true every PERIOD milliseconds
    {
      stage = 80;
    }
    else if (now - lastTime >= PERIOD2) // this will be true every PERIOD milliseconds
    {
      stage = 70;
    }
    else if (now - lastTime >= PERIOD3) // this will be true every PERIOD milliseconds
    {
      stage = 60;
    }

  }
}



johnwasser

Here is the translation fixed to at least compile.  I don't have the specific DHT libraries so I commented those parts out.
Code: [Select]
//#include <DHT.h>
//#include <DHT_U.h>
//#include "DHT.h"


#define dhtPin 2 //Read DHT11 data
#define dhtType DHT11 // //Insert DHT11
const byte FAN = 8;
const byte HOT = 9;
const unsigned long PERIOD1 = (10ul * 60ul * 60ul * 1000ul); //in a few units
const unsigned long PERIOD2 = (15ul * 60ul * 60ul * 1000ul); //in millimeters
const unsigned long PERIOD3 = (25ul * 60ul * 60ul * 1000ul);
// millis() returns unsigned long, so we will use it to track
unsigned long lastTime = 0;
/*
  Temperature control program
  Maintain 80 degrees for 10 minutes
  Maintain 70 degrees for 15 minutes
  Maintain 60 degrees in 25 minutes
*/


//DHT dht (dhtPin, dhtType); //Initialize DHT sensor
int Stage = 0;
float t;


void setup()
{
  Serial.begin(9600);//Set the baud rate to 9600
  pinMode (FAN, OUTPUT);
  pinMode (HOT, OUTPUT);
  digitalWrite(FAN, LOW);
  digitalWrite(HOT, LOW);
  // dht.begin(); //Start DHT
  lastTime = millis();
  Stage = 80;
}


void up()
{
  if (Stage >= t - 3)
  {
    digitalWrite(FAN, HIGH);
    digitalWrite(HOT, LOW);
  }
  else if (Stage <= t - 3)
  {
    digitalWrite(FAN, HIGH);
    digitalWrite(HOT, LOW);
  }
  else // Stage == t-3
  {
    digitalWrite(FAN, LOW);
    digitalWrite(HOT, LOW);
  }
}


void loop()
{
//  t = dht.readTemperature();//Read the temperature in Celsius
  float f; // = dht.readTemperature(true);//Whether read the temperature in Fahrenheit
  float h;
  if (isnan(h) || isnan(t) || isnan(f))
  {
    Serial.println("Unable to read from DHT sensor!");
    return;
  }
  unsigned long now = millis();
  if (now - lastTime >= PERIOD1) //every PERIOD count
  {
    Stage = 80;
  }
  else if (now - lastTime >= PERIOD2) //every PERIOD count is once
  {
    Stage = 70;
  }
  else if (now - lastTime >= PERIOD3) //every PERIOD count
  {
    Stage = 60;
  }
}
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp
See who has no social life: https://forum.arduino.cc/index.php?action=stats :)

wildbill

That was quite a tangle - my initial attempts to compile it sent the compiler into an infinite loop - I had to close the IDE to stop it.

The climate control piece is the up function, but it's apparently never called.

ylling

這是至少可以編譯的翻譯。我沒有特定的DHT庫,因此我將那些部分註釋掉了。
Code: [Select]
//#include <DHT.h>
//#include <DHT_U.h>
//#include "DHT.h"


#define dhtPin 2 //Read DHT11 data
#define dhtType DHT11 // //Insert DHT11
const byte FAN = 8;
const byte HOT = 9;
const unsigned long PERIOD1 = (10ul * 60ul * 60ul * 1000ul); //in a few units
const unsigned long PERIOD2 = (15ul * 60ul * 60ul * 1000ul); //in millimeters
const unsigned long PERIOD3 = (25ul * 60ul * 60ul * 1000ul);
// millis() returns unsigned long, so we will use it to track
unsigned long lastTime = 0;
/*
  Temperature control program
  Maintain 80 degrees for 10 minutes
  Maintain 70 degrees for 15 minutes
  Maintain 60 degrees in 25 minutes
*/


//DHT dht (dhtPin, dhtType); //Initialize DHT sensor
int Stage = 0;
float t;


void setup()
{
  Serial.begin(9600);//Set the baud rate to 9600
  pinMode (FAN, OUTPUT);
  pinMode (HOT, OUTPUT);
  digitalWrite(FAN, LOW);
  digitalWrite(HOT, LOW);
  // dht.begin(); //Start DHT
  lastTime = millis();
  Stage = 80;
}


void up()
{
  if (Stage >= t - 3)
  {
    digitalWrite(FAN, HIGH);
    digitalWrite(HOT, LOW);
  }
  else if (Stage <= t - 3)
  {
    digitalWrite(FAN, HIGH);
    digitalWrite(HOT, LOW);
  }
  else // Stage == t-3
  {
    digitalWrite(FAN, LOW);
    digitalWrite(HOT, LOW);
  }
}


void loop()
{
//  t = dht.readTemperature();//Read the temperature in Celsius
  float f; // = dht.readTemperature(true);//Whether read the temperature in Fahrenheit
  float h;
  if (isnan(h) || isnan(t) || isnan(f))
  {
    Serial.println("Unable to read from DHT sensor!");
    return;
  }
  unsigned long now = millis();
  if (now - lastTime >= PERIOD1) //every PERIOD count
  {
    Stage = 80;
  }
  else if (now - lastTime >= PERIOD2) //every PERIOD count is once
  {
    Stage = 70;
  }
  else if (now - lastTime >= PERIOD3) //every PERIOD count
  {
    Stage = 60;
  }
}


https://github.com/adafruit/DHT-sensor-library
這是我使用的dht庫

johnwasser

https://github.com/adafruit/DHT-sensor-library
Thanks.
Code: [Select]
/*
  Temperature control program
  Maintain 80 degrees for 10 minutes
  Maintain 70 degrees for 15 minutes
  Maintain 60 degrees in 25 minutes
*/


#include <DHT.h>
const byte dhtPin = 2;  //Read DHT11 data
#define dhtType DHT11 // Insert DHT11
const byte FAN_PIN = 8;
const byte HEATER_PIN = 9;
const unsigned long PERIOD1 = (10ul * 60ul * 60ul * 1000ul); //in a few units
const unsigned long PERIOD2 = (15ul * 60ul * 60ul * 1000ul); //in millimeters
const unsigned long PERIOD3 = (25ul * 60ul * 60ul * 1000ul);


// millis() returns unsigned long, so we will use it to track
unsigned long StartTime = 0;


DHT dht (dhtPin, dhtType); //Initialize DHT sensor


int Setpoint = 0;  // Desired temperature
float t, h;
void setup()
{
  Serial.begin(9600);//Set the baud rate to 9600
  pinMode (FAN_PIN, OUTPUT);
  pinMode (HEATER_PIN, OUTPUT);
  digitalWrite(FAN_PIN, LOW);
  digitalWrite(HEATER_PIN, LOW);
  dht.begin(); //Start DHT
  StartTime = millis();
  Setpoint = 0;
}




void up()
{
  if (Setpoint == 0)
  {
    // Temperature control has ended
    digitalWrite(FAN_PIN, LOW);
    digitalWrite(HEATER_PIN, LOW);
    return;
  }


  if (Setpoint >= t + 3)  // Above maximum temperature
  {
    digitalWrite(FAN_PIN, HIGH);  // Turn on the fan 
    digitalWrite(HEATER_PIN, LOW); // Turn off the heater
  }
  else if (Setpoint <= t - 3)  // Below minimum temperature
  {
    digitalWrite(FAN_PIN, LOW);      // Turn off the fan
    digitalWrite(HEATER_PIN, HIGH);  // Turn on the heater
  }
  else // Setpoint == t +/- 3
  {
    // In Range.  Tuern off both
    digitalWrite(FAN_PIN, LOW);
    digitalWrite(HEATER_PIN, LOW);
  }
}




void loop()
{
  t = dht.readTemperature();//Read the temperature in Celsius
  float f = dht.readTemperature(true);//Whether read the temperature in Fahrenheit


  if (isnan(h) || isnan(t) || isnan(f))
  {
    Serial.println("Unable to read from DHT sensor!");
    return;
  }


  unsigned long now = millis();
  if (now - StartTime <= PERIOD1) //every PERIOD count
  {
    // First period.
    Setpoint = 80;
  }
  else if (now - StartTime <= PERIOD1 + PERIOD2) //every PERIOD count is once
  {
    // Second period
    Setpoint = 70;
  }
  else if (now - StartTime >= PERIOD1 + PERIOD2 + PERIOD3) //every PERIOD count
  {
    // Third period
    Setpoint = 60;
  }
  else
    Setpoint = 0;  // All done.


  up();  // Adjust temperature
}
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp
See who has no social life: https://forum.arduino.cc/index.php?action=stats :)

ylling

已經按照您的方式去做,但問題還是會產生......
怎麼會這樣呢? :(


6v6gt

Thank you for your picture of the error message.
In the black colored area below the the orange colored bar, we see only the last error message but if you scroll within the black area, there are certainly many more errors. We need to see all those errors.
Please use the copy and paste function of your PC to write the error message text so we can see them.

A guess is that a certain keyboard character has been used which has been rejected on your PC and has caused the errors.

ylling


UKHeliBob

This is not going well.  Screen shots are usually no help at all

On the screen shot in post #9 there is a button bottom/right of the black box.  In English this is labelled "Copy error messages".  Click on that then paste what has been copied in a new post, using code tags when you do
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

6v6gt

I am guessing that the characters like "{" and "}" are different on an English and a Chinese keyboard, even though visually they appear to be the same.

Delete the characters { and } and replace these with the same characters typed from your keyboard, then try again.

ylling

這進展不順利。屏幕截圖通常根本無濟於事

在文章9的屏幕截圖中,黑框的底部/右側有一個按鈕。用英語將其標記為"複製錯誤消息"。單擊該按鈕,然後在執行操作時使用代碼標籤粘貼已復制的內容到新帖子中
OK我直接複製過來了
Arduino:1.8.13(Windows 10),開發板:" Arduino Uno"

C:\ Users \ㄚㄚ\ Desktop \ 01 \ 01.ino:在函數" void setup()"中:
01:29:14:錯誤:此處不允許在'{'標記之前的函數定義
01:44:10:錯誤:在'{'標記之前不允許在此處進行功能定義
01:64:13:錯誤:在'{'標記之前不允許在此處進行功能定義
01:86:1:錯誤:輸入末尾預期為'}'
退出狀態1
'{'標記之前的功能定義在這裡是不允許的
該報告將包含更多信息
"在編譯期間顯示詳細輸出"
文件->首選項中啟用的選項。

Go Up