如何解决“在'{'标记之前不允许在此处进行功能定义”

时间:2019-05-13 01:01:17

标签: c++ c arduino-uno

我正在为Arduino Uno R3设置BMP180和GY-521,并在编译代码时给了我这个错误。

此外,抱歉,如果我发布了很多代码,我不知道您需要多少才能看到问题。

我对编码还很陌生,这是我的第一个主要项目之一,所以如果您能帮助的话,将不胜感激。

C:\Users\jdkak\AppData\Local\Temp\arduino_modified_sketch_680842\sketch_may12a.ino: In function 'void setup()':

sketch_may12a:49:2: error: a function-definition is not allowed here before '{' token

  {

  ^

sketch_may12a:232:1: error: expected '}' at end of input

 }

 ^

Using library Wire at version 1.0 in folder: C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\hardware\arduino\avr\libraries\Wire 
Using library Adafruit_Unified_Sensor at version 1.0.3 in folder: C:\Users\jdkak\OneDrive\Documents\Arduino\libraries\Adafruit_Unified_Sensor 
Using library Adafruit_BMP085_Unified-master at version 1.0.0 in folder: C:\Users\jdkak\OneDrive\Documents\Arduino\libraries\Adafruit_BMP085_Unified-master 
exit status 1
a function-definition is not allowed here before '{' token
void loop(void) 
 {
  Wire.beginTransmission(MPU_ADDR);
  Wire.write(0x3B); 
  Wire.endTransmission(false); 
  Wire.requestFrom(MPU_ADDR, 7*2, true);

   "Wire.read()<<8 | Wire.read();"
  accelerometer_x = Wire.read()<<8 | Wire.read(); 
  accelerometer_y = Wire.read()<<8 | Wire.read();
  temperature = Wire.read()<<8 | Wire.read(); // reading registers: 0x41 (TEMP_OUT_H) and 0x42 (TEMP_OUT_L)
  gyro_x = Wire.read()<<8 | Wire.read(); // reading registers: 0x43 (GYRO_XOUT_H) and 0x44 (GYRO_XOUT_L)
  gyro_y = Wire.read()<<8 | Wire.read(); // reading registers: 0x45 (GYRO_YOUT_H) and 0x46 (GYRO_YOUT_L)
  gyro_z = Wire.read()<<8 | Wire.read(); // reading registers: 0x47 (GYRO_ZOUT_H) and 0x48 (GYRO_ZOUT_L)

  // print out data
  Serial.print("Accelerometer");
  Serial.println();
  Serial.print(" X = "); Serial.print(convert_int16_to_str(accelerometer_x));
  Serial.print(" Y = "); Serial.print(convert_int16_to_str(accelerometer_y));
  Serial.print(" Z = "); Serial.print(convert_int16_to_str(accelerometer_z));
  Serial.println();
  // the following equation was taken from the documentation [MPU-6000/MPU-6050 Register Map and Description, p.30]
  Serial.print("Temperature");
  Serial.println();
  Serial.print(temperature/340.00+36.53);
  Serial.println();
  Serial.print("GyroScope");
  Serial.println();
  Serial.print("  X="); Serial.print(convert_int16_to_str(gyro_x));
  Serial.print("  Y="); Serial.print(convert_int16_to_str(gyro_y));
  Serial.print("  Z="); Serial.print(convert_int16_to_str(gyro_z));
  Serial.println();
  Serial.println();

    /* Get a new sensor event */ 
  sensors_event_t event;
  bmp.getEvent(&event);

  /* Display the results (barometric pressure is measure in hPa) */
  if (event.pressure)
  {
    /* Display atmospheric pressue in hPa */
    Serial.print("Pressure:    ");
    Serial.print(event.pressure);
    Serial.println(" hPa");


    float temperature;
    bmp.getTemperature(&temperature);
    Serial.print("Temperature: ");
    Serial.print(temperature + 1.8 + 32);
    Serial.println(" F");

    float seaLevelPressure = SENSORS_PRESSURE_SEALEVELHPA;
    Serial.print("Altitude:    "); 
    Serial.print(bmp.pressureToAltitude(seaLevelPressure,
                                        event.pressure)); 
    Serial.println(" m");
    Serial.println("");

  else
  {
    Serial.println("Sensor error");
  }
delay(1000);
}

2 个答案:

答案 0 :(得分:1)

循环功能没有}来关闭括号的范围(您只能关闭else块的范围)。查看错误消息:

In function 'void setup()':

似乎您也缺少该函数的结束},因此抱怨您无法在函数“ setup”内声明函数“ loop”。

答案 1 :(得分:0)

我认为在其他地方添加}可以解决问题。没有}可以关闭if块。