C++ 错误:在';之前应为非限定id;Arduino中的{&"x27;标记
我在为两个I2C传感器编译代码时遇到问题。我不断收到错误:C++ 错误:在';之前应为非限定id;Arduino中的{&"x27;标记,c++,arduino,i2c,arduino-uno,arduino-ide,C++,Arduino,I2c,Arduino Uno,Arduino Ide,我在为两个I2C传感器编译代码时遇到问题。我不断收到错误:在“{”标记之前应为非限定id 下面是我试图解决的代码: #include <i2cmaster.h> #include "Wire.h" // imports the wire library for talking over I2C int led = 13; //before void setup #define SENSOR_ADDR_OFF_OFF (0x4B) #define SENSOR_ADDR_O
在“{”标记之前应为非限定id
下面是我试图解决的代码:
#include <i2cmaster.h>
#include "Wire.h" // imports the wire library for talking over I2C
int led = 13;
//before void setup
#define SENSOR_ADDR_OFF_OFF (0x4B)
#define SENSOR_ADDR_OFF_ON (0x4A)
#define SENSOR_ADDR_ON_OFF (0x49)
#define SENSOR_ADDR_ON_ON (0x4m8)
// Set the sensor address here
const uint8_t sensorAddr = SENSOR_ADDR_OFF_OFF;
//void setup begins here
void setup()
{
// Start the serial port for output
Serial.begin(9600);
pinMode(led, OUTPUT);
// Join the I2C bus as master
Wire.begin();
// Set up the ADC on the sensor (reset everything)
i2c_init(); //Initialise the i2c bus
PORTC = (1 << PORTC4) | (1 << PORTC5);//enable pullups
}
//void loop begins here
void loop(){
int dev = 0x5A<<1;
int data_low = 0;
int data_high = 0;
int pec = 0;
i2c_start_wait(dev+I2C_WRITE);
i2c_write(0x07);
// read
i2c_rep_start(dev+I2C_READ);
data_low = i2c_readAck(); //Read 1 byte and then send ack
data_high = i2c_readAck(); //Read 1 byte and then send ack
pec = i2c_readNak();
i2c_stop();
//This converts high and low bytes together and processes temperature, MSB is a error bit and is ignored for temps
double tempFactor = 0.02; // 0.02 degrees per LSB (measurement resolution of the MLX90614)
double tempData = 0x0000; // zero out the data
int frac; // data past the decimal point
// This masks off the error bit of the high byte, then moves it left 8 bits and adds the low byte.
tempData = (double)(((data_high & 0x007F) << 8) + data_low);
tempData = (tempData * tempFactor)-0.01;
float celcius = tempData - 273.15;
float fahrenheit = (celcius*1.8) + 32;
Serial.print("Celcius: ");
Serial.println(celcius);
Serial.print("Fahrenheit: ");
Serial.println(fahrenheit);
}
uint8_t left;
uint8_t right;
{
if ((ReadByte(sensorAddr, 0x0, &left) == 0) &&
(ReadByte(sensorAddr, 0x1, &right) == 0))
{
// Use a threshold (value from 0-255) to determine if sensor detected a dark
// or light surface; the threshold should be modified according to the
// environment on which the sensor will be used
{
Serial.print("Left: ");
Serial.println(left);
}{
Serial.print("Right: ");
Serial.println(right);
}}
delay(1000);
}
// Read a byte on the i2c interface
int ReadByte(uint8_t addr, uint8_t reg, uint8_t *data)
{
// Do an i2c write to set the register that we want to read from
Wire.beginTransmission(addr);
Wire.write(reg);
Wire.endTransmission();
//Read a byte from the device
Wire.requestFrom(addr, (uint8_t)1);
if (Wire.available())
{
*data = Wire.read();
}
else
{
// Read nothing back
return -1;
}
return 0;
}
// Write a byte on the i2c interface
void WriteByte(uint8_t addr, uint8_t reg, byte data)
}
{
// Begin the write sequence
Wire.beginTransmission(addr);
// First byte is to set the register pointer
Wire.write(reg);
// Write the data byte
Wire.write(data);
// End the write sequence; bytes are actually transmitted now
Wire.endTransmission();
}
}
#包括
#包括“Wire.h”//imports用于通过I2C对话的Wire库
int led=13;
//无效设置之前
#定义传感器地址关闭(0x4B)
#定义传感器地址关闭打开(0x4A)
#定义传感器地址开关(0x49)
#定义传感器地址(0x4m8)
//在此处设置传感器地址
const uint8\u t sensorAddr=传感器ADDR\u OFF\u OFF;
//无效设置从这里开始
无效设置()
{
//启动串行端口进行输出
Serial.begin(9600);
引脚模式(led,输出);
//将I2C总线作为主总线连接
Wire.begin();
//在传感器上设置ADC(重置所有内容)
i2c_init();//初始化i2c总线
PORTC=(1您有这个函数,它以错误的大括号类型开始
void WriteByte(uint8_t addr, uint8_t reg, byte data)
} // Remove this, it's wrong
{ // Opening brace
... the rest of your code ...
} // Closing brace
} // Another closing brace, but I don't know why (I'd just remove it too)
你应该有
void WriteByte(uint8_t addr, uint8_t reg, byte data)
{ // Opening brace
... the rest of your code ...
} // Closing brace
修正你的缩进。你自己会发现问题的。提示:检查void WriteByte()
的括号。我仍然不明白你的意思。你能给我看一下这个格式化成代码的格式吗?所以我再次验证了代码,但是,我不断得到一个工作的带有led的\u两个传感器。ino:69:1:错误:预期在“}之前声明'标记显然在第69行。如果我删除第69行的大括号,我会得到另一个错误,即使用指示灯工作两个传感器。'ino:70:4:error:expected unqualified id在'If'@AnoopPatta You有很多看起来不必要的大括号'{'}’。清理所有这些问题,如果您仍然有问题,请在问题中正确地格式化代码,并清楚地指出错误是什么以及错误发生的位置(如您的答案中所示,因为只有一行代码没有多大帮助)。