/* Example of BH1750 library usage. This example initialises the BH1750 object using the default high resolution continuous mode and then makes a light level reading every second. Connections - VCC to 3V3 or 5V - GND to GND - SCL to SCL (A5 on Arduino Uno, Leonardo, etc or 21 on Mega and Due, on esp8266 free selectable) - SDA to SDA (A4 on Arduino Uno, Leonardo, etc or 20 on Mega and Due, on esp8266 free selectable) - ADD to (not connected) or GND ADD pin is used to set sensor I2C address. If it has voltage greater or equal to 0.7VCC voltage (e.g. you've connected it to VCC) the sensor address will be 0x5C. In other case (if ADD voltage less than 0.7 * VCC) the sensor address will be 0x23 (by default). */ #include <BH1750.h> #include <Wire.h> BH1750 lightMeter; void setup() { Serial.begin(9600); // Initialize the I2C bus (BH1750 library doesn't do this automatically) Wire.begin(); // On esp8266 you can select SCL and SDA pins using Wire.begin(D4, D3); // For Wemos / Lolin D1 Mini Pro and the Ambient Light shield use // Wire.begin(D2, D1); lightMeter.begin(); Serial.println(F("BH1750 Test begin")); } void loop() { float lux = lightMeter.readLightLevel(); Serial.print("Light: "); Serial.print(lux); Serial.println(" lx"); delay(1000); } 用arduino运行后为什么会出现[BH1750] Device is not configured! Light: -2.00 lx 错误
时间: 2024-04-28 12:20:42 浏览: 8
这个错误可能是因为BH1750传感器没有正确初始化导致的。在BH1750库的使用中,需要调用`lightMeter.begin()`来初始化传感器。请确保你已经正确地连接了传感器,并且使用的是正确的I2C地址。你还可以尝试重新插拔传感器,或者检查你的代码是否正确。如果问题仍然存在,你可以尝试使用其他的库或者检查传感器是否损坏。
相关问题
In your own words, explain, using a most easy example, how the this reference works. What is an advantage of using this reference?
The `this` reference in programming is used to refer to the current object instance that a method or property belongs to. This is especially useful when you have multiple objects of the same class and need to differentiate between them.
For example, let's say we have a class called `Person` with a property called `name`. We create two instances of this class: `person1` and `person2`. If we want to set the `name` property for `person1`, we would use `person1.name = "John"`. But how would we set the `name` property for `person2`? We can't use `person1.name` again, because that would change the name for `person1` again. This is where the `this` reference comes in handy.
We can define the `name` property like this:
```
class Person {
constructor(name) {
this.name = name;
}
}
```
Now, when we create a new `Person` instance, we can use `this.name` to set the name property for that specific instance:
```
const person1 = new Person("John");
const person2 = new Person("Jane");
console.log(person1.name); // "John"
console.log(person2.name); // "Jane"
```
The advantage of using `this` is that it allows us to write more flexible and reusable code. We can create multiple instances of the same class, and each instance can have its own unique properties and values.
In your own words, explain, using an example, how the this reference works. What is an advantage of using this reference?
In object-oriented programming, the `this` reference is used to refer to the current instance of a class. It allows us to access and modify the properties and methods of the current object.
For example, let's say we have a class called `Person` with two properties: `name` and `age`. We can define a method called `introduce` that prints out a message introducing the person with their name and age:
```
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
introduce() {
console.log(`Hi, my name is ${this.name} and I am ${this.age} years old.`);
}
}
let john = new Person("John", 30);
john.introduce(); // Output: Hi, my name is John and I am 30 years old.
```
In this example, `this` refers to the current instance of the `Person` class, which is `john`. By using `this.name` and `this.age`, we can access the properties of the current instance and print them out in the `introduce` method.
An advantage of using the `this` reference is that it allows us to write more flexible and reusable code. If we create multiple instances of the `Person` class with different names and ages, the `introduce` method will automatically adapt and print out the correct information for each instance. Additionally, if we were to rename the `name` or `age` property in the future, we would only need to update it in one place (inside the class), and all methods that use `this.name` or `this.age` would still work correctly.